我试图在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