2017-01-14 19 views
-2

我想知道爲什麼下面的基本Java郵件程序不工作,因爲我沒有得到任何錯誤,因爲程序執行得很好。有什麼我做錯了嗎?任何幫助將不勝感激。我還想補充一點,我也嘗試過使用錯誤的用戶名和密碼組合,但仍然沒有錯誤,程序運行完全正常。簡單的Java郵件程序不工作?

public class emailfromgmail { 

public static void main(String[]args) 
{ 
    final String from = "username"; 
    final String pass = "password"; 
    String to = "[email protected]"; 
    String host="smtp.gmail.com"; 
    String subject = "java Mail"; 
    String body = "example of java mail api using gmail smtp"; 

//get the session object 

     Properties p = System.getProperties(); 
     p.put("mail.smtp.starttls.enable","true"); 
     p.put("mail.smtp.host",host); 
     p.put("mail.smtp.user",from); 
     p.put("mail.smtp.password",pass); 
     p.put("mail.smtp.port", "587"); 
     p.put("mail.smtp.auth","true"); 

     Session session = Session.getInstance(p, 
    new javax.mail.Authenticator() { 
       @Override 
    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(from, pass); 
    } 
    }); 

    try{ 
     MimeMessage msg = new MimeMessage(session); 
     msg.setFrom(new InternetAddress(from)); 
     msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
     msg.setSubject(subject); 
     msg.setText(body); 

     Transport.send(msg); 
     System.out.print("message sent successfully"); 

    } 
    catch(MessagingException e){ 
     e.printStackTrace(); 

    }  
} 
} 
+0

請解釋一下你的意思是「不工作」 –

+0

尊敬的先生其實我的意思是在程序執行罰款,我沒有得到任何錯誤,但它只是沒有做任何事情,我的意思是它應該發送郵件,但它沒有這樣做。 –

+0

由於電子郵件的「發件人」值不是電子郵件地址,而只是一個名稱,因此您的郵件可能會被作爲垃圾郵件丟棄。嘗試將'from'更改爲'sender @ gmail.com',其中'sender'當然是您的真實姓名。 – Andreas

回答

0

我有同樣的問題 - 我跑我的程序,並放在各地的對話框,看看它停止做任何事情。我仍然不知道什麼是錯,但我嘗試了一種完全不同的方法,並且它工作。我通過SSL發送,而不是通過TLS發送。我用這個網站:

https://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

他們TLS沒有工作 - 它只是沒有做任何事情,當我跑了。他們的SSL就像一個魅力!

下面是代碼:

package com.mkyong.common; 
 

 
import java.util.Properties; 
 
import javax.mail.Message; 
 
import javax.mail.MessagingException; 
 
import javax.mail.PasswordAuthentication; 
 
import javax.mail.Session; 
 
import javax.mail.Transport; 
 
import javax.mail.internet.InternetAddress; 
 
import javax.mail.internet.MimeMessage; 
 

 
public class SendMailSSL { 
 
\t public static void main(String[] args) { 
 
\t \t Properties props = new Properties(); 
 
\t \t props.put("mail.smtp.host", "smtp.gmail.com"); 
 
\t \t props.put("mail.smtp.socketFactory.port", "465"); 
 
\t \t props.put("mail.smtp.socketFactory.class", 
 
\t \t \t \t "javax.net.ssl.SSLSocketFactory"); 
 
\t \t props.put("mail.smtp.auth", "true"); 
 
\t \t props.put("mail.smtp.port", "465"); 
 

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

 
\t \t try { 
 

 
\t \t \t Message message = new MimeMessage(session); 
 
\t \t \t message.setFrom(new InternetAddress("[email protected]")); 
 
\t \t \t message.setRecipients(Message.RecipientType.TO, 
 
\t \t \t \t \t InternetAddress.parse("[email protected]")); 
 
\t \t \t message.setSubject("Testing Subject"); 
 
\t \t \t message.setText("Dear Mail Crawler," + 
 
\t \t \t \t \t "\n\n No spam to my email, please!"); 
 

 
\t \t \t Transport.send(message); 
 

 
\t \t \t System.out.println("Done"); 
 

 
\t \t } catch (MessagingException e) { 
 
\t \t \t throw new RuntimeException(e); 
 
\t \t } 
 
\t } 
 
}