使用来自亚太地区(孟买)的AWS SES SDK(Java)发送电子邮件



我试图使用来自亚太地区(孟买)的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 = "test@sample.com";
    final String TO = "test@sample.com";
    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.ap-south-nawaws.amazonaws.com/13.123.126.113.212,email..ap,email.ap-south-1.amazonaws.com/35.154.131.193,email.ap.ap-south-1.amazonaws.com/13.13.126.245.211]失败:连接时间

可能是什么原因?是因为我使用了亚太地区(孟买)吗?如果是这样,我应该使用哪个区域?因为我是印度人,所以我可以使用其他地区吗?我是AWS SES的初学者。请帮助我

您的端点未在AWS区域和端点列表中列出。您确定要与真实端点连接吗?您是否检查过是否被防火墙阻止了?

参考:https://docs.aws.amazon.com/general/latest/gr/rande.html#ses_region

我将区域更改为East(N. Virginia),然后起作用。确保防火墙未被阻止

SES仅在3个区域可用。US-EAST-1,EU-WEST-1和US-WEST-2https://docs.aws.amazon.com/general/latest/gr/rande.html#ses_region

最新更新