2010-06-03 44 views
2

即時嘗試發送郵件使用javamail api使用下面的代碼:當我編譯類文件即時獲取下面的錯誤,說'必須首先發出starttls命令'我已經提到錯誤下面。也getProvider()函數錯誤,我認爲這樣...我不知道錯誤的意思。javamail錯誤:必須首先發出starttls命令

import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.mail.event.*; 
import javax.mail.Authenticator; 
import java.net.*; 
import java.util.Properties; 
public class mailexample 
    { 
    public static void main (String args[]) throws Exception { 

    String from = args[0]; 
    String to = args[1]; 
try 
{ 
Properties props=new Properties(); 
props.put("mail.transport.protocol", "smtp"); 
props.put("mail.smtp.host","smtp.gmail.com"); 
props.put("mail.smtp.port", "25"); 
props.put("mail.smtp.auth", "true"); 
javax.mail.Authenticator authenticator = new javax.mail.Authenticator() 
    { 
    protected javax.mail.PasswordAuthentication getPasswordAuthentication() 
     { 
     return new javax.mail.PasswordAuthentication("[email protected]", "pass"); 
    } 
}; 
Session sess=Session.getDefaultInstance(props,authenticator); 
sess.setDebug (true); 
Transport transport =sess.getTransport ("smtp"); 
Message msg=new MimeMessage(sess); 
msg.setFrom(new InternetAddress(from)); 
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
msg.setSubject("Hello JavaMail"); 
msg.setText("Welcome to JavaMail"); 
transport.connect(); 
transport.send(msg); 

} 
catch(Exception e) 
{ 
System.out.println("err"+e); 
} 
} 
} 

錯誤:

 
C:\Users\bobby\Desktop>java mailexample [email protected] [email protected] 
com 

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s 
mtp.SMTPTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: useEhlo true, useAuth true 

DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25 

DEBUG SMTP RCVD: 220 mx.google.com ESMTP q10sm12956046rvp.20 

DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25 

DEBUG SMTP SENT: EHLO bobby-PC 
DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29] 
250-SIZE 35651584 
250-8BITMIME 
250-STARTTLS 
250 ENHANCEDSTATUSCODES 


DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s 
mtp.SMTPTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: useEhlo true, useAuth true 

DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25 

DEBUG SMTP RCVD: 220 mx.google.com ESMTP l29sm12930755rvb.16 

DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25 

DEBUG SMTP SENT: EHLO bobby-PC 
DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29] 
250-SIZE 35651584 
250-8BITMIME 
250-STARTTLS 
250 ENHANCEDSTATUSCODES 

DEBUG SMTP SENT: MAIL FROM: 
DEBUG SMTP RCVD: 530 5.7.0 Must issue a STARTTLS command first. l29sm12930755rvb 
.16 

DEBUG SMTP SENT: QUIT 
errjavax.mail.SendFailedException: Sending failed; 
    nested exception is: 
     javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command f 
irst. l29sm12930755rvb.16 
+0

@bobby也看到我的更新,你剛纔的問題http://stackoverflow.com/questions/2963625/help-with-javamail-api – JoseK 2010-06-03 08:43:30

+0

@bobby - 你嘗試從馬修鏈路上的所有步驟富蘭琛? – JoseK 2010-06-03 10:24:35

回答

3

也許,你需要把你props.put("mail.smtp.starttls.enable","true");驗證之前。

+0

hhey即時通訊仍然有同樣的問題...同樣的錯誤.. – simplyblue 2010-06-03 09:44:32

0

此代碼似乎可以正常使用Gmail SMTP服務器發送電子郵件。注 - 這沒有附件。

(來源:從實施例修改於https://forums.oracle.com/forums/thread.jspa?threadID=1587188

package org.ssb.mail; 

import java.util.Date; 
import java.util.Properties; 

import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class MailClient { 

/** 
* Entry method 
* 
* @param args 
*   String[] 
*/ 
public static void main(String[] args) { 

    MailClient client = new MailClient(); 

    try { 
     client.sendMail(); 
    } catch (AddressException ae) { 
     ae.printStackTrace(); 
    } catch (MessagingException me) { 
     me.printStackTrace(); 
    } 

} 

/** 
* Sends an email 
* 
* @param none 
*/ 
private void sendMail() throws AddressException, MessagingException { 

    // Get a Properties object 
    Properties props = System.getProperties(); 

    // ******************** FOR PROXY ****************** 

    // props.setProperty("proxySet","true"); 
    // props.setProperty("socksProxyHost","9.10.11.12"); 
    // props.setProperty("socksProxyPort","80"); 
    // props.setProperty("socksProxyVersion","5"); 

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

    // ******************** FOR SSL ****************** 
    //final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 
    //props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); 
    //props.setProperty("mail.smtp.socketFactory.fallback", "false"); 
    //props.setProperty("mail.smtp.port", "465"); 
    //props.setProperty("mail.smtp.socketFactory.port", "465"); 

    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.debug", "true"); 
    props.put("mail.store.protocol", "pop3"); 
    props.put("mail.transport.protocol", "smtp"); 
    final String username = "sender-username"; 
    final String password = "sender-password"; 
    Session session = Session.getDefaultInstance(props, 
      new Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(username, password); 
       } 
      }); 

    // -- Create a new message -- 
    Message msg = new MimeMessage(session); 
    msg.setFrom(new InternetAddress("[email protected]")); 
    msg.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse("[email protected]", false)); 
    msg.setSubject("Hello"); 
    msg.setSentDate(new Date()); 

    // **************** Without Attachments ****************** 
    msg.setText("How are you"); 


    Transport.send(msg); 
    System.out.println("Message sent."); 

} 

} 
0

像你描述我有同樣的問題。在我的情況下,

Session session = Session.getInstance(props);

更換

Session session = Session.getDefaultInstance(props);

的伎倆。我希望這會有所幫助。

0

這個工作適合我。 設置以下標籤。它會工作。

props.put("mail.smtp.user","username"); 
props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.port", "25"); 
props.put("mail.debug", "true"); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.starttls.enable","true"); 
props.put("mail.smtp.EnableSSL.enable","true"); 

props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
props.setProperty("mail.smtp.socketFactory.fallback", "false"); 
props.setProperty("mail.smtp.port", "465"); 
props.setProperty("mail.smtp.socketFactory.port", "465"); 
相關問題