-->

Build.xml is failing to send email, gives “java.la

2019-07-29 04:03发布

问题:

Below is one of the targets that runs after the completion of tests, buil.xml(pass or fail). This target is failing and giving error "java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage". So I expanded the mail.1.4.jar file and saw this class was there. So next I checked that this jar was there in my setclasspath target or not, and it was there. Third thing I checked that mail.1.4.jar was included in ant's path or not and it was there. Now I am not able to understand why I am getting this error.

<target name="sendmail">
    <mail from="[myname@company.com]"
    subject="Test Email" mailhost="smpt.gmail.com" 
    user="myusername" password="mypassword" message="This is a test email">
        <to name="receivers name" address="[receiver@gmail.com]" />
    </mail>
</target>

回答1:

If Ant gives the “java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage” error, it means there is a jar missing.

Go to the findjar website: http://www.findjar.com/, and find the corresponding jar by searching "javax.mail.internet.MimeMessage". You'll find it's mail.jar.

Then download the jar file from the central Maven repository: https://mvnrepository.com/artifact/javax.mail/mail, for example, version mail-1.4.7.jar, and put it in your Ant lib directory: ${ANT_HOME}/lib

That should work.



回答2:

Add mail.jar in the classpath and related transitive dependencies.

Ok try the following, firstly in your ant installation, ${ANT_HOME}/lib check if the following jar is present, ant-javamail, if NOT:

You can find the following here: http://mvnrepository.com/artifact/org.apache.ant/ant-javamail

Pick the correct one according to your ant version.

Copy this jar in the lib directory of ant. And try to rerun the task.

Let know if it resolves.

Command to run

ant -f %ANT_HOME%/fetch.xml -Ddest=user -Dm2.url=repo1.maven.org/maven2


回答3:

I got the similar error from the 1st thread before. You probably want to download both mail.jar and activation.jar to be located under your ant library folder and when you executed the build, make sure you use "ant -lib" with the path to your lib folder.