How to send email by using ant.mail

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:'', mailport:'25', subject:'test email') {
  from (address:'')
  to (address:'')
  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.

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

buildscript {

repositories { … }

dependencies {

classpath …


} </code

Thank you Peter! I’ll try it.

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

apply from: 'credentials.gradle'
  repositories { mavenCentral() }
  // from
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 =
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:

Or also with groovy grapes here:

Edit: I put the groovy solution here:


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:


Could not find javax.activation:activation:1.1.1.

Required by:


Could not find javax.mail:mail:1.4.7.

Required by:


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

Regards, Sam

This means, that this line:

repositories { mavenCentral() }

does not work. Are you behind a proxy, so that you can’t reach

More about mavenCentral() here: