[GRADLE-2150] Problem sending mail from a build script Created: 06/Mar/12  Updated: 04/Jan/13  Resolved: 08/Mar/12

Status: Resolved
Project: Gradle
Affects Version/s: None
Fix Version/s: 1.0-milestone-8

Type: Bug
Reporter: Gradle Forums Assignee: Adam Murdoch
Resolution: Fixed Votes: 0


 Description   

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



 Comments   
Comment by Gradle Forums [ 06/Mar/12 ]

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

Comment by Gradle Forums [ 06/Mar/12 ]

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

Generated at Wed Jun 30 12:14:06 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.