2011-07-19 222 views
0

我正在編寫一個簡單的Java程序來發送郵件,但出現錯誤。下面的代碼:使用SMTP發送電子郵件

package mypackage; 
import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 

// Send a simple, single part, text/plain e-mail 
public class Sendmail { 

    public static void main(String[] args) { 

     // SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!! 
     String to = "[email protected]"; 
     String from = "[email protected]"; 
     // SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!! 
     String host = "smtp.gmail.com"; 

     // Create properties, get Session 
     Properties props = new Properties(); 

     // If using static Transport.send(), 
     // need to specify which host to send it to 
     props.put("mail.smtp.host", host); 
     // To see what is going on behind the scene 
     props.put("mail.debug", "true"); 
     Session session = Session.getInstance(props); 

     try { 
      // Instantiatee a message 
      Message msg = new MimeMessage(session); 
      System.out.println("in try blk"); 
      //Set message attributes 
      msg.setFrom(new InternetAddress(from)); 
      InternetAddress[] address = {new InternetAddress(to)}; 
      msg.setRecipients(Message.RecipientType.TO, address); 
      msg.setSubject("Test E-Mail through Java"); 
      msg.setSentDate(new Date()); 

      // Set message content 
      msg.setText("This is a test of sending a " + 
         "plain text e-mail through Java.\n" + 
        "Here is line 2."); 

      //Send the message 
      Transport.send(msg); 
     } 
     catch (MessagingException mex) { 
      // Prints all nested (chained) exceptions as well 
      System.out.println("in catch block"); 
      mex.printStackTrace(); 
     } 
    } 
}//End of class 

這裏的錯誤:

221 2.0.0 closing connection d1sm3094152pbj.24 
in catch block 
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. d1sm3094152pbj.24 
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057) 
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580) 
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097) 
at javax.mail.Transport.send0(Transport.java:195) 
at javax.mail.Transport.send(Transport.java:124) 
at mypackage.Sendmail.main(Sendmail.java:48) 
+0

PLZ告訴別人的幫助,將不勝感激 – Atul

+0

您的郵件服務器(谷歌)需要一個TLS連接,你的代碼沒有做。 –

+0

以及如何做TLS連接... – Atul

回答

1

簡單google search爲 「JavaMail的Gmail的」 將產生怎樣使用JavaMail使用Gmail,就像this one很多例子。 Google還有一個configuration page列出了您需要的連接配置,因此您可以仔細檢查設置。

+0

以及謝謝你,鏈接真的很有用:) – Atul

2

的Gmail只支持通過SSL/TLS SMTP。

添加

props.put("mail.smtp.socketFactory.port", "465"); 
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
props.put("mail.smtp.socketFactory.fallback", "false"); 

您還需要登錄到服務器:

props.put("mail.smtp.host", "smtp.gmail.com"); 

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { 
    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication("username", "password"); 
    } 
}); 
+0

我已經添加了這些,即使它顯示相同的錯誤.. :( – Atul