Pushpraj Singh
Pushpraj Singh

Reputation: 61

Build.xml is failing to send email, gives "java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage" error

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="[[email protected]]"
    subject="Test Email" mailhost="smpt.gmail.com" 
    user="myusername" password="mypassword" message="This is a test email">
        <to name="receivers name" address="[[email protected]]" />
    </mail>
</target>

Upvotes: 1

Views: 11349

Answers (4)

rednoah
rednoah

Reputation: 1082

If you're using Ant via Groovy then you can add the necessary libraries via @Grab like so:

@GrabConfig(systemClassLoader=true)
@Grapes([
    @Grab(group='org.apache.ant', module='ant-javamail', version='1.10.7'),
    @Grab(group='com.sun.mail', module='javax.mail', version='1.6.2')
])

Upvotes: 0

Yuci
Yuci

Reputation: 30149

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.

Upvotes: 1

juniorb
juniorb

Reputation: 81

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.

Upvotes: 0

Himanshu Bhardwaj
Himanshu Bhardwaj

Reputation: 4123

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

Upvotes: 0

Related Questions