How to send email by using ant.mail


(boseevr) #1

Hi all, I am new to Gradle & Groovy. I can send an email from an ant script, but cannot figure out how to do the same thing in Gradle. Below is my Gradle script:

task sendMail << {
 ant.mail (mailhost:'smtp.host.com', mailport:'25', subject:'test email') {
  from (address:'aaa@host.com')
  to (address:'bbb@host.com')
  message ('message content here')
 }
}

When run “gradle sendMail”, the build is failed and the error is:

> java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage

I have copy the javax.mail.jar to the GRADLE_HOME/lib, my gradle version is 1.7, and jvm version is 1.7.0_15.

Thank in advance.


(Peter Niederwieser) #2

Try to add the JavaMail implementation Jar to the build script class path:

buildscript {

repositories { … }

dependencies {

classpath …

}

} </code


(boseevr) #3

Thank you Peter! I’ll try it.


(elsamuko) #4

Hey ho, this is how you send mails with ant:

apply from: 'credentials.gradle'
  repositories { mavenCentral() }
  // from
// http://forums.gradle.org/gradle/topics/unable_to_use_ant_junitreport_task_even_though_ant_junit_jar_is_present#reply_8805529
configurations { antClasspath }
dependencies {
 antClasspath 'ant:ant-javamail:1.6.5'
 antClasspath 'javax.activation:activation:1.1.1'
 antClasspath 'javax.mail:mail:1.4.7'
}
  ClassLoader antClassLoader = org.apache.tools.ant.Project.class.classLoader
configurations.antClasspath.each { File jar ->
 println "Adding to ant classpath : " + jar.absolutePath
 antClassLoader.addURL( jar.toURI().toURL() )
}
  task sendMail << {
   def mailParams = [
  mailhost: project.ext.mailHost,
  subject: "subject",
  messagemimetype: "text/plain",
  user: project.ext.mailUser,
  password: project.ext.mailPassword
 ]
   ant.mail( mailParams ) {
  from( address:project.ext.sender )
  to( address:project.ext.receiver )
  message( "So far, so good" )
 }
}

A similar case was handled here: http://forums.gradle.org/gradle/topics/unable_to_use_ant_junitreport_task_even_though_ant_junit_jar_is_present#reply_8805529

Or also with groovy grapes here: http://stackoverflow.com/questions/1641116/groovy-with-grape-and-antbuilder-classloader-problem

Edit: I put the groovy solution here: http://fd-imaging.com/sending-an-email-with-groovy-ant-and-gmail/


(Sampath Kambar) #5

Hi,

When I run this script, it is showing the following error.

  • What went wrong: A problem occurred evaluating root project ‘gradle-download-task-master’. > Could not resolve all dependencies for configuration ‘:antClasspath’.

Could not find ant:ant-javamail:1.6.5.

Required by:

:gradle-download-task-master:4.0

Could not find javax.activation:activation:1.1.1.

Required by:

:gradle-download-task-master:4.0

Could not find javax.mail:mail:1.4.7.

Required by:

:gradle-download-task-master:4.0

Can any body tell me where I am going wrong? Any pointers would be helpful.

Regards, Sam


(elsamuko) #6

This means, that this line:

repositories { mavenCentral() }

does not work. Are you behind a proxy, so that you can’t reach http://repo1.maven.org/maven2/?

More about mavenCentral() here: http://www.gradle.org/docs/current/javadoc/org/gradle/api/artifacts/dsl/RepositoryHandler.html#mavenCentral()