作者:lanshan1126141 | 来源:互联网 | 2023-12-09 17:35
本文介绍了解决java开源项目apachecommonsemail简单使用报错的方法,包括使用正确的JAR包和正确的代码配置,以及相关参数的设置。详细介绍了如何使用apachecommonsemail发送邮件。
JAR包:commons-email-1.4.jar
代码:`
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public static void send(String subject, String msg) {
try {
System.out.println("从properites取参数");
ResourceBundle cOnfig= ResourceBundle.getBundle("email");
String hostName = config.getString("hostName");
String userName = config.getString("userName");
String password = config.getString("password");
String sender = config.getString("sender");
String reciever = config.getString("reciever");
SimpleEmail email = new SimpleEmail();
email.setHostName(hostName);
email.setAuthentication(userName, password);
email.setSSLOnConnect(true);
email.setFrom(sender);
email.setCharset("utf-8");
email.setSubject(subject);
email.setMsg(msg);
email.addTo(reciever);
System.out.println("开始发送");
email.send();
System.out.println("发送完毕");
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
send("主题","文本");
}` |
报错:`
1 2 3 4 5 6 7
| Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/BEncoderStream
at javax.mail.internet.MimeUtility.doEncode(MimeUtility.java:664)
at javax.mail.internet.MimeUtility.encodeWord(MimeUtility.java:643)
at javax.mail.internet.MimeUtility.encodeText(MimeUtility.java:444)
at javax.mail.internet.MimeMessage.setSubject(MimeMessage.java:793)
at org.apache.commons.mail.Email.buildMimeMessage(Email.java:1284)
at org.apache.commons.mail.Email.send(Email.java:1447) |
`
然后查资料说是和Myeclipse自带JAR包冲突,遂直接发布Tomcat中测试,还是报错:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| java.lang.NoClassDefFoundError: javax/mail/Message
at sun.reflect.GeneratedMethodAccessor91.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722) |
这两个错误不知道是不是相同的原因引起的,熟悉邮件发送的同学、高手、大神给解释一下原因并给出解决方案,感谢~~
错误已经解决,缺少了javamail-1.4.3.jar,因为commons-email项目是在javamail项目基础上开发的。另外,有同学说,javaee1.6下,内部存在javamail可以省略,但是我的项目是javaee1.5、tomcat6所以...
最后,发送人邮箱尽量不要用qq邮箱,一堆授权码都要搞晕了,推荐使用网易163、126的邮箱。使用时,先进邮箱界面在设置中开启smtp服务,并且查看相应(smtp)服务器地址,126服务器地址为smtp.126.com。
非大神,勿喷