我试图使用来自亚太地区(孟买)的aws ses java sdk发送电子邮件。请参阅下面的代码,我尝试了AWSClientService(用于获取凭据,客户端,创建模板等)
@Service
public class AWSClientServiceImpl implements AWSClientService {
@Value("${aws.ses.accesKey}")
private String accessKey;
@Value("${aws.ses.secretKey}")
private String secretKey;
@Override
public AWSCredentialsProvider getAWSCredentials(){
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey,secretKey);
return new AWSStaticCredentialsProvider(credentials);
}
@Override
public AmazonSimpleEmailService getAmazonSESClient(){
return AmazonSimpleEmailServiceClientBuilder.standard()
.withCredentials(getAWSCredentials())
.withRegion(Regions.AP_SOUTH_1).build();
}
@Override
public VerifyEmailIdentityResult verifyEmailIdentity(AmazonSimpleEmailService client, String emailAddress){
VerifyEmailIdentityRequest emailIdentityRequest = new VerifyEmailIdentityRequest().withEmailAddress(emailAddress);
return client.verifyEmailIdentity(emailIdentityRequest);
}
@Override
public CreateTemplateResult createTemplate(AmazonSimpleEmailService amazonSES, String templateName, String subjectPart, String htmlPart) {
Template template = new Template();
template.setTemplateName(templateName);
template.setSubjectPart(subjectPart);
template.setHtmlPart(htmlPart);
CreateTemplateRequest createTemplateRequest = new CreateTemplateRequest();
createTemplateRequest.setTemplate(template);
return amazonSES.createTemplate(createTemplateRequest);
}
}
使用此我尝试从另一个类发送电子邮件
public void sendSimpleSESMessage(){
final String FROM = "[email protected]";
final String TO = "[email protected]";
final String SUBJECT = "Amazon SES test (AWS SDK for Java)";
final String HTMLBODY = "<h1>Amazon SES test (AWS SDK for Java)</h1>"
+ "<p>This email was sent with <a href='https://aws.amazon.com/ses/'>"
+ "Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-java/'>"
+ "AWS SDK for Java</a>";
final String TEXTBODY = "This email was sent through Amazon SES "
+ "using the AWS SDK for Java.";
try {
AmazonSimpleEmailService client = awsClientService.getAmazonSESClient();
log.info("Email Verification for " + FROM + " started");
VerifyEmailIdentityResult verifyEmailIdentityResult = awsClientService.verifyEmailIdentity(client, FROM);
log.info("Email verification for " + FROM + " completed");
SendEmailRequest request = new SendEmailRequest()
.withDestination(
new Destination().withToAddresses(TO))
.withMessage(new com.amazonaws.services.simpleemail.model.Message()
.withBody(new Body()
.withHtml(new Content()
.withCharset("UTF-8").withData(HTMLBODY))
.withText(new Content()
.withCharset("UTF-8").withData(TEXTBODY)))
.withSubject(new Content()
.withCharset("UTF-8").withData(SUBJECT)))
.withSource(FROM);
client.sendEmail(request);
log.info("Email was sent from "+FROM+" to "+TO);
} catch (Exception ex) {
ex.printStackTrace();
}
}
当我尝试执行此方法时,我得到了一个例外
com.amazonaws.SdkClientException:无法执行HTTP请求:连接到email.ap-south-1.amazonaws.com:443 [email.ap-south-1.amazonaws.com/13.126.113.212,cmail.ap-south- 1.amazonaws.com/35.154.131.193,cmail.ap-south-1.amazonaws.com/13.126.245.211]失败:连接超时
可能是什么原因?是因为我使用了亚太地区(孟买)吗?如果是这样,我应该使用哪个地区?因为我是印度人,我可以使用其他地区吗?我是aws ses的初学者。请帮我
您的端点未列在AWS区域和端点列表中。您确定要与真实终端连接吗?你检查过你是否被防火墙阻止了?
参考:https://docs.aws.amazon.com/general/latest/gr/rande.html#ses_region
我将该地区改为美国东部(弗吉尼亚州北部),然后就有效了。确保防火墙未被阻止
SES仅在3个地区提供。 us-east-1,eu-west-1和us-west-2 https://docs.aws.amazon.com/general/latest/gr/rande.html#ses_region