【发布时间】:2017-08-12 11:51:37
【问题描述】:
我想创建电子邮件模板,因为我想附加 html 代码来创建模板,所以我尝试了下面的代码,其中 port 465 号码不起作用
谁能帮帮我?
package com.indoabus2.mail;
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 SendHTMLEmail {
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "vpenchalaprasad2@gmail.com";
// Sender's email ID needs to be mentioned
String from = "vpenchalaprasad2@gmail.com";
final String username = "vpenchalaprasad2";//change accordingly
final String password = "100509732041";//change accordingly
// Assuming you are sending email through relay.jangosmtp.net
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "465");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("Testing Subject");
// Send the actual HTML message, as big as you like
message.setContent(
"<h1>This is actual message embedded in HTML tags</h1>",
"text/html");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
坚果代码没有被执行异常是
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
我无法追踪为什么465 不起作用,以及response: -1 是什么,任何人都可以建议我一个解决方案
【问题讨论】: