当前位置: 首页 > 知识库问答 >
问题:

无法在Java中使用Amazon SES发送电子邮件

穆德海
2023-03-14

我使用下面提到的代码发送电子邮件。

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

我得到下面提到的错误。

通用域名格式。亚马逊。AmazonServiceException:我们计算的请求签名与您提供的签名不匹配。检查您的AWS秘密访问密钥和签名方法。有关详细信息,请参阅维修文档。此请求的规范字符串应为“POST”/

主持人:电子邮件。美国东部1号。亚马逊。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_66 x-amz-日期:20160223T062544Z

主办用户代理;x-amz-date 4c1f25e3dcf887bd49756ddd01c5e923cf49f2affa73adfc7059d00140032edf'

(服务:AmazonSimpleEmailService;状态代码:403;错误代码:SignatureDesNotMatch;

共有2个答案

拓拔俊艾
2023-03-14

您提供的凭据不正确。您正在为IAM提供用户名和密码。相反,您必须提供access\u key\u idaccess\u key\u id

AWSCredentials credentials = new BasicAWSCredentials(access_key_id, secret_access_key)

请参阅:在AWS SDK for Java中提供AWS凭据

越嘉茂
2023-03-14

这是使用AmazonSES发送电子邮件的示例代码。最初,当您创建一个Amazon 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();
    }
}

}

 类似资料:
  • responseerror:禁止在d:\node-projects\ecom\node_modules@sendgrid\client\src\类\client.js:146:29在processTicksAndRejections(internal/process/task_queues.js:93:5){ 代码:403,response:{headers:{server:'nginx',dat

  • 我正在尝试使用CodeIgniter的电子邮件库发送电子邮件。这是我写的代码。 错误:这是我得到的错误。 遇到以下SMTP错误:0php_network_getaddresses:getaddrinfo失败:名称或服务未知无法发送数据:AUTH LOGIN发送AUTH LOGIN命令失败。错误:无法发送数据:邮件从:从:遇到以下SMTP错误:无法发送数据:RCPT TO:到:遇到以下SMTP错误:

  • 问题内容: 我正在尝试使用Java发送电子邮件: 我收到错误消息: 此代码可以发送电子邮件吗? 问题答案: 户名+密码不再是推荐的解决方案。这是因为 我尝试了此操作,Gmail向该代码中用作用户名的电子邮件发送了一封电子邮件,其中说我们最近阻止了你的Google帐户的登录尝试,并将我定向到此支持页面:support.google.com/accounts/answer/6010255因此它看起来很

  • 我得到下面提到的错误。我将mail.jar、activation.jar、mailapi.jar、common-lang3.jar复制到/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext

  • 我正在尝试用Laravel 5.2发送电子邮件。这是我第一次在拉威尔发电子邮件。但它抛出了这个错误 这是我发送电子邮件的代码: 这是我在env文件中的电子邮件设置 我的欢迎视图只有一条消息“Hello world” 我已经在Gmail设置中为我的电子邮件配置了不太安全的应用程序设置。那么,我的代码有什么问题,为什么会抛出那个错误?

  • 我的: 但是邮件不会发送到该地址,在控制台中,当我单击时,正在打印,但它不会发送任何电子邮件。 我用的是Django 1.3.7和Python 2.6。 不知道是版本的问题还是什么逻辑问题。