我試圖在Eclipse中使用Java發送簡單郵件。當我試圖運行該程序時,打開一個名爲ast jtree示例的小窗口。它的根目錄是c1 c2 c3,郵件沒有發送。在eclipse中使用Java Mail發送郵件時出錯
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SingleEmail {
public static void main(String[] args) {
String to = "[email protected]";
String from = "[email protected]";
String host = "localhost";//or IP address
//Get the session object
Properties properties = System.getProperties();
properties.setProperty("mail.user", "[email protected]");
properties.setProperty("mail.password", "tecknodel");
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
//compose the message
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Ping");
message.setText("Hello, this is example of sending email ");
// Send message
Transport.send(message);
System.out.println("message sent successfully....");
}catch (MessagingException mex) {mex.printStackTrace();}
}
}
你有本地主機上運行的郵件服務器嗎?因爲這是你發送郵件的地方。 – Glorfindel
其實我並不熟悉java,所以請你介紹一下郵件服務器。 – kani
檢查@馬努的答案下面,那些似乎是正確的配置。 – Glorfindel