com.github.jackpanz.spring.email.EmailUtils Maven / Gradle / Ivy
package com.github.jackpanz.spring.email;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
public class EmailUtils {
private static Logger LOGGER = LoggerFactory.getLogger(EmailUtils.class);
public static void send(EmailObject emailObject) {
boolean isAuth = StringUtils.isNotEmpty(emailObject.getUserName()) && StringUtils.isNotEmpty(emailObject.getPassword());
try {
LOGGER.info("准备发送邮件");
//可以加载一个配置文件
Properties props = new Properties();
//使用SMTP简单邮件传输协议
//存储发送邮件服务器的信息
props.put("mail.smtp.host", emailObject.getSmtpHost()); //SMTP Host
props.put("mail.smtp.port", emailObject.getPort()); //TLS Port
props.put("mail.smtp.auth", String.valueOf(isAuth)); //enable authentication
// props.put("mail.transport.protocol", "smtp");
//https
props.put("mail.smtp.starttls.enable", String.valueOf(emailObject.getStarttls())); //enable STARTTLS
Session session = Session.getInstance(props, isAuth ? new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailObject.getUserName(), emailObject.getPassword());
}
} : null);//根据属性新建一个邮件会话
MimeMessage message = new MimeMessage(session);//由邮件会话新建一个消息对象
if (emailObject.getPersonal() != null)
message.setFrom(new InternetAddress(emailObject.getFrom(), emailObject.getPersonal()));//设置发件人的地址
else
message.setFrom(new InternetAddress(emailObject.getFrom()));//设置发件人的地址
//设置收件人,并设置其接收类型为TO
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailObject.getTo()));
//设置标题
message.setSubject(emailObject.getSubject(), "UTF-8");
MimeMultipart multipart = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
if (StringUtils.isNotEmpty(emailObject.getText())) {
//text
message.addHeader("Content-type", "text/plain; charset=UTF-8");
textBodyPart.setContent(emailObject.getText(), "text/plain;charset=UTF-8");
} else {
//html
message.addHeader("Content-type", "text/html; charset=UTF-8");
textBodyPart.setContent(emailObject.getHtml(), "text/html;charset=UTF-8");
}
multipart.addBodyPart(textBodyPart);
message.setContent(multipart);
//设置发信时间
message.setSentDate(new Date());
//存储邮件信息
if (emailObject.getAttachments() != null && !emailObject.getAttachments().isEmpty()) {
for(int i=0;i测试
你不在学校吗?
记得28号来学校
");
emailObject.setSmtpHost("smtp.exmail.qq.com");
emailObject.setPort("587");
emailObject.setUserName("[email protected]");
emailObject.setPassword("L2MrIKD5");
emailObject.setAttachments(new ArrayList() {{
add(new File("D:\\linux\\server.xml"));
add(new File("C:\\Users\\bj\\Desktop\\female\\13.jpg"));
}});
EmailUtils.send(emailObject);
}
}