2015-05-27 91 views
1

我正嘗試使用JavaMail(1.4.7)通過Amazon SMTP發送電子郵件。無法使用SMTP使用JavaMail和Amazon SES發送郵件

這裏是我的代碼

import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class AmazonSESSample { 

     static final String FROM = "[email protected]"; 
     static final String TO = "[email protected]"; 

     static final String BODY = "this is the body"; 
     static final String SUBJECT = "hello test"; 
     static final String USER = "..."; 
     static final String PASS = "..."; 

     // Port we will connect to on the Amazon SES SMTP endpoint. We are choosing port 25 because we will use 
     // STARTTLS to encrypt the connection. 
     static final int PORT = 465; 
     static final String PROTOCOL = "smtps"; 
     static final String HOST = "email-smtp.us-west-2.amazonaws.com"; 

     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", PROTOCOL); 
       props.put("mail.smtps.host", HOST); 
       props.put("mail.smtps.port", PORT); 
       props.put("mail.smtps.user", FROM); 
       props.put("mail.debug","true"); 

       // Set properties indicating that we want to use STARTTLS to encrypt the connection. 
       // The SMTP session will begin on an unencrypted connection, and then the client 
       // will issue a STARTTLS command to upgrade to an encrypted connection. 
//  props.put("mail.smtps.auth", "true"); 
//  props.put("mail.smtp.starttls.enable", "true"); 
//  props.put("mail.smtp.starttls.required", "true"); 
//  props.put("mail.smtps.ssl.enable", "true"); 

       // Create a Session object to represent a mail session with the specified properties. 
       Session session = Session.getDefaultInstance(props); 
       session.setDebug(true); 

       // Create a message with the specified information. 
       MimeMessage msg = new MimeMessage(session); 
       msg.setFrom(new InternetAddress(FROM)); 
       msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO)); 
       msg.setSubject(SUBJECT); 
       msg.setContent(BODY,"text/plain"); 
       //msg.saveChanges(); 

       // Create a transport.   
       Transport transport = session.getTransport("smtps"); 

       // Send the message. 
       try 
       { 
         System.out.println("connecting..."); 

         // Connect to Amazon SES using the SMTP username and password you specified above. 
         transport.connect(HOST, USER, PASS); 
         System.out.println("connected"); 
         // Send the email. 
         System.out.println("sending..."); 
         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();   
       } 
     } 
} 

我得到兩種類型的錯誤零星。

有時我得到 「421超時等待來自客戶端的數據」:

DEBUG: JavaMail version 1.4.7 
DEBUG: URL jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers 
DEBUG: Bad provider entry: 
DEBUG: successfully loaded resource: jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers 
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers 
DEBUG: Tables of loaded providers 
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], com.amazonaws.services.simpleemail.AWSJavaMailTransport=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]} 
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], aws=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]} 
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map 
DEBUG: setDebug: JavaMail version 1.4.7 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle] 
connecting... 
DEBUG SMTP: useEhlo true, useAuth false 
DEBUG SMTP: trying to connect to host "email-smtp.us-west-2.amazonaws.com", port 465, isSSL true 
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-1062959777 MTyV0IkyBqUpkEgbX2Av 
DEBUG SMTP: connected to host "email-smtp.us-west-2.amazonaws.com", port: 465 

EHLO 10.8.1.217 
250-email-smtp.amazonaws.com 
250-8BITMIME 
250-SIZE 10485760 
250-AUTH PLAIN LOGIN 
250 Ok 
DEBUG SMTP: Found extension "8BITMIME", arg "" 
DEBUG SMTP: Found extension "SIZE", arg "10485760" 
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN" 
DEBUG SMTP: Found extension "Ok", arg "" 
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed 
DEBUG SMTP: AUTH LOGIN succeeded 
connected 
sending... 
DEBUG SMTP: use8bit false 
MAIL FROM:<[email protected]> 
250 Ok 
RCPT TO:<[email protected]> 
250 Ok 
DEBUG SMTP: Verified Addresses 
DEBUG SMTP: [email protected] 
DATA 
354 End data with <CR><LF>.<CR><LF> 
From: [email protected] 
To: [email protected] 
Message-ID: <[email protected]> 
Subject: hello test 
MIME-Version: 1.0 
Content-Type: text/plain; charset=us-ascii 
Content-Transfer-Encoding: 7bit 

