Problem sending mail from a build script


(Adrian Abraham) #1

I need to send an email from my build script. My code works perfectly as a standalone program; however, when it’s in a build script, I get an exception.

Here’s the script:

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
  buildscript {
  repositories {
    ...
  }
    dependencies {
    classpath group: 'org.apache.commons', name: 'commons-email', version: '1.0'
    classpath 'javax.mail:mail:1.4.3'
    classpath 'javax.activation:activation:1.1.1'
    classpath 'com.sun.mail:smtp:1.3'
  }
}
  task sendEmail() {
    EmailAttachment attachment = new EmailAttachment()
    attachment.setPath(buildDir.getPath() + "/distributions/attachment.tar.bz2")
    attachment.setDisposition(EmailAttachment.ATTACHMENT)
    attachment.setDescription("attachment.tar.bz2")
    attachment.setName("attachment.tar.bz2")
          MultiPartEmail mpe = new MultiPartEmail()
    mpe.setHostName("SNIP")
    mpe.addTo("SNIP")
    mpe.setFrom("SNIP")
    mpe.setSubject("Test Email")
    mpe.setMsg("Test")
    mpe.attach(attachment)
    mpe.send()
}

Here’s the last bin of the exception:

Caused by: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
 javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_0_1295168.1330979754288"
 at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:930)
 at org.apache.commons.mail.Email.sendMimeMessage(Email.java:863)
 ... 77 more
Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_0_1295168.1330979754288"
 at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:888)
 ... 78 more

(Adrian Abraham) #2

This seems to be a strange classloader issue: http://forum.springsource.org/archive/index.php/t-69180.html.


(Adrian Abraham) #3

Well, I’ve got a workaround - I just hope it doesn’t break other things in strange ways …

I forget if I found it on that post or on another one, but I ended up wrapping my code with:

def oldClassLoader = Thread.currentThread().contextClassLoader
Thread.currentThread().contextClassLoader = getClass().getClassLoader()
  ... send email ...
  Thread.currentThread().contextClassLoader = oldClassLoader

(Luke Daley) #4

Thanks for the detective work. This may mean that we aren’t setting up the context class loader correctly.

I’ve created GRADLE-2150 to track the resolution to this. It may be the case that we are right here and the mail library is wrong, but it needs to be verified either way.

Cheers.


(Adam Murdoch) #5

Which version of Gradle are you using?

Can you try with the most recent 1.0-milestone-9 snapshot, as this does set the context class loader correctly?

Also, you should define your task like this (otherwise you’re sending the email when the build script executes, rather than when the task executes):

task sendEmail {
    doFirst {
        EmailAttachment attachment = new EmailAttachment()
        ....
    }
}

(Adrian Abraham) #6

I’m still on M6 (working to move to M8 now - some “tricks” I used don’t work so well any more).

The problem seems to be fixed in M8 (and in the M9 snapshot).

You’re right about the doFirst - my real code has it, but my example was a poor cut 'n paste.