mail.properties文件
#163
mail.default.from=11@163.com
mail.smtp.host=smtp.163.com
mail.sender.username=11@163.com
mail.sender.password=110ac14c82357
mail.port=25
mail.smtp.auth=true
mail.debug=true
mail.transport.protocol=smtp
mail.sender.nick_name=\u82F1\u5927\u957F\u5B89
mail.image.basePath =http\://172.168.1.1\:80/res/uploads
发送邮件类
package com.caej.insurance.backoffice.common.utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
/**
* java发送邮件和附件
* @author tongyh
*
*/
public class JavaMailUtilsByUTF {
protected final Logger log = Logger.getLogger(this.getClass());
/**
* Message对象将存储我们实际发送的电子邮件信息,
* Message对象被作为一个MimeMessage对象来创建并且需要知道应当选择哪一个JavaMail session。
*/
private MimeMessage message;
/**
* Session类代表JavaMail中的一个邮件会话。
* 每一个基于JavaMail的应用程序至少有一个Session(可以有任意多的Session)。
*
* JavaMail需要Properties来创建一个session对象。
* 寻找"mail.smtp.host" 属性值就是发送邮件的主机
* 寻找"mail.smtp.auth" 身份验证,目前免费邮件服务器都需要这一项
*/
private Session session;
/***
* 邮件是既可以被发送也可以被受到。JavaMail使用了两个不同的类来完成这两个功能:Transport 和 Store。
* Transport 是用来发送信息的,而Store用来收信。对于这的教程我们只需要用到Transport对象。
*/
private Transport transport;
private String mailHost="";
private String sender_username="";
private String sender_password="";
private String nick_name = "";
/*
* 初始化方法
*/
public JavaMailUtilsByUTF(boolean debug) {
Properties properties = new Properties();
DesUtils du=new DesUtils();
InputStream in = JavaMailUtilsByUTF.class.getResourceAsStream("/config/mail.properties");
try {
properties.load(in);
this.mailHost = properties.getProperty("mail.smtp.host");
this.sender_username = properties.getProperty("mail.sender.username");
String pass=properties.getProperty("mail.sender.password");
this.sender_password =(null!=pass)?du.decrypt(pass):"123456";//设置默认密码
this.nick_name = properties.getProperty("mail.sender.nick_name");
} catch (IOException e) {
e.printStackTrace();
}
session = Session.getInstance(properties);
session.setDebug(debug);//开启后有调试信息
message = new MimeMessage(session);
}
/**
* 发送邮件
*
* @param subject 邮件主题
* @param sendHtml 邮件内容
* @param receiveUser 收件人地址
*/
public boolean doSendHtmlEmail(String subject, String sendHtml,String receiveUser) {
try {
// 下面这个是设置发送人的Nick name
InternetAddress from = new InternetAddress("<"+sender_username+">");
message.setFrom(from);
// 收件人
InternetAddress to = new InternetAddress(receiveUser);
message.setRecipient(Message.RecipientType.TO, to);//还可以有CC、BCC
// 邮件主题
message.setSubject(subject);
String content = sendHtml.toString();
// 邮件内容,也可以使纯文本"text/plain"
message.setContent(content, "text/html;charset=UTF-8");
// 保存邮件
message.saveChanges();
transport = session.getTransport("smtp");
// smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect(mailHost, sender_username, sender_password);
// 发送
transport.sendMessage(message, message.getAllRecipients());
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
if(transport!=null){
try {
transport.close();
return true;
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
/**
* 发送邮件-附件
*
* @param subject 邮件主题
* @param sendHtml 邮件内容
* @param receiveUser 收件人地址
* @param attachment 附件
*/
public boolean doSendHtmlAttachEmail(String subject, String sendHtml,String receiveUser, File attachment) {
try {
// 发件人
InternetAddress from = new InternetAddress((new String(nick_name.getBytes(),"UTF-8"))+" <"+sender_username+">");
message.setFrom(from);
// 收件人
InternetAddress to = new InternetAddress(receiveUser);
message.setRecipient(Message.RecipientType.TO, to);
// 邮件主题
message.setSubject(subject);
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
// 添加邮件正文
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(sendHtml, "text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);
// 添加附件的内容
if (attachment != null) {
BodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
attachmentBodyPart.setDataHandler(new DataHandler(source));
//MimeUtility.encodeWord可以避免文件名乱码
attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
multipart.addBodyPart(attachmentBodyPart);
//多个附件构造BodyPart即可
/* BodyPart attachmentBodyPart2 = new MimeBodyPart();
DataSource source2 = new FileDataSource(image_2);
attachmentBodyPart2.setDataHandler(new DataHandler(source2));
attachmentBodyPart2.setFileName(MimeUtility.encodeWord(image_2.getName()));
multipart.addBodyPart(attachmentBodyPart2);
BodyPart attachmentBodyPart3 = new MimeBodyPart();
DataSource source3 = new FileDataSource(image_3);
attachmentBodyPart3.setDataHandler(new DataHandler(source3));
attachmentBodyPart3.setFileName(MimeUtility.encodeWord(image_3.getName()));
multipart.addBodyPart(attachmentBodyPart3);*/
}
// 将multipart对象放到message中
message.setContent(multipart);
// 保存邮件
message.saveChanges();
transport = session.getTransport("smtp");
// smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect(mailHost, sender_username, sender_password);
// 发送
transport.sendMessage(message, message.getAllRecipients());
log.info("send success!");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (transport != null) {
try {
transport.close();
return true;
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
}
用法: