package com.wangjunwei.test;
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
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;
public class Email {
private static String EmailSendAccount = "misslove3290@qq.com";
private static String EmailSendAccountPWD = "eonpaepqktxubfbi";
private static String EmailSendSMTPHOST = "smtp.qq.com";
public static void SendEmail(String sendAccount,String sendUserName,String receiveAccount,String receiveUserName,String emailTitel,String emailContent,Date emailSendDate) throws Exception{
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");//设置邮件协议
properties.setProperty("mail.smtp.host", EmailSendSMTPHOST);//设置邮件授权参数
properties.setProperty("mail.smtp.auth", "true");//设置认证方式
properties.setProperty("mail.smtp.timeout", "1000");
// PS: 某些邮箱服务器要求 SMTP 连接需要使用 SSL 安全认证 (为了提高安全性, 邮箱支持SSL连接, 也可以自己开启),
// 如果无法连接邮件服务器, 仔细查看控制台打印的 log, 如果有有类似 “连接失败, 要求 SSL 安全连接” 等错误,
// 打开下面 /* ... */ 之间的注释代码, 开启 SSL 安全连接。
// SMTP 服务器的端口 (非 SSL 连接的端口一般默认为 25, 可以不添加, 如果开启了 SSL 连接,
// 需要改为对应邮箱的 SMTP 服务器的端口, 具体可查看对应邮箱服务的帮助,
// QQ邮箱的SMTP(SLL)端口为465或587, 其他邮箱自行去查看)
final String smtpPort = "465";
properties.setProperty("mail.smtp.port", smtpPort);
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.socketFactory.port", smtpPort);
// 根据配置创建会话对象, 用于和邮件服务器交互
Session session = Session.getDefaultInstance(properties);
// 设置为debug模式, 可以查看详细的发送 log
session.setDebug(true);
// 创建一封邮件
MimeMessage mimeMessage = createMimeMessageWithAttachment(session, sendAccount, sendUserName, receiveAccount,receiveUserName, emailTitel, emailContent, emailSendDate);
// 创建邮件传输对象
Transport transport = session.getTransport();
transport.connect(EmailSendAccount, EmailSendAccountPWD);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
}
/**
* 创建邮件
* @param session 邮件会话
* @param sendAccount 发送账号
* @param sendUserName 发送用户名
* @param receiveAccount 接收账号
* @param receiveUserName 接收用户名
* @param emailTitel 邮件标题
* @param emailContent 邮件内容
* @param emailSendDate 邮件发送时间
* @return 邮件对象
* @throws Exception
*/
public static MimeMessage createMimeMessage(Session session,String sendAccount,String sendUserName,String receiveAccount,String receiveUserName,String emailTitel,String emailContent,Date emailSendDate) throws Exception{
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(sendAccount,sendUserName,"utf-8"));
// 收件人
mimeMessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveAccount,receiveUserName,"utf-8"));
// 添加收件人
mimeMessage.addRecipient(MimeMessage.RecipientType.TO,new InternetAddress(receiveAccount,receiveUserName,"utf-8"));
// 设置抄送
mimeMessage.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(receiveAccount,receiveUserName,"utf-8"));
// 设置密送
mimeMessage.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(receiveAccount,receiveUserName,"utf-8"));
// 设置标题
mimeMessage.setSubject(emailTitel,"utf-8");
// 设置邮件正文
mimeMessage.setContent(emailContent,"text/html;charset=UTF-8");
// 设置发件时间
mimeMessage.setSentDate(emailSendDate);
// 保存邮件设置
mimeMessage.saveChanges();
return mimeMessage;
}
/**
* 创建邮件(携带附件)
* @param session 邮件会话
* @param sendAccount 发送账号
* @param sendUserName 发送用户名
* @param receiveAccount 接收账号
* @param receiveUserName 接收用户名
* @param emailTitel 邮件标题
* @param emailContent 邮件内容
* @param emailSendDate 邮件发送时间
* @return 邮件对象
* @throws Exception
*/
public static MimeMessage createMimeMessageWithAttachment(Session session,String sendAccount,String sendUserName,String receiveAccount,String receiveUserName,String emailTitel,String emailContent,Date emailSendDate) throws Exception{
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(sendAccount,sendUserName,"utf-8"));
// 收件人
mimeMessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveAccount,receiveUserName,"utf-8"));
// 添加收件人
mimeMessage.addRecipient(MimeMessage.RecipientType.TO,new InternetAddress(receiveAccount,receiveUserName,"utf-8"));
// 设置抄送
mimeMessage.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(receiveAccount,receiveUserName,"utf-8"));
// 设置密送
mimeMessage.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(receiveAccount,receiveUserName,"utf-8"));
// 设置标题
mimeMessage.setSubject(emailTitel,"utf-8");
// 设置邮件文本
// mimeMessage.setText("这是附件测试");
BodyPart bodyPart = new MimeBodyPart();
// bodyPart.setText("这是附件测试邮件");
bodyPart.setContent("<h1>这是附件测试<span style='color: red;'>邮件</span></h1></p><img src='http://img3.redocn.com/tupian/20150312/haixinghezhenzhubeikeshiliangbeijing_3937174.jpg' width='200px' heigth='200px'/>","text/html;charset=utf-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart);
bodyPart = new MimeBodyPart();
DataSource dataSource = new FileDataSource(new File("D://公安局机关党委网站要求.txt"));
bodyPart.setDataHandler(new DataHandler(dataSource));
bodyPart.setFileName("公安局机关党委网站要求.txt");
multipart.addBodyPart(bodyPart);
// 设置邮件正文
mimeMessage.setContent(multipart);
// 设置发件时间
mimeMessage.setSentDate(emailSendDate);
// 保存邮件设置
mimeMessage.saveChanges();
return mimeMessage;
}
}