1\
2\
package org.jeecg.common.util.io;
import com.sun.mail.util.MailSSLSocketFactory;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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.*;
@Slf4j
public class MailUtil {
public static void main(String[] args) throws Exception {
sendQQEmail("[email protected]", "主题", "自测内容" + DateUtils.getNowStr());
//sendQQEmailHasFile("[email protected]", "主题", "自测内容" + DateUtils.getNowStr(), Arrays.asList(new File("C:\\Users\\xxxxx\\Desktop\\微信图片_xxxxx192242.jpg")));
}
private static Boolean sendQQEmail(String receiveEmail, String subject, String content) throws Exception {
Session session = getQQSession("smtp.qq.com", "[email protected]", "密码");
return send(session, "[email protected]", receiveEmail, subject, content, null);
}
private static Boolean sendQQEmailHasFile(String receiveEmail, String subject, String content, List<File> fileList) throws Exception {
Session session = getQQSession("smtp.qq.com", "[email protected]", "密码");
return send(session, "[email protected]", receiveEmail, subject, content, fileList);
}
private static List<BodyPart> getBodyPartList(List<File> fileList) throws Exception {
List<BodyPart> partList = new ArrayList<>();
if (fileList == null || fileList.size() <= 0) {
return partList;
}
for (File file : fileList) {
BodyPart bodyPart_curr = new MimeBodyPart();
DataSource dataSource = new FileDataSource(file.getPath());
bodyPart_curr.setDataHandler(new DataHandler(dataSource));
bodyPart_curr.setFileName(file.getName());
partList.add(bodyPart_curr);
}
return partList;
}
private static Session getQQSession(String host, String userName, String password) throws Exception {
Properties properties = System.getProperties();
MailSSLSocketFactory sslSocketFactory = new MailSSLSocketFactory();
sslSocketFactory.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sslSocketFactory);
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
//properties.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
return session;
}
private static Boolean send(Session session, String sendEmail, String receiveEmail, String subject, String content, List<File> fileList) throws Exception {
try {
MimeMessage message = new MimeMessage(session);
//防止邮件被当然垃圾邮件处理,披上Outlook的马甲
message.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
message.setFrom(new InternetAddress(sendEmail));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveEmail));
message.setSubject(subject);
//message.setText(content);
BodyPart bodyPart_text = new MimeBodyPart();
bodyPart_text.setText(content);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart_text);
List<BodyPart> bodyPartList = getBodyPartList(fileList);
bodyPartList.forEach(a -> {
try {
multipart.addBodyPart(a);
} catch (MessagingException e) {
e.printStackTrace();
}
});
message.setContent(multipart);
Transport.send(message);
String msg = DateUtils.getNowStrForLogPre()
+ String.format("邮件发送成功,发送者:%s,接收者:%s,主题:%s,内容:%s", sendEmail, receiveEmail, subject, content);
log.info(msg);
return true;
} catch (Exception e) {
String msg = DateUtils.getNowStrForLogPre()
+ String.format("邮件发送失败:%s,发送者:%s,接收者:%s,主题:%s,内容:%s", e.getMessage(), sendEmail, receiveEmail, subject, content);
log.error(msg, e);
return false;
}
}
private static Session getCapSession(String smtpServer, int smtpPort, String password) {
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpServer);
properties.put("mail.smtp.port", smtpPort);
properties.put("mail.smtp.auth", "false");
properties.put("mail.smtp.starttls.enable", "false");
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("from", password);
}
});
return session;
}
}
标签:qq,03,Java,String,new,2024,import,mail,com
From: https://www.cnblogs.com/kikyoqiang/p/18099960