this is the body 
. 
421 Timeout waiting for data from client. 
DEBUG SMTP: got response code 421, with response: 421 Timeout waiting for data from client. 

RSET 
DEBUG SMTP: EOF: [EOF] 
DEBUG SMTP: MessagingException while sending, THROW: 
com.sun.mail.smtp.SMTPSendFailedException: 421 Timeout waiting for data from client. 

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108) 
    at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1889) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1120) 
    at AmazonSESSample.main(AmazonSESSample.java:68) 
The email was not sent. 
Error message: 421 Timeout waiting for data from client. 

QUIT 
Exception in thread "main" javax.mail.MessagingException: Can't send command to SMTP host; 
    nested exception is: 
    java.net.SocketException: Connection closed by remote host 
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2157) 
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2144) 
    at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1210) 
    at AmazonSESSample.main(AmazonSESSample.java:79) 
Caused by: java.net.SocketException: Connection closed by remote host 
    at sun.security.ssl.SSLSocketImpl.checkWrite(SSLSocketImpl.java:1490) 
    at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:70) 
    at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:128) 
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82) 
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140) 
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2155) 
    ... 3 more 

,有時我得到 「EOF:[EOF]」:

DEBUG: JavaMail version 1.4.7 
DEBUG: URL jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers 
DEBUG: Bad provider entry: 
DEBUG: successfully loaded resource: jar:file:/Users/adavid/.m2/repository/com/amazonaws/aws-java-sdk-ses/1.9.23/aws-java-sdk-ses-1.9.23.jar!/META-INF/javamail.providers 
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers 
DEBUG: Tables of loaded providers 
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], com.amazonaws.services.simpleemail.AWSJavaMailTransport=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]} 
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], aws=javax.mail.Provider[TRANSPORT,aws,com.amazonaws.services.simpleemail.AWSJavaMailTransport,Amazon Web Services LLC]} 
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map 
DEBUG: setDebug: JavaMail version 1.4.7 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle] 
connecting... 
DEBUG SMTP: useEhlo true, useAuth false 
DEBUG SMTP: trying to connect to host "email-smtp.us-west-2.amazonaws.com", port 465, isSSL true 
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-1062893701 2HRiJbNHu9DYCQClBXLu 
DEBUG SMTP: connected to host "email-smtp.us-west-2.amazonaws.com", port: 465 

EHLO 10.8.1.217 
421 Timeout waiting for data from client. 
HELO 10.8.1.217 
DEBUG SMTP: EOF: [EOF] 
The email was not sent. 
Error message: [EOF] 

我失去了一些東西,我不知道什麼...

將不勝感激任何幫助

+0

亞馬遜SMTP可能有非常嚴格的超時要求,以防止客戶端浪費連接。也許你的應用程序或你的虛擬機正在受到調度延遲的困擾,這會阻止它足夠快地響應。您可能需要升級到[當前版本的JavaMail](https://java.net/projects/javamail/pages/Home)。這將允許您使用java.util.logging作爲調試輸出,這將允許您獲取時間戳日誌條目,可能會顯示延遲的位置。 –

回答

2

你可以試試

System.setProperty("line.separator", "\r\n"); 

只設置你的財產,以符合RFC 822

+0

這應該沒有必要。 JavaMail確保在使用SMTP發送時線路以CRLF結束。 –

+0

沒有。它解決了問題! – asafd

+0

重新檢查後,看起來問題只發生在我的私人開發計算機上,這是一臺OSX機器。 在Ubuntu上,它使用或不使用:System.setProperty(「line.separator」,「\ r \ n」); 它也可能是一個網絡問題,因爲Ubuntu測試服務器是一個AWS服務器。 – asafd

1

爲421客戶端expcetion後 - 我有同樣的問題。我通過驗證傳輸是否連接來解決它:

if (!transport.isConnected())//make sure the connection is alive 
     transport.connect(); 
transport.sendMessage(message, message.getAllRecipients()); 
相關問題