使用 aws ses sdk(Java) 从亚太地区(孟买)发送电子邮件
Send emails using aws ses sdk(Java) from region Asia Pacific (Mumbai)
我试图使用来自亚太地区(孟买)的 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);
}
}
使用这个我尝试从另一个发送电子邮件 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();
}
}
当我尝试执行此方法时出现异常
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
可能是什么原因?
是因为我使用了亚太地区(孟买)吗?
如果是这样,我应该使用哪个区域?
因为我是印度人,我可以使用其他地区吗?
我是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
我试图使用来自亚太地区(孟买)的 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);
}
}
使用这个我尝试从另一个发送电子邮件 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();
}
}
当我尝试执行此方法时出现异常
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
可能是什么原因? 是因为我使用了亚太地区(孟买)吗? 如果是这样,我应该使用哪个区域? 因为我是印度人,我可以使用其他地区吗? 我是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