2014-02-27 97 views
1

http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-java.html中有關於如何通過AWS-SES發送電子郵件的說明。它指的是access_key和secret_key。但我擁有的是我在門戶上生成的SMTP用戶名和SMTP密碼。AWS上的憑證SES

目前我的代碼如下:

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

爲AmazonSimpleEmailServiceClient的構造函數AWSCredentials但不是SMTP憑證。有關如何使用SMTP憑據的任何想法?

回答

0

使用JavaMail作爲Amazon SES SMTP的傳輸。 Amazon SES文檔中還提供了使用SMTP端點的Instructions and sample code

如果您打算通過Amazon SES API發送電子郵件,請使用SDK。

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