title: Running ant-target to send a mail via java
body: <p>I´m a pretty new to ant and I´m trying to send an email within an ant-target which is called by Java. I´m using the Netbeans IDE.</p>
  
  <p>Ant:</p>
  
  <pre><code>&lt;project name="AntTargets" default="main" basedir="."&gt;
  &lt;description&gt;Builds, tests, and runs the project AntTargets.&lt;/description&gt;
  &lt;import file="nbproject/build-impl.xml"/&gt;
  
  &lt;target name="run" depends="jar"&gt;
      &lt;java classpathref="AntTargets.classpath" fork="true" classname="Client" /&gt;
  &lt;/target&gt;
  
  &lt;target name="main"&gt;
      &lt;echo message="******************[MAIN]******************"/&gt;
      &lt;property file="nbproject/build.properties"/&gt;
      &lt;property name="to" value="${builder}" /&gt;
      &lt;property name="from" value="${builder}" /&gt;
      &lt;property name="server" value="${server}" /&gt;
      &lt;property name="port" value="${port}" /&gt;
  
      &lt;mail mailhost="${server}" mailport="${port}" subject="Test build"&gt;
          &lt;from address="${from}"/&gt;
          &lt;replyto address="${to}"/&gt;
          &lt;to address="test@domain.de"/&gt;
          &lt;message&gt;The build has been completed&lt;/message&gt;
      &lt;/mail&gt;
  
      &lt;echo message="Email to ${to} from ${from} has been sent using the server ${server} on port ${port}."/&gt;
  &lt;/target&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>Java:</p>
  
  <pre><code>import java.io.File;
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.ProjectHelper;
  
  public class AntTargets {
  
  public static void main(String[] args) {
      //get new file
      File buildFile = new File("build.xml");
  
      //create new project
      Project p = new Project();
      p.setUserProperty("ant.file", buildFile.getAbsolutePath());
      p.init();
  
      //initialize helper
      ProjectHelper helper = ProjectHelper.getProjectHelper();
      p.addReference("ant.projectHelper", helper);
  
      //parse and execute
      helper.parse(p, buildFile);
      p.executeTarget(p.getDefaultTarget());
  }   
  }
  </code></pre>
  
  <p>When I execute the ant part directly, the properties file is found, read and works overall but the mail-part gives me this error: </p>
  
  <pre><code>Failed to send email: javax.mail.internet.MimeMessage
  build.xml:16: 
  java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage
  </code></pre>
  
  <p>When I call it with java I get:</p>
  
  <pre><code>Reference AntTargets.classpath not found.
  BUILD FAILED (total time: 0 seconds)
  </code></pre>
  
  <p>I would greatly appreciate any help and tip overall.</p>