问题可能是因为在服务器上缺少AWS配置文件。可以在服务器上创建~/.aws/credentials
文件,并添加AWS密钥和访问密钥。
[default]
aws_access_key_id = ACCESS_KEY
aws_secret_access_key = SECRET_KEY
在代码中添加以下代码来读取credentials
文件:
try {
//设置系统变量指示将在本地FileSystemCredentialsProvider中查找默认凭据配置文件'credentials”。
System.setProperty("aws.sharedCredentialsFile", "/home/username/.aws/credentials");
//或者用以下代码来指定凭据文件的位置:
//ProfilesConfigFile configFile = new ProfilesConfigFile("/home/username/.aws/credentials");
//然后指定ProfileName即可。
//builder.setCredentials(new ProfileCredentialsProvider(configFile, "default"));
//使用AWS传输客户端来创建一个新的Amazon SES客户端
AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard().build();
// 创建发送电子邮件的请求
SendEmailRequest request = new SendEmailRequest()
.withDestination(
new Destination().withToAddresses("recipient@example.com"))
.withMessage(new Message()
.withBody(new Body()
.withHtml(new Content()
.withCharset("UTF-8").withData("HTML BODY
"))
.withText(new Content()
.withCharset("UTF-8").withData("TEXT BODY")))
.withSubject(new Content()
.withCharset("UTF-8").withData("Test email")))
.withSource("sender@example.com");
// 发送电子邮件
SendEmailResult response = client.sendEmail(request);
System.out.println("Email sent!");
} catch (AmazonServiceException e) {
// 抛出AmazonServiceException异常时,Amazon SES未能调用成功。
System.out.println("The email was not sent.");
System.out.println("Error message: " + e.getMessage());
} catch (Exception e) {
// 抛出异常时
System.out.println("The email was not sent.");
System.out.println("Error message: " + e.getMessage());
}
在以上代码中,ACCESS_KEY和SECRET_KEY
是AWS的访问凭据。/home/username/.aws/credentials
是用户AWS凭证的配置文件位置。recipient@example.com
和sender@example.com
是收件人和发件人的电子邮件地址。其他内容
下一篇:AWSSES中的电子邮件状态