# Problem sending mail from a build script

**URL:** <https://discuss.gradle.org/t/problem-sending-mail-from-a-build-script/1993>\
**Category:** Old Forum Archive\
**Created:** [March 5, 2012, 8:42pm UTC](https://discuss.gradle.org/t/problem-sending-mail-from-a-build-script/1993 "2012-03-05T20:42:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrian](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@Adrian](https://discuss.gradle.org/u/Adrian)\
**Post date:** [March 5, 2012, 8:42pm UTC](https://discuss.gradle.org/t/problem-sending-mail-from-a-build-script/1993/1 "2012-03-05T20:42:00Z")

</div>

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:

```gradle
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:

```gradle
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

```

---

<div class="post-metadata">

**Author:** ![Adrian](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@Adrian](https://discuss.gradle.org/u/Adrian)\
**Post date:** [March 6, 2012, 1:09pm UTC](https://discuss.gradle.org/t/problem-sending-mail-from-a-build-script/1993/2 "2012-03-06T13:09:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Adrian](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@Adrian](https://discuss.gradle.org/u/Adrian)\
**Post date:** [March 6, 2012, 1:36pm UTC](https://discuss.gradle.org/t/problem-sending-mail-from-a-build-script/1993/3 "2012-03-06T13:36:00Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [March 6, 2012, 3:14pm UTC](https://discuss.gradle.org/t/problem-sending-mail-from-a-build-script/1993/4 "2012-03-06T15:14:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adammurdoch](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/adammurdoch/32/17_2.png) [@adammurdoch](https://discuss.gradle.org/u/adammurdoch)\
**Post date:** [March 6, 2012, 7:52pm UTC](https://discuss.gradle.org/t/problem-sending-mail-from-a-build-script/1993/5 "2012-03-06T19:52:00Z")

</div>

Which version of Gradle are you using?

Can you try with the most recent [1.0-milestone-9 snapshot](http://gradle.org/release-candidate), 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):

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

```

---

<div class="post-metadata">

**Author:** ![Adrian](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@Adrian](https://discuss.gradle.org/u/Adrian)\
**Post date:** [March 6, 2012, 8:05pm UTC](https://discuss.gradle.org/t/problem-sending-mail-from-a-build-script/1993/6 "2012-03-06T20:05:00Z")

</div>

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.
