2015-11-24 35 views
3

我創建了一個使用STARTLS發送電子郵件的示例。在不同的域中託管的3個不同的Windows版本上運行它,一切正常。Javamail和STARTTLS不能在不同平臺上工作

bizzare的事情是,當我在Ubuntu服務器14.02 LTS上運行它時,它不起作用。沒有防火牆阻塞,並且java應用程序二進制文件是相同的。

這是代碼:

// 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 = 25; 

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

    // 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.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.starttls.required", "true"); 
    props.put("mail.smtp.ssl.enable", "false"); 
    props.put("mail.debug", "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.setReplyTo(InternetAddress.parse("[email protected]")); 
    msg.setFrom(new InternetAddress(FROM)); 
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO)); 
    msg.setSubject(SUBJECT); 
    msg.setContent(BODY, "html/plain"); 

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

    // Send the message. 
    try { 
     System.out.println("Attempting to send an email through the Amazon SES SMTP interface..."); 

     // Connect to Amazon SES using the SMTP username and password you specified above. 
     transport.connect(HOST, PORT, 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(); 
    } 
} 

這是在Windows環境中的JavaMail日誌(什麼是工作):

DEBUG: JavaMail version 1.5.4 
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]} 
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]} 
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] 
Attempting to send an email through the Amazon SES SMTP interface... 
DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: trying to connect to host "email-smtp.us-west-2.amazonaws.com", port 25, isSSL false 
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-1207632523 H3nxFSQJ7ktEpHBHuT38 
DEBUG SMTP: connected to host "email-smtp.us-west-2.amazonaws.com", port: 25 

EHLO roberton 
250-email-smtp.amazonaws.com 
250-8BITMIME 
250-SIZE 10485760 
250-STARTTLS 
250-AUTH PLAIN LOGIN 
250 Ok 
DEBUG SMTP: Found extension "8BITMIME", arg "" 
DEBUG SMTP: Found extension "SIZE", arg "10485760" 
DEBUG SMTP: Found extension "STARTTLS", arg "" 
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN" 
DEBUG SMTP: Found extension "Ok", arg "" 
STARTTLS 
220 Ready to start TLS 
EHLO xxxxxxxxxxxxxxxx 
250-email-smtp.amazonaws.com 
250-8BITMIME 
250-SIZE 10485760 
250-STARTTLS 
250-AUTH PLAIN LOGIN 
250 Ok 
DEBUG SMTP: Found extension "8BITMIME", arg "" 
DEBUG SMTP: Found extension "SIZE", arg "10485760" 
DEBUG SMTP: Found extension "STARTTLS", arg "" 
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 
DEBUG SMTP: use8bit false 
MAIL FROM:<[email protected]> 
250 Ok 
RCPT TO:<xxxxxxxxxxxxxxxxxxxxx> 
250 Ok 
DEBUG SMTP: Verified Addresses 
DEBUG SMTP: xxxxxxxxxxxxxxx 
DATA 
354 End data with <CR><LF>.<CR><LF> 
From: xxxxxxxxxxxxxx 
Reply-To: xxxxxxxxxxxxx 
To: xxxxxxxxxxxxxxxxxxx 
Message-ID: <[email protected]> 
Subject: Amazon SES test (SMTP interface accessed using Java) 
MIME-Version: 1.0 
Content-Type: html/plain 
Content-Transfer-Encoding: 7bit 

This email was sent through the Amazon SES SMTP interface by using Java. 
. 
250 Ok 0000015138f203b5-6fcd5424-60c3-43eb-9542-83699cf36c46-000000 
DEBUG SMTP: message successfully delivered to mail server 
Email sent! 
QUIT 
221 Bye 

這是從Ubuntu(環境什麼ISN日誌「T工作):

DEBUG: JavaMail version 1.5.4 
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]} 
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]} 
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] 
Attempting to send an email through the Amazon SES SMTP interface... 
DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: trying to connect to host "email-smtp.us-west-2.amazonaws.com", port 25, isSSL false 
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-1207632523 tIUsmWGoY4gXLCMWdUpi 
DEBUG SMTP: connected to host "email-smtp.us-west-2.amazonaws.com", port: 25 

EHLO xxxxxxxxx 
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: STARTTLS required but not supported 
The email was not sent. 
Error message: STARTTLS is required but host does not support STARTTLS 

這是調用堆棧錯誤:

javax.mail.MessagingException: STARTTLS is required but host does not support STARTTLS 
     at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:719) 
     at javax.mail.Service.connect(Service.java:364) 
     at SESEmail.main(SESEmail.java:64) 

Java版本:1.8.0_60-B27

爲什麼發生這種情況?

+0

你在windows機器上使用什麼版本的java,哪些版本在Ubuntu上? – MaVRoSCy

+0

並請提供與確切的異常堆棧跟蹤一樣的... – MaVRoSCy

+0

在Ubuntu會話中返回EHLO時看到:該服務器不支持STARTTLS(當時,來自該源?)。這是可重複的嗎? STARTTLS從該服務器不可見?你應該嘗試在25端口上telnetting email-smtp.us-west-2.amazonaws.com並自己啓動EHLO主機名稱 – Jan

回答

0

我使用的是AWS SES http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-smtp-java.html的官方示例,並且此人使用端口smtp 25作爲TLS。

由於某些原因,此端口(25)在我的某些環境中不適用於TLS。

經過一番研究,我發現這個http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-connect.html並試圖使用默認的TLS端口587,現在它可以在我的兩個環境(linux和windows)中工作。

我認爲這是來自AWS SES服務器的問題/行爲,而不是Java Mail問題。我將嘗試向AWS團隊報告此問題。

相關問題