在以下會話對象中的代碼(PasswordAuthentication)
我們必須提供什麼用戶名和密碼才能發送郵件?發件人的用戶名密碼或接收者的憑據? 我真的很困惑,我使用Java郵件發送郵件JavaMail中的會話
public void sendMail(String email,String token)
{
// Recipient's email ID needs to be mentioned.
String to = email;
// Sender's email ID needs to be mentioned
// 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", "587");
// 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("Issme-Customer-Service"));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Email Verification Of issme Account");
message.setContent(
"<h2>Email Verification </h2>" +
"<h3> Please goto the following URL to verify your ISSME account\n </h3> " +
token , "text/html; charset=utf-8");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
爲什麼會是接收者的憑據?這是發件人的用戶名和密碼,就像您在郵件客戶端中設置的一樣。 – millimoose
如果這是一個Web應用程序,那麼最好在應用程序服務器中設置JavaMail會話。連接到外部服務是一個部署環境問題,它們的參數不應該被硬編碼。 – millimoose
您需要設置可以驗證到SMTP服務器(發件人)的憑據 –