无法在Java中使用Amazon SES发送电子邮件
我使用下面提到的代码发送电子邮件无法在Java中使用Amazon SES发送电子邮件,java,email,amazon-web-services,Java,Email,Amazon Web Services,我使用下面提到的代码发送电子邮件 public static void send(String email, String subject, String body) { try { fromEmail = "abc.@xyz.com"; Content subjectContent = new Content(subject); Destination destination = new Destination().withToAddr
public static void send(String email, String subject, String body) {
try {
fromEmail = "abc.@xyz.com";
Content subjectContent = new Content(subject);
Destination destination = new Destination().withToAddresses(new String[] { "cde@gmail.com" });
Content htmlContent = new Content().withData("<h1>Hello - I hope you're having a good day.</h1>");
Body msgBody = new Body().withHtml(htmlContent);
// Create a message with the specified subject and body.
Message message = new Message().withSubject(subjectContent).withBody(msgBody);
SendEmailRequest request = new SendEmailRequest()
.withSource(fromEmail)
.withDestination(destination)
.withMessage(message);
SendRawEmailRequest sendRawEmailRequest = new SendRawEmailRequest()
.withSource(fromEmail)
.withDestinations(destination.getBccAddresses())
.withRawMessage(new RawMessage());
AWSCredentials credentials = new BasicAWSCredentials(userName,password);
AmazonSimpleEmailServiceClient sesClient = new AmazonSimpleEmailServiceClient(credentials);
// ListVerifiedEmailAddressesResult verifiedEmails =
// sesClient.listVerifiedEmailAddresses();
SendRawEmailResult result = sesClient.sendRawEmail(sendRawEmailRequest);
System.out.println(result + "Email sent");
} catch (Exception e) {
logger.error("Caught a MessagingException, which means that there was a "
+ "problem sending your message to Amazon's E-mail Service check the "
+ "stack trace for more information.{}" + e.getMessage());
e.printStackTrace();
}
}
公共静态无效发送(字符串电子邮件、字符串主题、字符串正文){
试一试{
fromEmail=“abc@xyz.com”;
内容主题内容=新内容(主题);
Destination Destination=new Destination()。带ToAddress(新字符串[]{”cde@gmail.com" });
Content htmlContent=new Content().withData(“你好,希望你今天过得愉快。”);
Body msgBody=new Body().withHtml(htmlContent);
//创建具有指定主题和正文的邮件。
Message Message=new Message().withSubject(subjectContent).withBody(msgBody);
SendEmailRequest=新的SendEmailRequest()
.withSource(来自电子邮件)
.withDestination(目的地)
.withMessage(消息);
SendRawEmailRequest SendRawEmailRequest=新建SendRawEmailRequest()
.withSource(来自电子邮件)
.withDestinations(destination.getbccadirements())
.withRawMessage(新的RawMessage());
AWSCredentials credentials=新的基本凭证(用户名、密码);
AmazonSimpleEmailServiceClient sesClient=新AmazonSimpleEmailServiceClient(凭据);
//ListVerifiedEmailAddressesResult verifiedEmails=
//sesClient.listVerifiedEmailAddresses();
SendRawEmailResult=sesClient.sendRawEmail(sendRawEmailRequest);
System.out.println(结果+发送电子邮件);
}捕获(例外e){
logger.error(“捕获到MessaginException,这意味着存在错误”
+“向亚马逊电子邮件服务发送邮件时出现问题,请检查”
+“堆栈跟踪以获取更多信息。{}”+e.getMessage());
e、 printStackTrace();
}
}
我得到下面提到的错误
com.amazonaws.AmazonServiceException:我们计算的请求签名与您提供的签名不匹配。检查您的AWS秘密访问密钥和签名方法。有关详细信息,请参阅维修文档。
此请求的规范字符串应为
“岗位
/
主持人:email.us-east-1.amazonaws.com
用户代理:aws sdk java/1.9.0 Linux/3.19.0-25-通用java\u热点(TM)\u 64位\u服务器\u VM/25.66-b17/1.8.0\u 66
x-amz-日期:20160223T062544Z
宿主;用户代理;x-amz-date
4c1f25e3dcf887bd49756ddd01c5e923cf49f2affa73adfc7059d00140032edf'
(服务:AmazonSimpleEmailService;状态代码:403;错误代码:SignatureDesNotMatch
您提供的凭据不正确。您提供的是IAM用户名和密码。相反,您必须提供
access\u key\u id
和access\u key\u id
AWSCredentials credentials = new BasicAWSCredentials(access_key_id, secret_access_key)
请参阅:这是使用Amazon SES发送电子邮件的示例代码。 最初,当您创建亚马逊SES帐户时,该帐户将处于沙箱模式,仅允许您发送200封电子邮件。要切换到生产模式,您需要“请求”扩展电子邮件限制。请查看文档 先决条件: 您需要激活Amazon SES帐户。 如果您当前处于沙盒模式,请验证电子邮件地址(收件人和发件人)或域。 在导航栏的“SMTP设置”下,您可以生成SMTP凭据。 这将包括smtp用户名和密码。您也可以下载包含此详细信息的csv文件 使用Java发送电子邮件:
public class AmazonSESExample {
static final String FROM = "your from email address";
static final String FROMNAME = "From name";
// Replace recipient@example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
static final String TO = "receiver email address";
// Replace smtp_username with your Amazon SES SMTP user name.
static final String SMTP_USERNAME = "username generated under smtp settings";
// Replace smtp_password with your Amazon SES SMTP password.
static final String SMTP_PASSWORD = "password generated under smtp settings";
// Amazon SES SMTP host name. This example uses the US West (Oregon) region.
// See https://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html#region-endpoints
// for more information.
static final String HOST = "email-smtp.us-east-1.amazonaws.com";
// The port you will connect to on the Amazon SES SMTP endpoint.
static final int PORT = 25;
static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)";
static final String BODY = String.join(
System.getProperty("line.separator"),
"<h1>Amazon SES SMTP Email Test</h1>",
"<p>This email was sent with Amazon SES using the ",
"<a href='https://github.com/javaee/javamail'>Javamail Package</a>",
" for <a href='https://www.java.com'>Java</a>."
);
public static void main(String[] args) throws Exception {
// Create a Properties object to contain connection configuration information.
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
// Create a Session object to represent a mail session with the specified properties.
Session session = Session.getDefaultInstance(props);
// Create a message with the specified information.
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(FROM, FROMNAME));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
msg.setSubject(SUBJECT);
msg.setContent(BODY, "text/html");
// Create a transport.
Transport transport = session.getTransport();
// Send the message.
try {
System.out.println("Sending...");
// Connect to Amazon SES using the SMTP username and password you specified above.
transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
// Send the email.
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("Email sent!");
} catch (Exception ex) {
System.out.println("The email was not sent.");
System.out.println("Error message: " + ex.getMessage());
} finally {
// Close and terminate the connection.
transport.close();
}
}
public类亚马逊示例{
静态最终字符串FROM=“您的发件人电子邮件地址”;
静态最终字符串FROMNAME=“FROMNAME”;
//替换recipient@example.com带有“收件人”地址。如果您的帐户
//仍在沙箱中,必须验证此地址。
静态最终字符串至=“收件人电子邮件地址”;
//将smtp_用户名替换为您的Amazon SES smtp用户名。
静态最终字符串SMTP_USERNAME=“在SMTP设置下生成的用户名”;
//将smtp_密码替换为您的Amazon SES smtp密码。
静态最终字符串SMTP_PASSWORD=“在SMTP设置下生成的密码”;
//Amazon SES SMTP主机名。此示例使用美国西部(俄勒冈州)地区。
//看https://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html#region-端点
//了解更多信息。
静态最终字符串HOST=“email smtp.us-east-1.amazonaws.com”;
//您将在Amazon SES SMTP端点上连接到的端口。
静态最终int端口=25;
静态最终字符串SUBJECT=“Amazon SES测试(使用Java访问SMTP接口)”;
静态最终字符串正文=String.join(
System.getProperty(“line.separator”),
“亚马逊SES SMTP电子邮件测试”,
“此电子邮件由Amazon SES使用“,
"",
“因为。”
);
公共静态void main(字符串[]args)引发异常{
//创建属性对象以包含连接配置信息。
Properties props=System.getProperties();
props.put(“mail.transport.protocol”、“smtp”);
props.put(“mail.smtp.port”,port);
props.put(“mail.smtp.starttls.enable”、“true”);
props.put(“mail.smtp.auth”,“true”);
//创建会话对象以表示具有指定属性的邮件会话。
Session Session=Session.getDefaultInstance(props);
//使用指定的信息创建消息。
MimeMessage msg=新MimeMessage(会话);
msg.setFrom(新的InternetAddress(FROM,FROMNAME));
msg.setRecipient(Message.RecipientType.TO,新的InternetAddress(TO));
msg.setSubject(主题);
msg.setContent(正文,“text/html”);
//创建一个传输。
传输=会话。getTransport();
//发送消息。
试一试{
System.out.println(“发送…”);
//使用上面指定的SMTP用户名和密码连接到Amazon SES。