赤青1

那些我们丢失的美好!是我们永远的遗憾与记忆

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

邮件发送工具:

private static String host = "smtp.exmail.qq.com";// 服务器地址
private static String port = "465";// 端口
private static String protocol = "smtp";// 协议

public static Session initProperties(String account, String password) {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", protocol);
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port", port);
// 使用smtp身份验证
properties.put("mail.smtp.auth", "true");
// 开启安全协议
MailSSLSocketFactory mailSSLSocketFactory = null;
try {
mailSSLSocketFactory = new MailSSLSocketFactory();
mailSSLSocketFactory.setTrustAllHosts(true);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
properties.put("mail.smtp.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory);
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
properties.put("mail.smtp.socketFactory.port", port);
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(account, password);
}
});
session.setDebug(true);
return session;
}
/**
* @Description: 发送邮件
* @Title: send
* @date 2019-10-08 17:12
* @param @param sender发件人别名
* @param @param subject邮件主题
* @param @param content接收者列表,多个接收者之间用","隔开
* @param @param receiverList
* @param @param fileSrc 附件地址
* @return void 返回类型
* @throws @return void
* @param sender
* @param subject
* @param content
* @param receiverList
* @param fileSrc
*/
public void send(String sender, String subject, String content, String receiverList, String fileSrc, String account,
String password) {
try {
Session session = initProperties(account, password);
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(account, sender));// 发件人,可以设置发件人的别名
// 收件人,多人接收
InternetAddress[] internetAddressTo = new InternetAddress().parse(receiverList);
mimeMessage.setRecipients(Message.RecipientType.TO, internetAddressTo);
// 主题
mimeMessage.setSubject(subject);
// 时间
mimeMessage.setSentDate(new Date());
// 容器类 附件
MimeMultipart mimeMultipart = new MimeMultipart();
// 附件
MimeBodyPart bodyPart = new MimeBodyPart();
// 设置内容
bodyPart.setContent(content, "text/html; charset=UTF-8");
mimeMultipart.addBodyPart(bodyPart);
// 添加图片&附件
if(fileSrc!=null){

bodyPart = new MimeBodyPart();
bodyPart.attachFile(fileSrc);

}
mimeMultipart.addBodyPart(bodyPart);
mimeMessage.setContent(mimeMultipart);
mimeMessage.saveChanges();
Transport.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

测试类:

public static void main(String[] args) throws Exception {
String account = "xxxxxxxx@xxxxxx.cn";// 登录账户
String password = "xxxxxxxxxxxxxxx";// 登录密码

SendEmailUtil tenMailUtil = new SendEmailUtil();
tenMailUtil.send("发件人别名", "邮件主题", "<h1 style='color:red'>HhhhHHHHHHH</h1>",
"xxxxxx@foxmail.com,xxxxx@xxxxx.cn", "C:/Users/Administrator/Desktop/index.html   as  附件", account,
password);
}

posted on 2019-10-09 16:45  赤青1  阅读(357)  评论(0编辑  收藏  举报