JavaMail smtp properties (for STARTTLS)(JavaMail smtp 属性(用于 STARTTLS))
问题描述
JavaMail 指定了一组可以设置为配置 SMTP 连接的属性.要使用 STARTTLS,需要设置以下属性
JavaMail specifies a bunch of properties that can be set to configure an SMTP connection. To use STARTTLS it is necessary to set the following property
mail.smtp.starttls.enable=true
在哪里指定用户名/密码以使用 smtp 服务?是否足以指定:
Where do I specify the username/password to use the smtp service? Is it enough to specify the:
mail.smtp.user=me
mail.smtp.password=secret
或者我必须使用以下方式显式登录:
Or do I have to explicitely login using the:
transport.connect(server, userName, password)
是的,我已经尝试过这样做,似乎有必要使用 transport.connect(..)
进行连接.但如果是,mail.smtp.user
&传递属性?他们还不足以将 smtp 与 starttls
一起使用吗?
Yes, I already tried to do this and it seems that it is necessary to connect using transport.connect(..)
. But if yes, what are the mail.smtp.user
& pass properties for? Are they not enough to use smtp with starttls
?
推荐答案
这是我的 sendEmail 方法,它使用 GMail smtp (JavaMail) 和 STARTTLS
Here is my sendEmail method which is using GMail smtp (JavaMail) with STARTTLS
public void sendEmail(String body, String subject, String recipient) throws MessagingException,
UnsupportedEncodingException {
Properties mailProps = new Properties();
mailProps.put("mail.smtp.from", from);
mailProps.put("mail.smtp.host", smtpHost);
mailProps.put("mail.smtp.port", port);
mailProps.put("mail.smtp.auth", true);
mailProps.put("mail.smtp.socketFactory.port", port);
mailProps.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
mailProps.put("mail.smtp.socketFactory.fallback", "false");
mailProps.put("mail.smtp.starttls.enable", "true");
Session mailSession = Session.getDefaultInstance(mailProps, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(login, password);
}
});
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
String[] emails = { recipient };
InternetAddress dests[] = new InternetAddress[emails.length];
for (int i = 0; i < emails.length; i++) {
dests[i] = new InternetAddress(emails[i].trim().toLowerCase());
}
message.setRecipients(Message.RecipientType.TO, dests);
message.setSubject(subject, "UTF-8");
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(body, "text/html;charset=utf-8");
mp.addBodyPart(mbp);
message.setContent(mp);
message.setSentDate(new java.util.Date());
Transport.send(message);
}
这篇关于JavaMail smtp 属性(用于 STARTTLS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JavaMail smtp 属性(用于 STARTTLS)
基础教程推荐
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01