2011-04-12 66 views
1

我想使用javamail api向自己發送電子郵件。我已經正確地跟蹤了我在網上找到的代碼,但似乎無法使其正常工作。我得到的錯誤是:使用javamail發送郵件時出現問題

Exception in thread "main" javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client 

這是否意味着我的電腦不支持身份驗證?

public class Emails 
{ 
    public static void main(String[] args) throws MessagingException 
    { 
     Emails e = new Emails(); 
     e.sendMail(); 
    } 

    public void sendMail() throws MessagingException 
    { 
     boolean debug = false; 
     String subject = "Testing"; 
     String message = "Testing Message"; 
     String from = "[email protected]"; 
     String[] recipients = {"[email protected]"}; 

     // create some properties and get the default Session 
     Session session = getSession(); 
     session.setDebug(debug); 

     // create a message 
     Message msg = new MimeMessage(session); 

     // set the from and to address 
     InternetAddress addressFrom = new InternetAddress(from); 
     msg.setFrom(addressFrom); 

     InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
     for (int i = 0; i < recipients.length; i++) 
     { 
      addressTo[i] = new InternetAddress(recipients[i]); 
     } 
     msg.setRecipients(Message.RecipientType.TO, addressTo); 

     // Optional : You can also set your custom headers in the Email if you Want 
     msg.addHeader("MyHeaderName", "myHeaderValue"); 

     // Setting the Subject and Content Type 
     msg.setSubject(subject); 
     msg.setContent(message, "text/plain"); 
     Transport.send(msg); 
    } 

    private Session getSession() 
    { 
     Authenticator authenticator = new Authenticator(); 

     Properties properties = new Properties(); 
     properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName()); 
     properties.setProperty("mail.smtp.auth", "true"); 

     properties.setProperty("mail.smtp.host", "smtp.examplehost.com"); 
     properties.setProperty("mail.smtp.port", "25"); 

     return Session.getInstance(properties, authenticator); 
    } 

    private class Authenticator extends javax.mail.Authenticator 
    { 
     private PasswordAuthentication authentication; 

     public Authenticator() 
     { 
      String username = "myusername"; 
      String password = "mypassword"; 
      authentication = new PasswordAuthentication(username, password); 
     } 

     protected PasswordAuthentication getPasswordAuthentication() 
     { 
      return authentication; 
     } 
    } 
} 

當我發送電子郵件從該服務器,我有什麼做的是,通過ssh登錄到服務器(login.server.com等),那麼我是否可以將郵件服務器的郵件(smtp.server .com等)。我要模仿這種在Java

解決:使用SMTPSSLTransport類

回答

-3

SMTPSSLTransport解決了這個問題

+0

你可以顯示你的代碼分辨率? – 2012-02-20 22:42:29

+0

我不確定「SMTPSSLTransport解決了這個問題」的含義。你如何指定這個類(而不是其他類)?我們遇到了同樣的問題,並且在我們使用SSL連接到Gmail時會起作用。但連接到MS Exchange服務器,它不。 – fool4jesus 2012-02-21 15:52:21

+1

@JPC剛剛通過谷歌搜索找到了你最初得到的同樣的錯誤。當你找到解決方案時,你應該提供更多的細節。如果您可以尋求幫助,您也應該準備好提供幫助。 – Greg 2012-03-06 19:38:15

1

確保您有一個SMTP服務器(smtp.examplehost.com)實際發送電子郵件。 確保您的smtp服務器允許「[email protected]」發送電子郵件。

這些是由標準smtp服務器實施的一些安全檢查。

+1

所以通常當我想從這個發送電子郵件服務器,我必須通過SSH登錄(login.server.com)。一旦我登錄,即將離任的服務器是一些smtp.server.com。我想我需要模仿java – JPC 2011-04-12 18:33:59

1

這可能是你敲錯了端口。我想你正在使用IMAP協議:對於IMAP通過SSL你必須連接到端口993

+0

爲了從服務器發送電子郵件,我必須通過SSH(login.server.com)登錄。一旦我登錄,發出的服務器是端口25上的一些smtp.server.com。我怎樣才能模仿在java中? – JPC 2011-04-12 18:46:51

2

我有同樣的問題,這一次沒有找到計算器答案。 @ JPC的答案提供了一個(非常小的)線索。

從javamail 1.4.3(?參見參考資料)開始,javamail將拒絕通過未加密的通道發送純文本憑據。因此啓用TLS傳輸是一種解決方案。另一種解決方案是嘗試另一種身份驗證方法(如果受服務器支持,如NTLM或SASL)。另一個解決方案是恢復到JavaMail 1.4.1,這在發送憑證時沒有問題。這樣做的好處是您的代碼不必更改。問題中的代碼可以毫無問題地工作。缺點是顯然你發送的憑證是明文的,但如果你的Exchange管理員允許的話......在我的情況下,郵件服務器不支持TLS,所以我別無選擇。

參考文獻: