1
我想创建电子邮件模板,因为我想追加html代码来创建模板,所以我试过下面的代码在这里port 465
号码不工作 任何人可以帮助我吗?无法连接到SMTP主机:smtp.gmail.com,端口:465,回应:-1为什么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 = "[email protected]";
// Sender's email ID needs to be mentioned
String from = "[email protected]";
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
任何一个可以建议我一个解决方案
而不是指定的SocketFactory的'更好的方法是使用JavaMail smtps协议而不是smtp。 –
感谢您在更改该行后重播以下异常即将到来'javax.mail.AuthenticationFailedException:535-5.7.8用户名和密码不被接受。但我的密码和邮件ID正确 – bharath
您需要启用“安全性较低的应用访问您的帐户”,请查看此[页](https://support.google.com/accounts/answer/6010255?hl= EN) –