java工具类之邮件工具类
import com.sun.mail.util.MailSSLSocketFactory;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class MailUtils {
private static String from;
private static String pass;
private static String host;
private static List<Map<String,String>> configList = new ArrayList();
public static void setMailConfig(String _from,String _pass,String _host){
if(from==null||pass==null||host==null){
from = _from;
pass = _pass;
host = _host;
}
HashMap<String, String> map = new HashMap();
map.put("from",_from);
map.put("pass",_pass);
map.put("host",_host);
configList.add(map);
}
private static int index = 0;
public static boolean sendForeach(String receive, String subject, String msg){
return sendForeach(receive,subject,msg,null);
}
public static boolean sendForeach(String receive, String subject, String msg, String filename){
return sendForeach(receive, subject, msg, filename);
}
public static boolean sendForeach(String receive, String subject, String msg, String filename, String reName){
if (isEmpty(receive)) {
return false;
}
boolean reBoo = true;
try {
send(receive,subject,msg,filename,reName);
} catch (Exception e) {
System.out.println(new Date()+"发送邮件失败,当前发件箱为:"+from);
index++;
if(index>=configList.size()){
return false;
}
changeConfig();
reBoo = sendForeach(receive,subject,msg,filename,reName);
}
return reBoo;
}
private static boolean isEmpty(String receive) {
return receive==null||"".equals(receive);
}
private static void changeConfig() {
Map<String, String> map = configList.get(index);
from = map.get("from");
pass = map.get("pass");
host = map.get("host");
}
private static boolean send(String receive, String subject, String msg, String filename, String reName)
throws Exception {
if (isEmpty(receive)) {
return false;
}
if(configList.size()==0){
return false;
}
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(receive));
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(msg);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
if(!isEmpty(filename)){
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
String fileName = MimeUtility.encodeText(source.getName());
if(!isEmpty(reName)){
fileName = reName;
}
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
}
message.setContent(multipart);
Transport.send(message);
return true;
}
public static void main(String[] args) {
setMailConfig("123456@qq.com","gudsafwupxiibhjd","smtp.qq.com");
setMailConfig("932326@qq.com","gudsafwupxiibhjd","smtp.qq.com");
String receive="123456@qq.com";
String subject ="标题";
String msg ="邮件的正文内容";
String filename ="D:\\myHome\\2021-08-10.xlsx";
filename = null;
boolean b = sendForeach(receive, subject, msg, filename,"123.xlsx");
System.out.println(b);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!