最近客户要求做邮件发送,做完后抽时间整理发个文章。
不多说,直接上代码
package com.hnac.hzinfo.mail.util;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.util.Properties;
/**
* 邮件发送
*/
@Service
public class MailUtil {
private static Logger logger = LoggerFactory.getLogger(MailUtil.class);
//SMTP发送邮件的服务器的IP
@Value("${mail.host}")
private String host;
//SMTP发送邮件的服务器的端口,qq-465,126-25
@Value("${mail.port}")
private String port;
//登陆SMTP邮件发送服务器的用户名
@Value("${mail.username}")
private String username;
//登陆SMTP邮件发送服务器的密码
@Value("${mail.password}")
//qq-qsggmfdnwtilbjhi
private String password;
//邮件发送账户
@Value("${mail.sendFrom}")
private String sendFrom;
@Value("${mail.sendNick}")
private String sendNick;
/**
* 发送邮件 (完整版)(结合Spring)
*
* //@param javaMailSender: 发送Bean //@param sendFrom : 发送人邮箱 //@param
* sendNick : 发送人昵称
*
* @param toAddress
* : 收件人邮箱
* @param mailSubject
* : 邮件主题
* @param mailBody
* : 邮件正文
* @param mailBodyIsHtml:
* 邮件正文格式,true:HTML格式;false:文本格式
* @param attachments
* : 附件
*/
public boolean sendMailSpring(String toAddress, String mailSubject, String mailBody, boolean mailBodyIsHtml,
File[] attachments) {
JavaMailSender javaMailSender = null;// ResourceBundle.getInstance().getJavaMailSender();
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8"); // 设置utf-8或GBK编码,否则邮件会有乱码;multipart,true表示文件上传
helper.setFrom(sendFrom, sendNick);
helper.setTo(toAddress);
// 设置收件人抄送的名片和地址(相当于群发了)
// helper.setCc(InternetAddress.parse(MimeUtility.encodeText("邮箱001")
// + " <@163.com>," + MimeUtility.encodeText("邮箱002") + "
// <@foxmail.com>"));
helper.setSubject(mailSubject);
helper.setText(mailBody, mailBodyIsHtml);
// 添加附件
if (ArrayUtils.isNotEmpty(attachments)) {
for (File file : attachments) {
helper.addAttachment(MimeUtility.encodeText(file.getName()), file);
}
}
// 群发
// MimeMessage[] mailMessages = { mimeMessage };
javaMailSender.send(mimeMessage);
return true;
} catch (Exception e) {
logger.info("{}", e);
}
return false;
}
/**
* 发送邮件 (完整版) (纯JavaMail)
*
* @param toAddress
* : 收件人邮箱
* @param mailSubject
* : 邮件主题
* @param mailBody
* : 邮件正文
* @param mailBodyIsHtml:
* 邮件正文格式,true:HTML格式;false:文本格式 //@param inLineFile : 内嵌文件
* @param attachments
* : 附件
*/
public boolean sendMail(String toAddress, String mailSubject, String mailBody, boolean mailBodyIsHtml,
File[] attachments) {
try {
// 创建邮件发送类 JavaMailSender (用于发送多元化邮件,包括附件,图片,html 等 )
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host); // 设置邮件服务主机
mailSender.setUsername(username); // 发送者邮箱的用户名
mailSender.setPassword(password); // 发送者邮箱的密码
// 配置文件,用于实例化java.mail.session
Properties pro = System.getProperties();
pro.put("mail.smtp.auth", "true"); // 登录SMTP服务器,需要获得授权
// (qq、网易126邮箱测试通过可以获得授权)
pro.put("mail.smtp.socketFactory.port", port);
pro.put("mail.smtp.socketFactory.fallback", "false");
mailSender.setJavaMailProperties(pro);
// 创建多元化邮件 (创建 mimeMessage 帮助类,用于封装信息至 mimeMessage)
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8");
helper.setFrom(sendFrom, sendNick);
String[] toAddressArr = toAddress.split(";");
if(toAddressArr.length>=1){
helper.setTo(toAddressArr[0]);
for (int i=1;i<toAddressArr.length;i++) {
helper.addTo(toAddressArr[i]);
}
}else{
logger.error("未设置邮件接收人!");
return false;
}
helper.setSubject(mailSubject);
//邮件正文不能为空
helper.setText(mailBody, mailBodyIsHtml);
// 添加内嵌文件,第1个参数为cid标识这个文件,第2个参数为资源
// helper.addInline(MimeUtility.encodeText(inLineFile.getName()),
// inLineFile);
// 添加附件
if (ArrayUtils.isNotEmpty(attachments)) {
for (File file : attachments) {
helper.addAttachment(MimeUtility.encodeText(file.getName()), file);
}
}
mailSender.send(mimeMessage);
return true;
} catch (Exception e) {
e.printStackTrace();
logger.error("", e);
}
return false;
}
static int total = 0;
public static void main(String[] args) {
MailUtil mailUtil = new MailUtil();
File[] attachments = new File[1];
File file = new File("C:\\Users\\zhongyj\\Desktop\\micro.txt");
attachments[0]= file;
String mailBody =
"<html><head><meta http-equiv=" + "Content-Type" + " content=" +
"text/html; charset=gb2312" +
"></head><body><h1>新书快递通知</h1>你的新书快递申请已推送新书,请到<a href=''>空间" +
"</a>中查看</body></html>";
mailUtil.sendMail("277674748@qq.com;wyjzhong@sina.cn", "测试邮件", mailBody, true, attachments);
/*ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i< 20; i++) {
exec.execute(new Thread(new Runnable() {
@Override
public void run() {
while (total < 1) {
String mailBody =
"<html><head><meta http-equiv=" + "Content-Type" + " content=" +
"text/html; charset=gb2312" +
"></head><body><h1>新书快递通知</h1>你的新书快递申请已推送新书,请到<a href=''>空间" +
"</a>中查看</body></html>";
sendMail("277674748@qq.com", "测试邮件", mailBody, false, null);
System.out.println(total);
total++;
}
}
}));
}*/
}
}