2010-04-29 106 views
1

我在eclipse中編寫這個應用程序我添加了所有的jar文件。我正在粘貼代碼和錯誤。請讓我知道我應該如何更改以正確運行應用程序。這段代碼有什麼問題。運行時錯誤

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


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.starttls.enable","true"); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.setProperty("mail.smtp.port", "25"); 

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

錯誤:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 13sm646598ewy.13 

    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:54) 
    at SendMail.main(SendMail.java:10) 
+0

要發送電子郵件到Gmail,從Java:http://stackoverflow.com/questions/46663/how-do-you-send-email-from -a-java-app-using-gmail – leonbloy 2010-04-29 17:58:44

回答

0

你試圖在一個期望加密連接(TLS在這種情況下)的連接發送郵件。

這個太陽論壇有幾個解決方案,你可以嘗試: http://forums.sun.com/thread.jspa?threadID=617974

+0

他是,但他也給了javamail屬性,應該在連接之後提升與TLS的連接。 – Geoff 2010-04-29 18:03:43

-1

用來爲JavaMail GMail的SMTP我用以下屬性:

mail.smtp.host="smtp.gmail.com" 
mail.smtp.port="587" 
mail.smtp.auth="true" 
mail.smtp.starttls.enable="true" 

我不知道,Gmail接受郵件端口25.也許這是你問題的根源。

很明顯,您仍然需要執行驗證,通常通過mail.smtp.usermail.smtp.password

+0

通過更改端口我得到這個錯誤 com.sun.mail.smtp.SMTPSendFailedException:530-5.5.1身份驗證必需。瞭解更多信息: 530 5.5.1 http://mail.google.com/support/bin/answer.py?answer=14257 i35sm2744098gve.26 – user327136 2010-04-29 18:06:46

+0

我已經讚賞我的回答,顯然GMail不是開放中繼,您可以需要執行認證。 – Geoff 2010-04-29 18:08:25

1

實際上,您還必須在使用smtps時設置smtps屬性。

只需設置郵件的屬性包括:

mail.smtps.host 
mail.smtps.auth