<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
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 javax.mail.util.ByteArrayDataSource;
/**
* 邮件工具类
* @author xiaokong
*
*/
public class EmailUtil {
public static void sendMail(String receiveName, String htmlContent, String subjectText, File []files) throws Exception {
sendMail("smtp.qq.com", "dhbpmasfptoscagb", "530457326@qq.com", receiveName, htmlContent, subjectText, files);
}
public static void sendMail(String host, String password, String username, String receiveName, String htmlContent, String subjectText, File []files) throws Exception {
// 参数设置
Properties properties = new Properties();
/*
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
*/
properties.setProperty("mail.host", "smtp.qq.com");
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.port", "465");
properties.setProperty("mail.smtp.socketFactory.port", "465");
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 在session中设置账户信息,Transport发送邮件时会使用W
return new PasswordAuthentication(username, password);
}
});
//设置为debug模式,查看日志
session.setDebug(true);
MimeMessage mimeMessage = createMessage(session, username, receiveName, htmlContent, subjectText, files);
// 发送邮件
Transport.send(mimeMessage, mimeMessage.getAllRecipients());
}
private static MimeMessage createMessage(Session session, String username, String receiveName,
String htmlContent, String subjectText, File []files) throws Exception {
// 邮件
MimeMessage mimeMessage = new MimeMessage(session);
// 设置主题
mimeMessage.setSubject(subjectText);
// 发件人 MimeUtility.encodeText(username)中文处理
mimeMessage.setFrom(new InternetAddress("\"" + MimeUtility.encodeText(username) + "\"<" + username + ">"));
// 附件
MimeMultipart mimeMultipart = new MimeMultipart("mixed");// 混合的组合关系
mimeMessage.setContent(mimeMultipart);
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files[i];
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeMultipart.addBodyPart(mimeBodyPart);
FileInputStream fis = null;
try {
fis = new FileInputStream(files[i]);
ByteArrayDataSource dataSource = new ByteArrayDataSource(fis, "text/data");
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
mimeBodyPart.setFileName(file.getName());
} catch (Exception e) {
} finally {
if(fis!=null) {
fis.close();
}
}
}
}
mimeMessage.setRecipients(Message.RecipientType.TO, receiveName);
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlContent, "text/html;charset=utf-8"); // html
mimeMultipart.addBodyPart(htmlPart);
return mimeMessage;
}
// public static void main(String[] args) throws Exception {
// String host = "smtp.qq.com";
// String username = "xxxxxxxx@qq.com";
// String password = "xxxxxxxx";
// File file = new File("E:\\test.xlsx");
// FileInputStream fis = new FileInputStream(file);
// byte[] bs = new byte[fis.available()];
// fis.read(bs);
// File []files = new File[] {file};
// EmailUtil.sendMail(host, password, username, "xxxxxxxxx@qq.com", "2222哈哈哈aaaa", "bbbb哈哈哈cccc", files);
// fis.close();
// }
}
QQ邮箱设置