2008-12-22 106 views
18

我試圖使用Bill the Lizard's code使用Google Apps發送電子郵件。我收到此錯誤:必須首先發出STARTTLS命令。使用Java和Google Apps發送電子郵件

Exception in thread "main" javax.mail.SendFailedException: Sending failed; 
    nested exception is: 
    javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. f3sm9277120nfh.74 

    at javax.mail.Transport.send0(Transport.java:219) 
    at javax.mail.Transport.send(Transport.java:81) 
    at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:81) 
    at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:44) 

比爾的代碼中包含的下一行,這似乎與錯誤:

props.put("mail.smtp.starttls.enable","true"); 

但是,它並不能幫助。

這些都是我的import語句:

​​

有誰知道這個錯誤?

回答

4

我認爲這與使用SMTPS而不是SMTP進行郵件傳輸有關。這是一個不同的版本,模仿JavaMail FAQ on accessing Gmail。請注意,爲清楚起見,我已經省略了所有更精細的異常處理。

private static void send(
     final String username, 
     final String password, 
     final String recipients, 
     final String subject, 
     final String body) 
     throws Exception 
{ 
    final Session session = Session.getInstance(System.getProperties(), null); 
    final Message msg = new MimeMessage(session); 
    final String senderEmail = username.contains("@") ? username : (username + "@gmail.com"); 
    msg.setFrom(new InternetAddress(senderEmail)); 

    final Address[] recipientAddresses = InternetAddress.parse(recipients); 
    msg.setRecipients(Message.RecipientType.TO, recipientAddresses); 

    msg.setSentDate(new Date()); 
    msg.setSubject(subject); 
    msg.setText(body); 

    final Transport transport = session.getTransport("smtps"); 
    transport.connect(GMAIL_SMTP_HOST, GMAIL_SMTP_PORT, username, password); 
    transport.sendMessage(msg, recipientAddresses); 
    transport.close(); 
} 
+0

我得到第n運行代碼時出現ext異常:線程「main」中出現異常javax.mail.NoSuchProviderException:沒有提供smtps – 2008-12-22 12:59:21

+0

似乎JavaMail資源文件丟失或損壞(請參閱http://java.sun.com/products/javamail/的javadoc /使用javax /郵件/ Session.html)。 mail.jar/META-INF中有文件的默認副本。 – 2008-12-22 13:11:59

16

我發現了這個問題。以前我使用j2ee.jar來導入javax.mail。

我從類路徑中刪除了j2ee.jar,並下載了JavaMail 1.4.1,並將兩個jar文件smtp.jar和mailapi.jar放入我的classpath中。我現在用的SMTPS代替SMTP

Transport transport = session.getTransport("smtps");    

現在比爾蜥蜴的代碼工作。

8

它是java郵件API的版本。 我正面臨這個問題,我只是更新了Java郵件API到1.4.3 它對我來說工作正常!

謝謝!

14

設置以下標籤。它會工作。

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"); 
1

這讓我發瘋,所以只想添加一些對我有用的東西。我必須更新我的JavaMail版本(1.4.5)才能使用 - 不知道以前使用的是哪個版本。

一旦我更新到JavaMail的新版本,下面的代碼爲我工作(可以取消調試線來獲得額外的調試信息 - 口爲587和主機爲smtp.gmail.com):

public void sendMailWithAuth(String host, String user, String password, 
    String port, List<String> toList, String htmlBody, 
     String subject) throws Exception { 

    Properties props = System.getProperties(); 

    props.put("mail.smtp.user",user); 
    props.put("mail.smtp.password", password); 
    props.put("mail.smtp.host", host); 
    props.put("mail.smtp.port", port); 
    //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"); 

    Session session = Session.getInstance(props, null); 
    //session.setDebug(true); 

    MimeMessage message = new MimeMessage(session); 
    message.setFrom(new InternetAddress(user)); 

    // To get the array of addresses 
    for (String to: toList) { 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
    } 

    message.setSubject(subject); 
    message.setContent(htmlBody, "text/html"); 

    Transport transport = session.getTransport("smtp"); 
    try { 
     transport.connect(host, user, password); 
     transport.sendMessage(message, message.getAllRecipients()); 
    } finally { 
     transport.close(); 
    } 
} 
相關問題