请直接点击查看 有道云分享 ,后面的内容是为了方便被搜索引擎抓取!
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>
email.properties
配置文件中(文末),PropertiesUtil
类的作用是读取该文件的参数值。/**
* 使用Java Mail同步发送邮件,只包含发送功能,不处理Exception等问题
* @param email 接收方地址
* @param copyTo 抄送方邮件地址
* @param subject 主题
* @param content 正文内容
* @param attachPaths 附件文件全路径
*/
public void sendSyncEmail(String email, Collection<String> copyTo, String subject,
String content, String[] attachPaths) {
PropertiesUtil propertiesUtil = new PropertiesUtil("email.properties");
// 发件人电子邮箱信息
final String from = propertiesUtil.getString("mail.auth.username");
final String password = propertiesUtil.getString("mail.auth.password");
//阻止程序自动切割超长文件名,文件名经过base64编码后很容易超长,会导致文件名出错
//参考链接:https://blog.csdn.net/sunroyfcb/article/details/88971647
System.setProperty("mail.mime.splitlongparameters", "false");
// 设置邮件服务器参数
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", propertiesUtil.getString("mail.smtp.host"));
properties.put("mail.smtp.auth", propertiesUtil.getBoolean("mail.smtp.auth"));
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.socketFactory", sf);
properties.put("mail.smtp.ssl.enable", propertiesUtil.getString("mail.smtp.open.ssl"));
// 根据设置好的系统properties参数获取默认session对象
Session session = Session.getDefaultInstance(properties, new Authenticator() {
// qq邮箱服务器账户、第三方登录授权码
@Override
public PasswordAuthentication getPasswordAuthentication() {
// 发件人邮件用户名、密码
return new PasswordAuthentication(from, password);
}
});
// 根据session创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// set 发件人地址及 *邮箱别名*
message.setFrom(new InternetAddress(from, propertiesUtil.getString("mail.sender.alias"), "UTF-8"));
// set 收件人信息(可以使用上面的格式为每个收件人或抄送人设置别名)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
message.setSubject(subject, "UTF-8");
BodyPart mimeBodyPart = new MimeBodyPart();
// set 邮件正文,支持html标签解析(正文内容类型为html)
mimeBodyPart.setContent(content, "text/html; charset=UTF-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
// 处理邮件附件:每一个attachPath都是一个文件的绝对路径
for (String attachPath : attachPaths) {
mimeBodyPart = new MimeBodyPart();
File file = new File(attachPath);
DataSource source = new FileDataSource(file);
mimeBodyPart.setDataHandler(new DataHandler(source));
// 为附件设置文件名:MimeUtility.encodeText() 方法可防止文件名乱码
mimeBodyPart.setFileName(MimeUtility.encodeText(source.getName()));
multipart.addBodyPart(mimeBodyPart);
}
// 设置抄送:抄送地址必须满足标准email地址格式,否则报错
for (String copy : copyTo) {
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copy));
}
//set 邮件内容并发送
message.setContent(multipart);
Transport.send(message);
}
#邮件发送
mail.smtp.host=smtp.exmail.qq.com
mail.smtp.port=465
#某些邮箱服务器要求 SMTP 连接需要使用 SSL 安全认证
mail.smtp.open.ssl=true
#认证的帐户和密码(密码可能是登录密码,也可能是客户端授权码,设置错误的话会报503错误,应该在邮箱安全设置中可调节,请自行研究)
mail.auth.username=ydinform@qq.com
mail.auth.password=email_password
mail.smtp.auth=true
mail.sender.username=ydinform@qq.com
mail.sender.alias=发件人别名
原文:https://www.cnblogs.com/wang1024/p/10931606.html