2013-03-24 200 views
14

這段代碼有什麼問題?不知怎的,它是在該行Transport.send(message);線,沒有錯誤信息,也不例外獲得在一個無限循環,說不定無限循環(我不知道,因爲我不等待超過5-10minute)通過gmail發送郵件smtp服務器在JAVA

final String username = "<mail_name>"; 
final String password = "<password>"; 

Properties props = new Properties(); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.starttls.enable", "true"); 
props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.port", "465"); 

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

try { 

    Message message = new MimeMessage(session); 
    message.setFrom(new InternetAddress("<mail_from>@gmail.com")); 
    message.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse("<mail_to>@gmail.com")); 
    message.setSubject("Test Subject"); 
    message.setText("Test"); 

    Transport.send(message); 

    System.out.println("Done"); 

} catch (MessagingException e) { 
    throw new RuntimeException(e); 
} 
+0

我建議在MessagingException之後添加一個'catch(Exception e){}'來查看是否有其他類型的異常被拋出。還要添加一個'finally'塊。您還可以添加'log4j.xml'並將'javax.mail'類設置爲'DEBUG'來查看還有什麼可以進行。 – Bizmarck 2013-03-24 11:04:17

+0

好的提示,謝謝!將測試它 – czupe 2013-03-24 11:06:39

+0

不幸的是仍然在Transport.send(消息)行等待...有人可以驗證此代碼的正確性? – czupe 2013-03-24 11:28:18

回答

18

在這裏我給一些變化,這些工作對我罰款:

Session session = Session.getInstance(props,null); 

您實例化的消息對象爲你做。最後:

Transport transport = session.getTransport("smtp"); 
String mfrom = "[email protected]"// example laabidiraissi 
transport.connect("smtp.gmail.com", mfrom, "thepassword"); 
transport.sendMessage(message, message.getAllRecipients()); 

編輯,請你給我一個忙,複製/粘貼和嘗試這個例子,顯示它顯示:

我可以重現行爲在你的問題中描述
package com.test; 

import java.util.Properties; 

import javax.mail.BodyPart; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

import org.junit.Test; 

public class EmailService { 

@Test 
public void test(){ 
    Properties props = System.getProperties(); 
    props.put("mail.smtp.starttls.enable", true); // added this line 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.user", "username"); 
    props.put("mail.smtp.password", "password"); 
    props.put("mail.smtp.port", "587"); 
    props.put("mail.smtp.auth", true); 



    Session session = Session.getInstance(props,null); 
    MimeMessage message = new MimeMessage(session); 

    System.out.println("Port: "+session.getProperty("mail.smtp.port")); 

    // Create the email addresses involved 
    try { 
     InternetAddress from = new InternetAddress("username"); 
     message.setSubject("Yes we can"); 
     message.setFrom(from); 
     message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("receivermail")); 

     // Create a multi-part to combine the parts 
     Multipart multipart = new MimeMultipart("alternative"); 

     // Create your text message part 
     BodyPart messageBodyPart = new MimeBodyPart(); 
     messageBodyPart.setText("some text to send"); 

     // Add the text part to the multipart 
     multipart.addBodyPart(messageBodyPart); 

     // Create the html part 
     messageBodyPart = new MimeBodyPart(); 
     String htmlMessage = "Our html text"; 
     messageBodyPart.setContent(htmlMessage, "text/html"); 


     // Add html part to multi part 
     multipart.addBodyPart(messageBodyPart); 

     // Associate multi-part with message 
     message.setContent(multipart); 

     // Send message 
     Transport transport = session.getTransport("smtp"); 
     transport.connect("smtp.gmail.com", "username", "password"); 
     System.out.println("Transport: "+transport.toString()); 
     transport.sendMessage(message, message.getAllRecipients()); 


    } catch (AddressException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (MessagingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
} 
+0

謝謝,但mail.smtp.auth屬性已經存在了...而且我也嘗試過使用其他端口。 – czupe 2013-03-24 12:07:39

+0

啊對於auth屬性感到抱歉,這是我的錯,我沒有看到它。 – 2013-03-24 12:09:03

+0

如果嘗試使用25或587端口,無論如何檢查您的解決方案... 線程「主」異常java.lang.RuntimeException:javax.mail.MessagingException:無法連接到SMTP主機:smtp.gmail.com,港口:25; 嵌套的異常是: \t java.net.ConnectException:連接被拒絕:connect – czupe 2013-03-24 12:23:38

1

和修理它。

send方法被卡在

SMTPTransport(Service).connect(String, int, String, String) line: 308 

該連接不會成功,因爲你有錯誤的端口爲Gmail的SMTP主機:465

將其更改爲587

props.put("mail.smtp.port", "587"); 

它會工作。

+0

對呀,不知道爲什麼我騎465港口,但無論如何,它仍然沒有工作:( – czupe 2013-03-24 12:33:01

+0

你現在得到什麼異常/錯誤?你的java.exe是否允許通過防火牆? – A4L 2013-03-24 12:40:37

+0

This:java.lang.RuntimeException:javax.mail.MessagingException:無法連接到SMTP主機:smtp.gmail.com,端口:25;嵌套的異常是:java.net.ConnectException:連接被拒絕:連接 我忽略了整個Windows防火牆.. – czupe 2013-03-24 12:45:10

3

好的。這是一個稍微複雜一點比我因子評分的第一次...要summorize我得到了什麼:

  • 有一個非常有用的命令:session.setDebug(true);。如果你確定這一點,每一個重要的過程都會被控制檯擊敗。我建議使用它。
  • 第二臺計算機只能使用安全選項,您可以使用:Transport transport = session.getTransport("smtps");而不是非安全smtp進行切換... JavaMail API傳輸對象還將處理端口(分別爲smtp:587,smtps :465)
  • 您也可以使用Transport類的靜態方法來發送消息和(保存之前,非靜態sendMessage方法不會保存消息),但是這次您需要使用javax.mail 。創建會話時的驗證器,如下所示:

    Session session = Session.getInstance(props,   new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication("login", "password"); 
        } 
    

    });

1.4.2 JavaMailApi還有一個例外比1.4.7版本這個問題...

如果你不使用它,你不能用靜態方法驗證。如果你使用實例方法,你可以。

  • 一臺電腦有maven,並且獲得了JavaMail API的1.4.2版本。第二臺電腦有一個下載的庫,版本爲1.4.7,我猜也是這樣的東西
  • 第一個comp Netbeans,第二個comp Intellij ... +1)在互聯網上有很多舊的和不好的示例代碼,這使得更難以正確使用這個API。

那麼漂亮搞砸了,但也有一些基本的概念,它應該集中...

1

使用Simple Java Mail它應該是簡單的:

Email email = new Email(); 

email.setFromAddress("lollypop", "[email protected]"); 
email.addRecipient("C.Cane", "[email protected]", RecipientType.TO); 
email.setText("We should meet up!"); 
email.setTextHTML("<b>We should meet up!</b>"); 
email.setSubject("hey"); 

new Mailer("smtp.gmail.com", 25, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email); 
new Mailer("smtp.gmail.com", 587, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email); 
new Mailer("smtp.gmail.com", 465, "your user", "your password", TransportStrategy.SMTP_SSL).sendMail(email); 

如果你有兩個因素登錄開啓您需要從您的Google帳戶生成application specific password

相關問題