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

此生再无相见时 提交于 2019-12-24 12:05:07

问题


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.




回答4:


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')
])


来源:https://stackoverflow.com/questions/16010897/build-xml-is-failing-to-send-email-gives-java-lang-classnotfoundexception-jav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!