2015-09-20 28 views
-2

的電子郵件,我知道有很多這樣的問題,但他們並沒有爲我工作 所以如果有任何的方式來發送電子郵件與Java (雅虎,Gmail或Hotmail)如何發送與Java

+0

你可以發佈你的代碼? – soorapadman

+0

什麼不適合你?你有什麼嘗試?你是否認真地問我們沒有提供任何細節? – PeterPan

+0

谷歌它...學習別人如何..顯示一些例子,並解釋他們爲什麼沒有工作..其他谷歌自由職業者 – Mayhem

回答

1

我很抱歉,這個問題出現在我的防病毒軟件上,我只是關閉它,它工作,這是代碼。 ....不要忘記關閉防病毒

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 

public class SendFrom 
{ 
public static void main(String [] args) 
{  
    // Sender's email ID needs to be mentioned 

    String from = "[email protected]";// 

    String pass ="YourPassword"; 
    // Recipient's email ID needs to be mentioned. 
    String to = "DestinationEmail"; 
    String host = "smtp.gmail.com"; 


    // Get system properties 
    Properties properties = System.getProperties(); 
    // Setup mail server 
    //props.put("mail.smtp.ssl.trust", host); 
    //properties.put("mail.smtp.ssl.enable", "true"); 
    properties.put("mail.smtp.starttls.enable", "true"); 
    properties.put("mail.smtp.host", host); 
    properties.put("mail.smtp.user", from); 
    properties.put("mail.smtp.password", pass); 
    properties.put("mail.smtp.port", "587"); 
    properties.put("mail.smtp.auth", "true"); 
    properties.put("mail.debug", "true"); 

    // Get the default Session object. 
    Session session = Session.getDefaultInstance(properties); 

    try{ 
     // Create a default MimeMessage object. 
     MimeMessage message = new MimeMessage(session); 

     // Set From: header field of the header. 
     message.setFrom(new InternetAddress(from)); 

     // Set To: header field of the header. 
     message.addRecipient(Message.RecipientType.TO, 
           new InternetAddress(to)); 

     // Set Subject: header field 
     message.setSubject("This is the Subject Line!"); 

     // Now set the actual message 
     message.setText("This is actual message"); 

     // Send message 
     Transport transport = session.getTransport("smtp"); 
     transport.connect(host, from, pass); 

     transport.sendMessage(message, message.getAllRecipients()); 
     transport.close(); 
     System.out.println("Sent message successfully...."); 
    }catch (MessagingException mex) { 
     mex.printStackTrace(); 
    } 
} 
}