1、pom.xml依赖包引入
2、email.properties文件
3、前期工作
4、EmailUtil工具类
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>
2、email.properties文件
#email
mail.smtp.host=smtp.mxhichina.com
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.auth=true
mail.smtp.port=465
fromEmail=XX@XX.com
password=XXXXXXX
toEmail=XX@qq.com,XX@XX.com
3、前期工作
//创建Properties对象
Properties props = new Properties();
try {
//ClassUtils.getDefaultClassLoader().getResource("").getPath()获取项目地址
props.load(new FileInputStream(ClassUtils.getDefaultClassLoader().getResource("").getPath() + File.separator + "email.properties"));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
final String fromEmail = props.getProperty("fromEmail");//邮件发起人
final String password = props.getProperty("password");//邮件发起人密码
final String toEmail = props.getProperty("toEmail"); //接收人邮件
//获得网络连接验证的对象
Authenticator auth = new Authenticator() {
//override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, password);
}
};
//得到默认的对话对象
Session session = Session.getDefaultInstance(props, auth);
//标题
String subject = "";
//内容
StringBuilder temp = new StringBuilder();
String filePath = "";
//发送邮件
EmailUtil.sendEmailWithAttachment(session,fromEmail,toEmail,subject,body,filePath);
4、EmailUtil工具类
/**
* send HTML email with Attachment
* @param session
* @param fromEmail
* @param toEmail
* @param subject
* @param body
* @param fileName
*/
public static void sendEmailWithAttachment(Session session, String fromEmail, String toEmail, String subject, String body, String fileName) throws Exception{
//消息创建
MimeMessage msg = new MimeMessage(session);
//set message headers
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress(fromEmail));
// 创建邮件的接收者地址,并设置到邮件消息中
Address[] toEmails = null;
if(toEmail.indexOf(",")>-1) {
String[] emailList = toEmail.split(",");
toEmails= new InternetAddress[emailList.length];
for (int i = 0; i < emailList.length; i ++) {
toEmails[i] = new InternetAddress(emailList[i]);
}
} else {
toEmails= new InternetAddress[1];
toEmails[0] = new InternetAddress(toEmail);
}
msg.setRecipients(Message.RecipientType.TO,toEmails);
msg.setSentDate(new Date());
//标题
msg.setSubject(subject,"UTF-8");
//创建body
MimeMultipart allBodyPart = new MimeMultipart("mixed");
//创建正文html
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(body,"text/html;charset=gb2312");
allBodyPart.addBodyPart(htmlBodyPart);
//创建附件
MimeBodyPart attachBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(fileName);
attachBodyPart.setDataHandler(new DataHandler(source));
//不带路径文件名称
File tempFile =new File(fileName.trim());
attachBodyPart.setFileName(tempFile.getName());
allBodyPart.addBodyPart(attachBodyPart);
//设置整个邮件内容为最终组合出的MimeMultipart对象
msg.setContent(allBodyPart);
//发送邮件
Transport transport = session.getTransport("smtp");
//transport.connect("smtp.mxhichina.com" , 465 , fromEmail, password);链接邮件服务器
transport.send(msg);
}