Jira 发送邮件,抛出异常 Error while sending mail: IMAPProvider not found,处理如下
Jira 发送邮件,抛出异常 Error while sending mail: IMAPProvider not found,处理如下:
import
com.sun.mail.imap.IMAPProvider;
import
com.sun.mail.pop3.POP3Provider;
import
com.sun.mail.smtp.SMTPProvider;
......
/**
*发送邮件
*/
public
static
void
sendMail(String to, String subject, String body)
throws
MailException {
log.info(
"发送邮件------sendMail---------"
);
val email =
new
Email(to);
email.setSubject(subject);
// 邮件主题
email.setBody(body);
// 邮件正文
email.setMimeType(
"text/html"
);
SMTPMailServer mailServer = MailFactory.getServerManager().getDefaultSMTPMailServer();
if
(Objects.isNull(mailServer)) {
// 若没找到 SMTP 邮件服务器,则不发送邮件
log.warn(
"SMTP Mail Server is not found. Please configure new SMTP mail server."
);
return
;
}
try
{
//获取一次Session进行确认,如果获取不到Session,会抛出异常
mailServer.getSession();
}
catch
(Throwable t) {
//异常处理
log.info(
"getSession error-----"
+t.getMessage());
log.info(
"ServiceLoader -----install"
);
//将发送邮件缺少的类,进行添加
ServiceLoader.loadInstalled(IMAPProvider.
class
);
ServiceLoader.loadInstalled(SMTPProvider.
class
);
ServiceLoader.loadInstalled(POP3Provider.
class
);
log.info(
"ServiceLoader -----install end"
);
}
// 发送时,会重新进行session 初始化操作
mailServer.send(email);
}
......
posted on 2021-02-09 18:38 Sunshine-jcy 阅读(185) 评论(0) 编辑 收藏 举报