2010-04-27 129 views
0

以下代碼會導致錯誤。請幫助我瞭解什麼是錯的。JavaMail:無法連接到SMTP服務器

import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 
public class SendMail 
{ 
    public static void main(String [] args)throws MessagingException 
    { 
    SendMail sm=new SendMail(); 
    sm.postMail(new String[]{"[email protected]"},"hi","hello","[email protected]"); 
    } 

public void postMail(String recipients[ ], String subject, String message , String from) throws MessagingException 
{ 
    boolean debug = false; 

    //Set the host smtp address 
    Properties props = new Properties(); 
    props.put("mail.smtp.host", "webmail.emailmyname.com"); 

    // create some properties and get the default Session 
    Session session = Session.getDefaultInstance(props, null); 
    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); 
} 
} 

Exception: 
<pre> 
com.sun.mail.smtp.SMTPSendFailedException: 450 smtpout04.dca.untd.com Authentication required 

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829) 
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886) 
    at javax.mail.Transport.send0(Transport.java:191) 
    at javax.mail.Transport.send(Transport.java:120) 
    at SendMail.postMail(SendMail.java:52) 
    at SendMail.main(SendMail.java:10) 

+0

@javacode,請不要轉貼同樣的問題,如果你有什麼要補充的,請更新您原來的問題。展示活動將在網站上提出您的問題,並將獲得更多觀衆。 – 2010-04-27 19:39:41

+0

可能重複的http:// stackoverflow。com/questions/2724053/run-time-error-whats-wrong – 2010-04-27 19:40:31

+1

BAH!沒有編輯! – 2010-04-27 19:45:04

回答

0

需要注意的是很多,許多ISP阻止他們的網絡之外的服務器訪問外部端口25。 ISP會迫使你使用他們的SMTP服務器。

如果您獲得「需要驗證」,您必須先輸入您的用戶名和密碼,並至少發出一個請求,例如檢查新郵件。儘管SMTP不需要發送電子郵件的用戶名和密碼,但許多SMTP服務器仍然通過讓您登錄並通過POP或IMAP檢查郵件來實現此目的,然後才能發送郵件。

+0

我可以有代碼嗎? – user327136 2010-04-27 19:53:16

+0

我應該在哪裏指定用戶名和密碼? – user327136 2010-04-27 19:54:54

+0

這是通過POP或IMAP完成的。 – 2010-04-27 19:56:30

1

異常消息中的「需要身份驗證」表明目標SMTP服務器要求您登錄(可能通過TLS或SSL)。直到幾年前,這在SMTP服務器上並不常見(這是一種反垃圾郵件措施),因此很容易忽視。

authenticate with JavaMail

使用SMTP認證連接到SMTP服務器時,您需要使用用戶名和密碼設置mail.smtp.auth屬性(見下文)或提供SMTP傳輸。爲此,您可以使用下列方法之一:

  • 創建郵件會話時提供一個Authenticator對象,並提供身份驗證回調過程中的用戶名和密碼信息。

    注意,mail.smtp.user屬性可以設置以提供回調默認的用戶名,但密碼仍然需要明確地提供。

    這種方法可以讓你使用靜態交通運輸發送方法來發送消息。

  • 呼叫傳輸用戶名和密碼參數明確連接方法。

該方法要求您顯式管理傳輸對象並使用傳輸sendMessage方法發送消息。 transport.java演示程序演示瞭如何管理傳輸對象。以下是大致相當於靜態交通發送方法,但提供所需的用戶名和密碼:

運輸TR = session.getTransport(「SMTP」);

tr.connect(smtphost,username,password);

msg.saveChanges(); //不要忘記這個

tr.sendMessage(味精,msg.getAllRecipients());

tr.close();

+0

如何登錄smtp服務器? – user327136 2010-04-27 19:58:09

+0

我搜索了Javadoc並粘貼了一個報價。 – 2010-04-27 22:37:27

+0

您的服務器未使用「SMTP驗證」。如果使用「SMTP身份驗證」,您將得到530響應,而不是450響應。如果響應爲450,則服務器很可能使用「POP before SMTP」。發送郵件之前,您需要通過POP或IMAP進行連接。 – 2010-04-28 14:25:37

相關問題