使用Simple Java Mail它應該很簡單:
Email email = new Email();
email.setFromAddress("lollypop", "[email protected]");
email.addRecipient("C.Cane", "[email protected]", RecipientType.TO);
email.setText("We should meet up!");
email.setTextHTML("<b>We should meet up!</b>");
email.setSubject("hey");
new Mailer("smtp.gmail.com", 25, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 587, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 465, "your user", "your password", TransportStrategy.SMTP_SSL).sendMail(email);
如果您有雙重登錄輪流編輯後,您需要從您的Google帳戶生成application specific password。
如果你仍然想這個做自己,code behind this庫是非常簡單的。它設置在這取決於TransportStrategy傳遞(普通紙,SSL或TLS)會話特定的性質,它使用一個Authenticator執行認證:
"mail.transport.protocol" : "smtp"
"mail.smtp.starttls.enable" : "true"
"mail.smtp.host" : host
"mail.smtp.port" : port
"mail.smtp.username" : username
"mail.smtp.auth" : "true"
而且
return Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
您好。你在哪裏輸入實際的用戶名和密碼? – masb 2014-05-01 09:42:07
在Authenticator類的getPasswordAuthentication()方法 – 2014-05-02 04:55:57
嗯,當我設置「... socketFactory.class」它似乎使用SMTP而不是StartTLS而使用SSL,並且我得到「無法識別的SSL消息,明文連接?例外。 – Stroboskop 2016-01-13 11:55:06