package xyz.anix.util;

import java.io.File;
import java.security.Security;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.junit.Test;


/**
* javaMail的邮件工具类
*
*/
public class sendEmailUtils {

/**
* 使用加密的方式,利用465端口进行传输邮件,开启ssl
* @param to 收件人邮箱
* @param message 发送的消息
*/
@SuppressWarnings("restriction")
public static void sendEmil(String to, String subject,String message,String html,List<File> files ) {
try {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
//设置邮件会话参数
Properties props = new Properties();
//邮箱的发送服务器地址
props.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
//邮箱发送服务器端口,这里设置为465端口
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
final String username = "echo@luotuowenxueshe.cn";
final String password = "Wxdlt1314";
//获取到邮箱会话,利用匿名内部类的方式,将发送者邮箱用户名和密码授权给jvm
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
//通过会话,得到一个邮件,用于发送
Message msg = new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress("echo@luotuowenxueshe.cn"));
//设置收件人,to为收件人,cc为抄送,bcc为密送
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(to, false));
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to, false));

//设置发送的日期
msg.setSentDate(new Date());

//设置邮件主题
msg.setSubject(subject);

//设置邮件消息
// msg.setText(message);

/**/

//整封邮件的MINE消息体
MimeMultipart msgMultipart = new MimeMultipart("mixed");//混合的组合关系
//设置邮件的MINE消息体
msg.setContent(msgMultipart);

//related
MimeMultipart relatedPart = new MimeMultipart("related");

//alternative
MimeMultipart alternativePart = new MimeMultipart("alternative");

//
MimeBodyPart relatedcontent = new MimeBodyPart();
MimeBodyPart alternativecontent = new MimeBodyPart();

//
relatedcontent.setContent(relatedPart);
alternativecontent.setContent(alternativePart);

if (message != null && message!="") {
//纯文本消息
BodyPart textpart = new MimeBodyPart();
textpart.setText(message);
alternativePart.addBodyPart(textpart);
}


if (html != null && html != "") {
//html代码部分
MimeBodyPart htmlPart = new MimeBodyPart();
//html代码
htmlPart.setContent(html,"text/html;charset=utf-8");
alternativePart.addBodyPart(htmlPart);
}

//html中嵌套的图片部分
//MimeBodyPart imgPart = new MimeBodyPart();
//正文添加图片和html代码
//relatedPart.addBodyPart(imgPart);
//图片代码
//imgPart.setHeader("Content-Location", "http://www.luotuowenxueshe.cn/upload/avatar/0/13.jpg");

//附件
// 添加附件的内容
if (null != files && files.size() != 0) {
for (File file : files) {
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
attachmentBodyPart.setDataHandler(new DataHandler(source));
//MimeUtility.encodeWord可以避免文件名乱码
attachmentBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));
//把附件加入到 MINE消息体中
msgMultipart.addBodyPart(attachmentBodyPart);
}
}

//把内容加入到 MINE消息体中
//relatedPart.addBodyPart(alternativecontent);
msgMultipart.addBodyPart(alternativecontent);

//生成文件邮件
msg.saveChanges();

/**/
//调用Transport的send方法去发送邮件
Transport.send(msg);

} catch (Exception e) {
e.printStackTrace();
}

}

@Test
public void testsend(){

String to ="1334663563@qq.com,18293169616@163.com";
String subject ="测试主题";
String text ="测试文本内容aaa111";
String html ="<span style='color:red'>中文呵呵5464646kl;kd</span> <img src='http://www.luotuowenxueshe.cn/upload/avatar/0/13.jpg'>";
File testFile=new File("D:\\test/123.txt");
File testFileimg=new File("D:\\test/93438.jpg");
File mydoc=new File("D:\\test/mydoc1.doc");

List<File> files=new ArrayList<File>();

files.add(testFile);
files.add(testFileimg);
files.add(mydoc);

sendEmil(to, subject, text,html, files);
}

}

 

最后修改于 2019-02-15 15:58:06
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