Build.xml 无法发送电子邮件,给出“java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16010897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Build.xml is failing to send email, gives "java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage" error
提问by user2172723
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.
下面是测试完成后运行的目标之一,buil.xml(通过或失败)。此目标失败并给出错误“java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage”。所以我展开了mail.1.4.jar 文件,看到这个类在那里。所以接下来我检查了这个 jar 是否在我的 setclasspath 目标中,它就在那里。第三件事,我检查了 mail.1.4.jar 是否包含在 ant 的路径中,它就在那里。现在我不明白为什么我会收到这个错误。
<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>
回答by Yuci
If Ant gives the “java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage” error, it means there is a jar missing.
如果 Ant 给出“java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage”错误,则意味着缺少 jar。
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.
进入findjar网站:http: //www.findjar.com/,通过搜索“javax.mail.internet.MimeMessage”找到对应的jar。你会发现它是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
然后从 Maven 中央存储库下载 jar 文件:https: //mvnrepository.com/artifact/javax.mail/mail ,例如版本 mail-1.4.7.jar,并将其放在您的 Ant lib 目录中: ${ ANT_HOME}/lib
That should work.
那应该有效。
回答by rednoah
If you're using Ant via Groovy then you can add the necessary libraries via @Grab
like so:
如果您通过 Groovy 使用 Ant,那么您可以通过@Grab
如下方式添加必要的库:
@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')
])
回答by Himanshu Bhardwaj
Add mail.jar in the classpath and related transitive dependencies.
在类路径中添加 mail.jar 和相关的传递依赖项。
Ok try the following, firstly in your ant installation, ${ANT_HOME}/lib check if the following jar is present, ant-javamail, if NOT:
好的,请尝试以下操作,首先在您的 ant 安装中,${ANT_HOME}/lib 检查是否存在以下 jar,ant-javamail,如果不存在:
You can find the following here: http://mvnrepository.com/artifact/org.apache.ant/ant-javamail
您可以在此处找到以下内容: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.
把这个jar复制到ant的lib目录下。并尝试重新运行任务。
Let know if it resolves.
让知道它是否解决。
Command to run
运行命令
ant -f %ANT_HOME%/fetch.xml -Ddest=user -Dm2.url=repo1.maven.org/maven2
回答by juniorb
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.
我之前从第一个线程中得到了类似的错误。您可能希望将 mail.jar 和 activation.jar 都下载到您的 ant 库文件夹下,并且在执行构建时,请确保使用带有 lib 文件夹路径的“ant -lib”。