简体   繁体   中英

Send emails using aws ses sdk(Java) from region Asia Pacific (Mumbai)

I was trying to send emails using aws ses java sdk from the region Asia Pacific (Mumbai). See the below code which i tried AWSClientService(for getting credentials, client, create template etc.)

@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);
}
}

Using this i try to send email from another class

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();
    }
}

When i try to execute this method i got an exception

com.amazonaws.SdkClientException: Unable to execute HTTP request: Connect to email.ap-south-1.amazonaws.com:443 [email.ap-south-1.amazonaws.com/13.126.113.212, email.ap-south-1.amazonaws.com/35.154.131.193, email.ap-south-1.amazonaws.com/13.126.245.211] failed: connect timed out

What may be the reason ? Is it because of i used the region Asia Pacific (Mumbai) ? If so, which region should i use ? As i am an Indian can i use other regions? I am beginner in aws ses. Please help me

Your endpoint is not listed in AWS regions and endpoint list. Are you sure you are connecting with the real endpoint? Did you check whether you are blocked by firewall?

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

I changed the region to US East (N. Virginia), then it worked. Make sure the firewall is not blocked

SES is only available in 3 regions. us-east-1, eu-west-1 and us-west-2 https://docs.aws.amazon.com/general/latest/gr/rande.html#ses_region

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM