Sending Mails Using Ant MailTask FDT/Eclipse

I always wanted to make use of the “Post compile ant file” from Eclipse.
After hours of searching, trial and (mostly) error i finally got it working.
The Ant-MailTask sends a mail from within Eclipse including attachment.

The downside is that i was unable to get this running with my default Ant installation (v.1.7.1) and had to install a newer version of Ant.

I dont want to bore you with details of my odyssey but show you how it gets done in more or less four steps.

First check the version of your current Ant installation by typing
ant -version in your Terminal which should give you something like:

Apache Ant version 1.8.1 compiled on April 30 2010

If you have the version 1.8.1 installed already you can obviously skip the first step
as well as step three.

Here we go:

Step 1.)
Download the latest Ant version (1.8.1 at the time of writing) and put it wherever you like. As i’m on OS X i put it into /usr/local/apache-ant-1.8.1/

Step 2.)
Next step is to download and extract some additional JAR files.
You’ll need the JavaMail API and the NetBeans Activation Framework.

Extract and copy these files into either ~/.ant/lib/ or into the lib folder of your
Ant installation directory. (To save you some time, i’ve put these files into a zip at the end of the article. There may changes or updates over time but for now they should do the job)

Step 3.)
The final step is to create or update the alias to the Ant binary. Open the Terminal again and use the following command.

sudo ln -s /usr/local/apache-ant-1.8.1/bin/ant /usr/bin/ant

(fit the paths to your needs if you’ve choosen a different directory)

If you got an error message, rename the current alias in /usr/bin to e.g. ant-1.7.1

Now the alias should point to your new Ant installation and ant -version should give you: Apache Ant version 1.8.1 compiled on April 30 2010.

If you want to be the previous Ant version your default again, rename your ant-1.7.1 to ant or create a new alias using the following command in the terminal.

sudo ln -s /usr/share/ant/bin/ant /usr/bin/ant

Step 4.)
The last step is to make the downloaded JAR available in the Eclipse environment.
All you need to do is go to:

Preferences > Ant > Runtime > Classpath > Add External JARs

and select the downloaded JAR files.

Thats pretty much all you have to do for preparation.
Now let’s see how an ant-target which makes usage of the MailTask could look like.

<target name="send.mail"
        description="sends a notication mail" >   
  <property name="" value="" />
  <property name="mail.port" value="25" />
  <property name="mail.user" value="" />
  <property name="mail.pass" value="mysecretpassword" />
  <property name="mail.from" value="" />
  <property name=""   value="" />
  <property name="subject" 
            value="Test Message sent via Ant MailTask" />
  <property name="message" 
            value="Hello World, i was sent by the Ant MailTask" />
    failonerror = "true"
    user      = "${mail.user}"
    password  = "${mail.pass}"
    subject   = "${subject}" >
    <from address     = "${mail.from}"/>
    <to address       = "${}"/>
    <fileset dir="${basedir}">
      <include name="*.zip"/>
    <format property="send.time" 
            pattern="MM/dd/yyyy hh:mm aa" 
  <echo message="Mail successfully sent at ${send.time}" />

I think it is pretty self-explanatory. This would send an Email with the subject: “Test Message sent via Ant MailTask” from “” to “” with “Hello World, i was sent by the Ant MailTask” as message and all zip-files from the basedir as attachment.
So far so good.

Put this task into a build.xml (setup a properties file to make it more generic) and provide the appropriate credentials for your mail provider. Save it in your project directory or update your existing build file and run the following command in the Terminal:

$ cd path/to/project
$ ant send.mail

If everything worked as expected the output should look like this:

[mail] Sending email: Hello World, i was sent by the Ant MailTask
[mail] Sent email with 1 attachments
[echo] Mail successfully sent at 11/28/2010 04:17 PM

Total time: 1 second

Now insert the path to your build.xml in the Ant Task tab in your launch-configuration dialog and you’re done. :)

I use it now (in a more fine-grained setup) e.g. in the launch-configuration for my ‘release-build’ to notify a customer about new versions, updates and so on.

Hopefully you’ve found some useful informations in this post. Thanks for reading and feel free to leave a comment or share it if you like.


Download: Ant Mailtask JAR Files


This entry was posted in experiments, howto and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">