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) –