java.lang.IllegalStateException:MimeMessage已经构建

时间:2017-09-26 20:01:08

标签: java email illegalstateexception

我尝试在spring应用程序中设置邮件发件人:

@Configuration
public class MailConfig {

    private static final String HOSTNAME = "mail.********.com";
    private static final int SMTP_PORT = 25;
    private static final String USERNAME = "*********";
    private static final String PASSWORD = "*********";
    private static final String FROM_ADDRESS = "***********";

    @Bean
    public HtmlEmail mailSender() {
        HtmlEmail email = new HtmlEmail();
        email.setHostName(HOSTNAME);
        email.setSmtpPort(SMTP_PORT);
        email.setAuthenticator(new DefaultAuthenticator(USERNAME, PASSWORD));
        email.setSSLOnConnect(true);
        try {
            email.setFrom(FROM_ADDRESS, "Support");
        } catch (EmailException e) {
            throw new BeanCreationException("mailSender", "Failed to create a mailSender", e);
        }
        return email;
    }

}

这是我用来发送邮件的服务:

@Service
public class MailService {

    public static Logger log = Logger.getLogger(Logger.class.getName());

    private final HtmlEmail mailSender;

    @Autowired
    public MailService(HtmlEmail mailSender) {
        this.mailSender = mailSender;
        this.notificationService = notificationService;
        this.commonDAO = commonDAO;
    }

    @Async
    public void sendMail(int accountId, String subject, String message) {
        log.debug("entered MailService.sendMail");
        String emailId = commonDAO.getEmailId(accountId);
        try {
            mailSender.setSubject(subject);
            mailSender.setHtmlMsg(getMailContent(message, accountId));
            mailSender.addTo(emailId);
            mailSender.addBcc("************");
            mailSender.send();
            sleep(1000L);
        } catch (Exception e) {
            log.error("Error:", e);
        }
    }
}

首次启动tomcat后,当我调用mailService.sendMail()时,邮件发送正确。第二次,我得到错误

java.lang.IllegalStateException: The MimeMessage is already built.
    at org.apache.commons.mail.Email.buildMimeMessage(Email.java:1261)
    at org.apache.commons.mail.MultiPartEmail.buildMimeMessage(MultiPartEmail.java:258)
    at org.apache.commons.mail.HtmlEmail.buildMimeMessage(HtmlEmail.java:528)
    at org.apache.commons.mail.Email.send(Email.java:1427)
    at com.cinch.v2.service.MailService.sendMail(MailService.java:41)
    at com.cinch.v2.service.MailService$$FastClassBySpringCGLIB$$d7350403.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:115)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.lang.Thread.run(Thread.java:745)

我在这里做错了什么?如何正确设置?

2 个答案:

答案 0 :(得分:0)

查看HtmlEmail。它有buildMimeMessage()sendMimeMessage()两种方法。根据{{​​3}},您可以调用buildMimeMessage()一次显式构建您的消息Mime,之后您可以随时再次显式调用'sendMimeMessage()`来发送消息。

尝试这样的事情:

public static buildEmail( String subject, int accountId){
String emailId = commonDAO.getEmailId(accountId);
            mailSender.setSubject(subject);
            mailSender.addTo(emailId);
            mailSender.addBcc("************");
            mailSender.buildMimeMessage();

}

public static sendEmail(String message, int accountId){
  mailSender.setHtmlMsg(getMailContent(message, accountId));
  mailSender.sendMimeMessage();
}

拨打buildEmail一次,然后您可以多次拨打sendEmail

答案 1 :(得分:0)

而不是调用调用

的send()
  • buildMimeMessage()
  • sendMimeMessage()

因此可以多次调用 sendMimeMessage() ,但我们应该调用或调用 buildMimeMessage() < / strong>只有一次。

您可以显式调用 buildMimeMessage() 一次,然后一次或多次调用 sendMimeMessage()