grails / grails-mail

The Grails Mail Plugin
https://grails.github.io/grails-mail/
Apache License 2.0
14 stars 25 forks source link

IllegalStateException: Not in multipart mode #28

Closed chris13524 closed 6 years ago

chris13524 commented 6 years ago

When running this code:

sendMail {
    async true
    from "Chris <my.email@email.com>
    bcc "other.email@email.com"
    to "recipient@email.com"
    subject "subject"
    html "<!doctype html><html><body>Hello Customer,...</body></html>"
    text "Hello Customer,..."
}

I get this exception:

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 org.springframework.mail.javamail.MimeMessageHelper.checkMultipart(MimeMessageHelper.java:383)
        at org.springframework.mail.javamail.MimeMessageHelper.getMimeMultipart(MimeMessageHelper.java:415)
        at org.springframework.mail.javamail.MimeMessageHelper.getMainPart(MimeMessageHelper.java:827)
        at org.springframework.mail.javamail.MimeMessageHelper.setText(MimeMessageHelper.java:813)
        at grails.plugins.mail.MailMessageBuilder.finishMessage(MailMessageBuilder.groovy:426)
        at grails.plugins.mail.MailMessageBuilder.sendMessage(MailMessageBuilder.groovy:105)
        at grails.plugins.mail.MailService.sendMail(MailService.groovy:53)
        at grails.plugins.mail.MailService.sendMail(MailService.groovy:57)
        at grails.plugins.mail.SendMail$Trait$Helper.sendMail(SendMail.groovy:31)
        at my.app.send(MyMailSender.groovy:123)

I tried setting multipart true, but that didn't seem to help.

I'm on the latest version, v2.0.0.RC6.

chris13524 commented 6 years ago

Same issue on v2.0.0. Any ideas?

chris13524 commented 6 years ago

Figured it out. Looks like I needed to add multipart true to the top of my parameters list. This is because from, bcc, to, subject, html, and text will all construct the message object. You need to enable multipart before you construct the message.

sendMail {
    async true
    multipart true
    from "Chris <my.email@email.com>
    bcc "other.email@email.com"
    to "recipient@email.com"
    subject "subject"
    html "<!doctype html><html><body>Hello Customer,...</body></html>"
    text "Hello Customer,..."
}