发邮件分为几种协议:
- IMAP:收imap.example.com , 发stmp.example.com,端口,加密(SSL/TLS、 STARTTLS)。
- exchange:服务器、域、SSL
- POP3: 收pop.example.com , 发stmp.example.com,端口,加密。
stmp协议代码:
注意:
- 端口
- 加密
- 如果是ssl连接,需要在session的properties中增加设置:“mail.smtp.ssl.enable”, “true”。
- //设置超时时间为20秒 prop.setProperty(“mail.smtp.timeout”, “20000”);
public static void sendMail1(String sendFromDBAccount, String sendFromDBpassword, String sendTo, String sendCc, String mailTitle,
String mailContent) throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties prop = new Properties();
prop.setProperty("mail.smtp.host", Mail_host);
prop.setProperty("mail.smtp.port", Mail_port);
//设置用户认证方式
prop.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(prop, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Mail_from_account, Mail_from_password);
}
});
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(Mail_from_account));
msg.setRecipients(MimeMessage.RecipientType.TO, new InternetAddress().parse(sendTo));
msg.setRecipients(MimeMessage.RecipientType.CC, new InternetAddress().parse(sendCc));
msg.setSubject(mailTitle);
msg.setText(mailContent);
Transport.send(msg);
}
exchange代码:
注意:
- host很特殊,样例为 https://*****/ews/exchange.asmx
- BodyType.HTML
- 多收件人,逗号分割
- 抄送人
- 附件
public static boolean sendMail2(String to, String subject, String content, String filePath) {
mailLog.info(String.format("exchange邮件发送 to:{%s}, subject:{%s}, content:{%s},filePath:{%s}", to, subject, content, filePath));
boolean isOK = false;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ExchangeCredentials credentials = new WebCredentials(Mail_from_account2, Mail_from_password2);
service.setCredentials(credentials);
try {
service.setUrl(new URI(Mail_host2));
EmailMessage msg = new EmailMessage(service);
msg.setSubject(subject);
MessageBody body = MessageBody.getMessageBodyFromText(content);
// body.setBodyType(BodyType.HTML);
body.setBodyType(BodyType.Text);
msg.setBody(body);
// 支持多个收件人
InternetAddress[] addresses = InternetAddress.parse(to);
for (InternetAddress address : addresses) {
msg.getToRecipients().add(address.getAddress());
}
if (!CommonUtils.isNullString(filePath)) {
msg.getAttachments().addFileAttachment(filePath);
}
msg.send();
isOK = true;
} catch (Exception e) {
e.printStackTrace();
mailLog.error("=>sendMail5-exchage-fail", e);
isOK = false;
}
// System.out.println(isOK);
return isOK;
}
引用包:
// e-mail
compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.0'
// e-mail-exchange
compile group: 'com.microsoft.ews-java-api', name: 'ews-java-api', version: '2.0'
compile group: 'joda-time', name: 'joda-time', version: '2.10.6'
参考资料:
- https://www.jb51.net/article/180095.htm
- https://segmentfault.com/q/1010000013832196
- https://mvnrepository.com/artifact/com.microsoft.ews-java-api/ews-java-api/2.0
- https://segmentfault.com/q/1010000004497746
- https://blog.csdn.net/zyhlwzy/article/details/79989577
- https://www.cnblogs.com/JackFe/p/6557365.html
- https://www.bbsmax.com/A/qVdeBAKrzP/
- https://www.cnblogs.com/qq642193463/p/10691516.html
- http://www.manongjc.com/article/29461.html