使用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