【问题标题】:Credential on AWS SESAWS SES 上的凭证
【发布时间】:2014-03-30 15:12:03
【问题描述】:

http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-java.html 中有关于如何通过 AWS-SES 发送电子邮件的说明。它指的是access_key 和secret_key。但我拥有的是我在门户网站上生成的 SMTP 用户名和 SMTP 密码。

目前我的代码如下:

AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(credentials);
client.sendEmail(request);

AmazonSimpleEmailServiceClient 的构造函数采用 AWSCredentials 但不采用 smtp 凭证。关于如何使用 SMTP 凭据的任何想法?

【问题讨论】:

    标签: java email amazon-web-services amazon-ses


    【解决方案1】:

    使用 JavaMail 作为 Amazon SES SMTP 的传输。 Amazon SES 文档中还提供了用于使用 SMTP 终端节点的Instructions and sample code

    如果您打算通过 Amazon SES API 发送电子邮件,请使用 SDK。

    【讨论】:

      【解决方案2】:
      You can use below code to send mail through SMTP 
      
       InternetAddress[] parse = InternetAddress.parse(toAddess , true);
      
              Properties props = System.getProperties();
              //Add properties
              props.put("mail.transport.protocol", "smtps");
              props.put("mail.smtp.port", port);
              props.put("mail.smtp.auth", "true");
              props.put("mail.smtp.starttls.enable", "true");
              props.put("mail.smtp.starttls.required", "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(fromemail));
      
      
              msg.setRecipients(javax.mail.Message.RecipientType.TO,  parse);
      
      
              msg.setSubject(subject);
              msg.setContent(body,"text/html");
      
              // Create a transport.
              Transport transport = session.getTransport();
      
              // Send the message.
              try
              {
                  logger.info("Attempting to send an email through the Amazon SES SMTP interface to "+toAddess);
      
                  // Connect to Amazon SES using the SMTP username and password specified above.
                  transport.connect(smtpHost, port, smtpuserName, smtpPassword);
      
      
                  // Send the email.
                  transport.sendMessage(msg, msg.getAllRecipients());
                 logger.info("MessageID"+ msg.getMessageID());
      
                  logger.info("Email sent!");
                  return msg.getMessageID();
              }
              catch (Exception ex) {
                  logger.error("The email was not sent. Error message: " + ex.getMessage());
              }
              finally
              {
                  // Close and terminate the connection.
                  transport.close();
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-09
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 2018-11-22
        • 1970-01-01
        相关资源
        最近更新 更多