前言
借用hutool工具,实现多账号发送邮件
相关实现
maven依赖
Hutool对所有第三方都是可选依赖,因此在使用MailUtil时需要自行引入第三方依赖。
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.15</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
简化需求场景
由于hutool中MailAccount没有合适得构造方法构建对象,我就创建了一个子类MailAccountSon继承MailAccount
在构建MailAccountSon时并构建MailAccount的相关属性值
当然你可以直接new MailAccount(),但是我觉得麻烦并且和账户耦合度很高
官方:
除了使用配置文件定义全局账号以外,MailUtil.send
方法同时提供重载方法可以传入一个MailAccount
对象,这个对象为一个普通Bean,记录了邮件服务器信息。
MailAccount account = new MailAccount();
account.setHost("smtp.yeah.net");
account.setPort("25");
account.setAuth(true);
account.setFrom("hutool@yeah.net");
account.setUser("hutool");
account.setPass("q1w2e3");
MailUtil.send(account, CollUtil.newArrayList("hutool@foxmail.com"), "测试", "邮件来自Hutool测试", false);
talk is cheap,look my code!
代码实现
MailAccountSon类,其中的构造方法根据需求添加,如果你是企业邮箱,参考hutool定义对应的构造函数
/**
* @author Xuewei.Zhang
* @date 2022/11/24
*/
public class MailAccountSon extends MailAccount {
public MailAccountSon(String host, Integer port, Boolean auth, String from, String user, String pass) {
super.setHost(host);
super.setPort(port);
super.setAuth(auth);
super.setFrom(from);
super.setUser(user);
super.setPass(pass);
}
public MailAccountSon(String host, Integer port, Boolean auth, String from, String pass) {
super.setHost(host);
super.setPort(port);
super.setAuth(auth);
super.setFrom(from);
super.setPass(pass);
}
public MailAccountSon(String from, String pass) {
super.setFrom(from);
super.setPass(pass);
}
}
SYS_EMAIL_MAP里面存放了我多个邮件账号实例,我将使用他们发送邮件
public interface SysConstants {
String EMAIL_126 = "126";
String EMAIL_qq = "qq";
// 邮件服务器
Map<String, MailAccountSon> SYS_EMAIL_MAP = new HashMap<String, MailAccountSon>() {{
put(EMAIL_126,new MailAccountSon("zhangxuewei0303@126.com","CTLTO***K****J") );
put(EMAIL_qq,new MailAccountSon("2289998609@qq.com","rp****oo***bhj") );
}};
}
测试QQ
public static void main(String[] args) {
MailAccount mailAccount = SysConstants.SYS_EMAIL_MAP.get(SysConstants.EMAIL_qq);
String msg = MailUtil.send(mailAccount, CollUtil.newArrayList("1263944101@qq.com"), "测试", "邮件来自erp_cloud测试", false);
log.info(msg);
}
打印信息
Connected to the target VM, address: '127.0.0.1:13216', transport: 'socket'
16:57:58.834 [main] INFO com.zxw.common.web.util.EmailUtil - <2040352617.1.1669280277966@LAPTOP-ORGH9L27.mshome.net>
Disconnected from the target VM, address: '127.0.0.1:13216', transport: 'socket'
测试126
public static void main(String[] args) {
MailAccount mailAccount = SysConstants.SYS_EMAIL_MAP.get(SysConstants.EMAIL_126);
String msg = MailUtil.send(mailAccount, CollUtil.newArrayList("1263944101@qq.com"), "测试", "邮件来自erp_cloud测试", false);
log.info(msg);
}
打印信息
Connected to the target VM, address: '127.0.0.1:1431', transport: 'socket'
17:18:13.025 [main] INFO com.zxw.common.web.util.EmailUtil - <2040352617.1.1669281492247@LAPTOP-ORGH9L27.mshome.net>
Disconnected from the target VM, address: '127.0.0.1:1431', transport: 'socket'