Java
Java
Amazon SES v2: Verify a sending domain with Easy DKIM
See more Amazon SES Examples
Use a domain you control. Run once to create the regional identity, publish the returned CNAMEs with your DNS provider, then set checkExisting to true and run again to read the status. The target suffix comes from SES. If your DNS provider appends the domain automatically, enter just the relative record name.
CreateEmailIdentity · GetEmailIdentity · Easy DKIM records
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.AuthAws;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.Socket;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
String domainName = "example.com";
boolean checkExisting = false;
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
String region = "us-west-2";
StringBuilder host = new StringBuilder();
host.append("email.");
host.append(region);
host.append(".amazonaws.com");
AuthAws signer = new AuthAws();
// Supply the credentials from your application's credential provider.
signer.setAccessKey("AWS_ACCESS_KEY_ID");
signer.setSecretKey("AWS_SECRET_ACCESS_KEY");
signer.setRegion(region);
signer.setServiceName("ses");
Socket connection = new Socket();
connection.setRequireSslCertVerify(true);
try {
connection.connect(host.getAsString(), 443, true, 10000);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Rest rest = new Rest();
try {
rest.useConnection(connection, false);
rest.setAuthAws(signer);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// For temporary AWS credentials, supply the session token from the same provider.
String sessionToken = "";
if (!"".equals(sessionToken)) {
rest.addQueryParam("X-Amz-Security-Token", sessionToken);
}
String responseText = "";
if (checkExisting) {
StringBuilder path = new StringBuilder();
path.append("/v2/email/identities/");
path.append(domainName);
responseText = rest.fullRequestNoBody("GET", path.getAsString());
} else {
JsonObject identity = new JsonObject();
identity.updateString("EmailIdentity", domainName);
rest.addHeader("Content-Type", "application/json");
responseText = rest.fullRequestString("POST", "/v2/email/identities", identity.emit());
}
if (!rest.getLastMethodSuccess()) {
System.out.println(rest.getLastErrorText());
return;
}
if (rest.getResponseStatusCode() != 200) {
System.out.println("SES returned HTTP " + rest.getResponseStatusCode() + " " + responseText);
return;
}
JsonObject response = new JsonObject();
try {
response.load(responseText);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("DKIM status: " + response.stringOf("DkimAttributes.Status"));
System.out.println("Verified for sending: " + response.boolOf("VerifiedForSendingStatus"));
int count = response.sizeOfArray("DkimAttributes.Tokens");
if (count < 1) {
System.out.println("No Easy DKIM tokens were returned. Check this identity's DKIM configuration.");
return;
}
if (!response.hasMember("DkimAttributes.SigningHostedZone")) {
System.out.println("Find the CNAME targets for this identity in the SES console.");
return;
}
String zone = response.stringOf("DkimAttributes.SigningHostedZone");
int i = 0;
while (i < count) {
response.setI(i);
String token = response.stringOf("DkimAttributes.Tokens[i]");
System.out.println(token + "._domainkey." + domainName + " CNAME " + token + "." + zone);
i = i + 1;
}
}
}