ELK以及IDEA 控制台记录如下报错日志:
c.x.c.web.controller.subscribe.BoardJobController [sendEmail:145] 发送邮件失败 java.lang.IllegalStateException: The mail session is already initialized at org.apache.commons.mail.Email.checkSessionAlreadyInitialized(Email.java:1940) at org.apache.commons.mail.Email.setSmtpPort(Email.java:529) at com.xy.cloudiview.common.services.impl.BoardJobServiceImpl.lambda$sendEmail(BoardJobServiceImpl.java:367) at java.util.ArrayList.forEach(ArrayList.java:1249) at com.xy.cloudiview.common.services.impl.BoardJobServiceImpl.sendEmail(BoardJobServiceImpl.java:296) at com.xy.cloudiview.web.controller.subscribe.BoardJobController.sendEmail(BoardJobController.java:141) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) at org.springframework.web.servlet.frameworkServlet.processRequest(frameworkServlet.java:1006) at org.springframework.web.servlet.frameworkServlet.doPost(frameworkServlet.java:909) at javax.servlet.http.HttpServlet.service(HttpServlet.java:665) at org.springframework.web.servlet.frameworkServlet.service(frameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
使用的工具包:
org.apache.commons commons-email1.4
出错的代码片段
toList.forEach(toEmail -> { // 遍历每个收件人,对每个收件人发送邮件 HtmlEmail email = new HtmlEmail(); email.setCharset("utf-8"); try { email.setHtmlMsg(""); // 数据库查询邮箱 EmailSftpModel mailServer = emailSftpMapper.getOneMailRand(); email.setAuthentication(mailServer.getSmtpUsername(), mailServer.getSmtpPassword()); email.setFrom(mailServer.getSmtpFrom()); email.setSubject(subject); email.addTo(toEmail); if ("smtp.partner.outlook.cn".equals(mailServer.getSmtpHost())) { email.setStartTLSEnabled(true); Properties prop = new Properties(); prop.setProperty("mail.smtp.host", mailServer.getSmtpHost()); if (StringUtils.isNotEmpty(mailServer.getSmtpPort())) { prop.setProperty("mail.smtp.port", mailServer.getSmtpPort()); } prop.setProperty("mail.smtp.auth", "true"); prop.setProperty("mail.smtp.starttls.enable", "true"); prop.setProperty("mail.smtp.ssl.protocols", "TLSv1.2"); DefaultAuthenticator authenticator = new DefaultAuthenticator(mailServer.getSmtpUsername(), mailServer.getSmtpPassword()); Session session = Session.getInstance(prop, authenticator); email.setMailSession(session); } if (!Strings.isNullOrEmpty(mailServer.getSmtpPort())) { email.setSmtpPort(Integer.parseInt(mailServer.getSmtpPort())); } email.setHostName(mailServer.getSmtpHost()); email.send(); } catch (EmailException e) { logger.error("发送邮件失败,收件人:" + toEmail, e); sendErrLog.append("发送邮件失败,收件人:").append(toEmail).append(e.getMessage()).append(";"); } });
报错行:
email.setSmtpPort(Integer.parseInt(mailServer.getSmtpPort()));
查看源码:
public abstract class Email { public void setSmtpPort(final int aPortNumber) { checkSessionAlreadyInitialized(); if (aPortNumber < 1) { throw new IllegalArgumentException( "Cannot connect to a port number that is less than 1 ( " + aPortNumber + " )"); } this.smtpPort = Integer.toString(aPortNumber); } private void checkSessionAlreadyInitialized() { if (this.session != null) { throw new IllegalStateException("The mail session is already initialized"); } } }
解决方案,将如下代码片段
// fix IllegalStateException: The mail session is already initialized if (!Strings.isNullOrEmpty(mailServer.getSmtpPort())) { email.setSmtpPort(Integer.parseInt(mailServer.getSmtpPort())); } email.setHostName(mailServer.getSmtpHost());
提前到:email.setMailSession(session);之前。
即放在if ("smtp.partner.outlook.cn".equals(mailServer.getSmtpHost())) {之前。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)