import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Authenticator;
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 MailSender {
private Session session;
private final String username;
private final String password;
public MailSender(String username, String password) {
this.username = username;
this.password = password;
init(username, password);
}
public final void init(String username, String password) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(MailSender.this.username, MailSender.this.password);
}
});
}
public boolean send(String recipient, String subject, String body) {
boolean status = false;
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
status = true;
} catch (MessagingException ex) {
Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
}
return status;
}
public static void main(String[] args) {
MailSender mailer = new MailSender("[email protected]", "******");
//Userprojects u=new Userprojects();
boolean status = mailer.send("[email protected]", "Testing Subject", "Testing message");
System.out.println(status);
}
}
我已經使用了正確的登錄電子郵件ID和密碼編譯&使用JavamailAPI和我runabble批處理文件中的命令執行這個程序給出如下
Authenitication未能例外:534.5。 7.14 https://account.google.com/continousSignIn
,然後 「按指定下面給出異常」 。這是我們必須爲自己發現的那些巧妙的面試問題之一嗎?那是最後一個時期嗎?一個微縮的圖形圖像實際上是以微文本的形式存在的。或者你忘了包含它? – paxdiablo
發佈錯誤的 – JavaLearner
驗證失敗例外:534.5.7.14 https://account.google.com/continousSignIn –