我使用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 lib位于何处?它被打包成战争文件吗?
如何在tomcat6和本地计算机上找到使用哪个版本的JavaMail?
答案 0 :(得分:0)
我找到了问题的根源 - 这是我自己的一个愚蠢的错误。
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)
}
对我感到羞耻 - 下次我会发布更精确的代码。