我试图使用this example从gmail id向另一个发送邮件。这是我的档案:
public class SendHTMLEmail
// Recipient's email ID needs to be mentioned.
String to = "harsh.hr99@gmail.com";
// Sender's email ID needs to be mentioned
String from = "web@gmail.com";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.user", "Admin");
properties.setProperty("mail.password", "admin");
// 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!");
// Send the actual HTML message, as big as you like
message.setContent("<h1>This is actual message</h1>", "text/html" );
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
我在localhost:9091端口上运行Tomcat服务器。我收到以下错误:
我该如何解决这个问题?
答案 0 :(得分:0)
好吧,你设置&#34; host = localhost&#34;所以你的程序将尝试使用位于localhost的smtp服务器。问题是:您在安装localhost时没有(并且不想要)smtp服务器。至少看起来像那样。
我认为你想要做的是:http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
如果您尝试此示例,则需要记住不要发布它,因为它包含硬编码的gmail凭据。