maven引入配置
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
工具类代码
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
public class SendMailUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(SendMailUtil.class);
public static boolean sendAttachMail(String mailHost, String protocol, String fromAddress,
String password, String recipientAddress, String subject,
String content, List<DataSource> attachFiles) {
Properties prop = new Properties();
prop.setProperty("mail.host", mailHost);
prop.setProperty("mail.transport.protocol", protocol);
prop.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(prop);
session.setDebug(true);
Transport ts = null;
try {
ts = session.getTransport();
ts.connect(mailHost, fromAddress, password);
Message message = createAttachMail(session, fromAddress, recipientAddress, subject,
content, attachFiles);
Thread.currentThread().setContextClassLoader(ts.getClass().getClassLoader());
ts.sendMessage(message, message.getAllRecipients());
} catch (MessagingException e) {
LOGGER.error("SendMailUtil.sendAttachMail MessagingException!subject={0}", subject, e);
return false;
} catch (Exception e) {
LOGGER.error("SendMailUtil.sendAttachMail Exception!subject={0}", subject, e);
return false;
} finally {
try {
ts.close();
} catch (MessagingException e) {
LOGGER.error("SendMailUtil.sendAttachMail finally MessagingException!subject={0}",
subject, e);
}
}
return true;
}
public static MimeMessage createAttachMail(Session session, String fromAddress,
String recipientAddress, String subject,
String content,
List<DataSource> ds) throws Exception {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientAddress));
message.setSubject(subject);
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8");
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.setSubType("mixed");
if (!CollectionUtils.isEmpty(ds)) {
for (DataSource dataSource : ds) {
DataHandler dh = new DataHandler(dataSource);
MimeBodyPart attach = new MimeBodyPart();
attach.setDataHandler(dh);
attach.setFileName(MimeUtility.encodeText(dh.getName()));
mp.addBodyPart(attach);
}
}
message.setContent(mp);
message.saveChanges();
return message;
}
public static void main(String[] args) {
String mailHost = "smtp.163.com";
String protocol = "smtp";
String fromAddress = "xxxxx@163.com";
String password = "xxxxxx";
String recipientAddress = "xxxxxx@qq.com";
String subject = "JavaMail邮件发送测试";
String content = "使用JavaMail创建的带附件的邮件";
List<DataSource> dataSources = new ArrayList<>();
FileDataSource attachFile = new FileDataSource(
"/Users/wangzhongxing/CodeFormatter.xml");
dataSources.add(attachFile);
boolean result = sendAttachMail(mailHost, protocol, fromAddress, password, recipientAddress, subject, content, dataSources);
System.out.println(result);
}
}