以下为Java代码
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
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;
/**
* 邮件
* @ClassName EmailUtils.java
* @author wly
* @date 2018年8月20日 下午2:46:58
*/
public class EmailUtils {
private static String userName;
private static String passWord;
private static Properties properties;
static {
try {
properties = new Properties();
// 使用ClassLoader加载properties配置文件生成对应的输入流
InputStream in = EmailUtils.class.getClassLoader().getResourceAsStream("config/email_config.properties");
// 使用properties对象加载输入流
properties.load(in);
userName = properties.getProperty("mail.userName");
passWord = properties.getProperty("mail.passWord");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param title
* 邮件主题
* @param content
* 邮件内容
* @param toAddr
* 收件人邮箱
* @param ccAddr
* 抄送人邮箱
* @param bccAddr
* 密送人邮箱
* @param attachmentsMap
* 附件
* @throws Exception
*/
public void sendEmail(String title, String content, String[] toAddr, String[] ccAddr, String[] bccAddr, Map<String, String> attachmentsMap) throws Exception {
// 2、创建定义整个应用程序所需的环境信息的 Session 对象
Session session = Session.getInstance(properties);
// 设置调试信息在控制台打印出来
session.setDebug(true);
// 3、创建邮件的实例对象
Message msg = getMimeMessage(session, title, content, toAddr, ccAddr, bccAddr, attachmentsMap);
// 4、根据session对象获取邮件传输对象Transport
Transport transport = session.getTransport();
// 设置发件人的账户名和密码
transport.connect(userName, passWord);
// 抄送人, 密送人
transport.sendMessage(msg, msg.getAllRecipients());
// 5、关闭邮件连接
transport.close();
}
/**
* 获得创建一封邮件的实例对象
*
* @param session
* @param title
* 邮件主题
* @param content
* 邮件内容
* @param toAddr
* 收件人邮箱
* @param ccAddr
* 抄送人邮箱
* @param bccAddr
* 密送人邮箱
* @param attachmentsMap
* 附件
* @return
* @throws MessagingException
* @throws AddressException
*/
private MimeMessage getMimeMessage(Session session, String title, String content, String[] toAddr, String[] ccAddr, String[] bccAddr, Map<String, String> attachmentsMap) throws Exception {
// 创建一封邮件的实例对象
MimeMessage msg = new MimeMessage(session);
// 设置发件人地址
msg.setFrom(new InternetAddress(userName));
/**
* 设置收件人地址, MimeMessage.RecipientType.TO: 发送
* MimeMessage.RecipientType.CC: 抄送 MimeMessage.RecipientType.BCC: 密送
*/
if (toAddr == null)
throw new NullPointerException("收件人不能为空");
// 添加收件人
for (String to : toAddr) {
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
}
// 添加抄送
if (ccAddr != null)
for (String cc : ccAddr)
msg.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(cc));
// 添加密送
if (bccAddr != null)
for (String bcc : bccAddr)
msg.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(bcc));
// 设置邮件主题
msg.setSubject(title, "UTF-8");
MimeMultipart mm = new MimeMultipart();
MimeMultipart mimeMultipart = new MimeMultipart();
// html内容邮件
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8");
mimeMultipart.addBodyPart(text);
// 关联
mimeMultipart.setSubType("related");
// 将html内容封装到body中
MimeBodyPart text_image = new MimeBodyPart();
text_image.setContent(mimeMultipart);
mm.addBodyPart(text_image);
for (String key : attachmentsMap.keySet()) {
MimeBodyPart attachment = new MimeBodyPart();
// 读取本地文件
DataHandler dh2 = new DataHandler(new FileDataSource(attachmentsMap.get(key)));
// 将附件数据添加到"节点"
attachment.setDataHandler(dh2);
// 设置附件的文件名(需要编码)
attachment.setFileName(MimeUtility.encodeText(key));
// 将附件添加进body
mm.addBodyPart(attachment);
}
// 混合
mm.setSubType("mixed");
msg.setContent(mm);
// 设置邮件的发送时间,默认立即发送
msg.setSentDate(new Date());
return msg;
}
public static void main(String[] args) {
try {
Map<String, String> attachmentsMap = new HashMap<>();
attachmentsMap.put("read.jar", "E:/read.jar");
// 发送html内容邮件
new EmailUtils().sendEmail("测试", "<img src=\"//www.baidu.com/img/bd_logo1.png\" width=\"270\" height=\"129\">", new String[] { "xxx@qq.com" }, new String[] { "xxx@qq.com" }, null, attachmentsMap);
// 发送普通文本邮件
new EmailUtils().sendEmail("测试", "文本邮件", new String[] { "xxx@qq.com" }, new String[] { "xxxx@qq.com" }, null, attachmentsMap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
配置文件如下
#邮件服务器地址
mail.smtp.host=
#是否身份认证
mail.smtp.auth=true
#邮件协议
mail.transport.protocol=smtp
#启用调试模式
mail.debug=true
#端口号
mail.smtp.port=
#发件人帐号
mail.userName=
#发件人密码
mail.passWord=