【问题标题】:IllegalStateException when sending multipart mail from tomcat6从 tomcat6 发送多部分邮件时出现 IllegalStateException
【发布时间】:2012-03-23 21:09:04
【问题描述】:

我正在使用 mail plugin 从我的 grails 应用程序发送带有附件的多部分邮件。

在我的本地机器 (Mac OS X) 上一切正常。如果我将我的应用程序部署到 tomcat6(Ubuntu - )由于IllegalStateException,邮件无法发送:

Stacktrace follows: 
   java.lang.IllegalStateException: Not in multipart mode - 
   create an appropriate MimeMessageHelper via a constructor that takes a 'multipart' 
   flag if you need to set alternative texts or add inline elements or attachments. 
      at grails.plugin.mail.MailMessageBuilder.doAdd(MailMessageBuilder.groovy:347) 
      at grails.plugin.mail.MailMessageBuilder.attach(MailMessageBuilder.groovy:308) 
      at grails.plugin.mail.MailMessageBuilder.attach(MailMessageBuilder.groovy:284) 
      at grails.plugin.mail.MailMessageBuilder.attachBytes(MailMessageBuilder.groovy:280)
      ...

简单的邮件(不是多部分的)可以从 tomat6 成功发送。

这是我发送多部分邮件的代码:

mailService.sendMail {
   multipart true
   to mail
   subject mySubject
   body (view: myView, model: myModel)
   attachBytes "${myTitle}.pdf", CH.config.grails.mime.types['pdf'], myBytes
} 

我可以做些什么来避免这些异常?

底层 JavaMail 库位于何处?它被打包到war文件中了吗?

如何找出我的 tomcat6 和本地机器上使用的 JavaMail 版本?

【问题讨论】:

    标签: java grails ubuntu tomcat6 jakarta-mail


    【解决方案1】:

    我找到了问题的根源——这是我自己犯的一个愚蠢的错误。

    mailService的调用被封装到另一个服务中,添加一个默认的bcc,方法如下:

    def sendWithBcc(Closure callable) {
        // build a wrapper closure around the standard mail closure, which adds BCC functionality
        def newMailClosure = {
            if(CH.config.extraMail.bcc) {
                bcc(CH.config.extraMail.bcc)
            }
    
            // set the delegate of the inner closure to the delegate of this closure and call the inner closure
            callable.delegate = delegate
            callable.resolveStrategy = Closure.DELEGATE_FIRST
            callable.call()
        }
    
        return mailService.sendMail(newMailClosure)
    }
    

    在我的本地机器上一切正常,因为我没有指定extraMail.bcc

    就在设置bcc的那一刻,外部闭包的multipart属性似乎在MailMessageBuilder中被忽略了。

    解决方法是改变bcc的位置,如下图:

    def sendWithBcc(Closure callable) {
        // build a wrapper closure around the standard mail closure, which adds BCC functionality
        def newMailClosure = {            
            // set the delegate of the inner closure to the delegate of this closure and call the inner closure
            callable.delegate = delegate
            callable.resolveStrategy = Closure.DELEGATE_FIRST
            callable.call()
    
            if(CH.config.extraMail.bcc) {
                bcc(CH.config.extraMail.bcc)
            }
        }
    
        return mailService.sendMail(newMailClosure)
    }
    

    真丢脸 - 下次我会发布更精确的代码。

    【讨论】:

      猜你喜欢
      • 2010-11-10
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 2015-09-13
      • 2020-03-12
      相关资源
      最近更新 更多