I’m sending multipart mails with attachment from my grails application using the mail plugin.
On my local machine (Mac OS X) all works fine. If I deploy my app to tomcat6 (Ubuntu – ) the mail could not be sent due to an 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)
...
Simple mails (not multipart) could be sent from tomat6 successfully.
Here is my code for sending multipart mails:
mailService.sendMail {
multipart true
to mail
subject mySubject
body (view: myView, model: myModel)
attachBytes "${myTitle}.pdf", CH.config.grails.mime.types['pdf'], myBytes
}
What can I do to avoid these Exception?
Where is the underlying JavaMail lib located? Is it packed into war file?
How can I find out which version of JavaMail is used within my tomcat6 and on my local machine?
I found the source of the problem – it was a stupid mistake of my own.
The call of the
mailServicewas encapsulated into another service to add a defaultbccin the following way:On my local machine all works fine because I havn’t specified
extraMail.bcc.Just in the moment when the
bccis set, the multipart property of the outer closure seems to be ignored within theMailMessageBuilder.The solution was to change the position of the
bccas shown below:Shame on me – next time I will post more precise code.