文章来源:https://www.toymoban.com/news/detail-841942.html
Java实现邮件推送功能
一、引入依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
二、邮件来源用户信息
private static Logger log = Log.getLogger();
private static ThreadPoolExecutor threadPool;
private static String emailServer;
private static String userName;
private static String password;
private static String emailEncoding;
private static String emailFrom;
private static String sysEmailFromName;
private static String encoding;
private static void initMail(Email mail) throws EmailException {
mail.setHostName(emailServer);
mail.setAuthentication(userName, password);
mail.setCharset(emailEncoding);
mail.setFrom(emailFrom, sysEmailFromName);
}
三、线程池异步
static {
threadPool = new ThreadPoolExecutor(5, 10, 3L, TimeUnit.SECONDS, new ArrayBlockingQueue(50), new ThreadPoolExecutor.DiscardPolicy());
//邮箱服务器地址
emailServer = "smtp.163.com";
//用户名
userName = "****";
//用户密码
password = "****";
//编码格式
emailEncoding = "GBK";
//邮件来源
emailFrom = "*****.com.cn";
//邮件来源用户名称
sysEmailFromName = "****";
//编码格式
encoding = "UTF-8";
}
四、异步发送
private static void sendMail(Email mail) {
threadPool.execute(new Runnable() {
@Override
public void run() {
try {
mail.send();
} catch (EmailException var2) {
log.error("[邮件抄送失败]-", var2);
}
}
});
}
五 完整发送文章来源地址https://www.toymoban.com/news/detail-841942.html
import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.*;
import org.slf4j.Logger;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 邮件发送工具
*/
public class MailUtil {
private static Logger log = Log.getLogger();
private static ThreadPoolExecutor threadPool;
private static String emailServer;
private static String userName;
private static String password;
private static String emailEncoding;
private static String emailFrom;
private static String sysEmailFromName;
private static String encoding;
private static void initMail(Email mail) throws EmailException {
mail.setHostName(emailServer);
mail.setAuthentication(userName, password);
mail.setCharset(emailEncoding);
mail.setFrom(emailFrom, sysEmailFromName);
}
static {
threadPool = new ThreadPoolExecutor(5, 10, 3L, TimeUnit.SECONDS,
new ArrayBlockingQueue(50),
new ThreadPoolExecutor.DiscardPolicy());
//邮箱服务器地址
emailServer = "mailha.sinosafe.local";
//用户名
userName = "****";
//用户密码
password = "****";
//编码格式
emailEncoding = "GBK";
//邮件来源
emailFrom = "*****.com.cn";
//邮件来源用户名称
sysEmailFromName = "****";
//编码格式
encoding = "UTF-8";
}
/**
* 异步发送邮件
*/
到了这里,关于Java实现邮件推送功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!