apache-commons-email是对mail的一个封装,所以使用起来确实是很方便。特别的,官网上的tutorial也是极其的简单。但是我也仍然是遇到了没有解决的问题。

jar包的添加

  • mail.jar && activation
  • apache-commons-email.jar

一开始我没有添加上面的mail.jar ,然后就导致在编码的过程中,各种报错。

SimpleEmail实例

package email;import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;
import org.junit.Test;public class SimpleEmailTest {@Testpublic void simple() throws Exception {final String HOSTNAME = "smtp.163.com";try {Email email = new SimpleEmail();email.setHostName(HOSTNAME);// email.setSmtpPort(465);email.setAuthenticator(new DefaultAuthenticator("15640SSS27", "XXXXXXX"));email.setSSLOnConnect(true);email.setFrom("1SSSSSS27@163.com");email.setSubject("Test Mail By Commons-Emial");email.setMsg("Congratulations!\nYou have been admitted, so come here and join us ! :-)");email.addTo("106SSSSSS@qq.com");email.send();System.out.println("邮件已成功发送!");} catch (Exception e) {e.printStackTrace();}}}

带附件实例(图片和URL)

带图片的

@Testpublic void test() throws Exception {// 添加一个附件EmailAttachment attachment = new EmailAttachment();attachment.setPath("E:\\Code\\Java\\apache-commons-email\\src\\email\\be.png");attachment.setDisposition(EmailAttachment.ATTACHMENT);attachment.setDescription("one big beauty!");attachment.setName("beauty.png");// 实例化邮件MultiPartEmail email = new MultiPartEmail();email.setHostName("smtp.163.com");email.setAuthenticator(new DefaultAuthenticator("15   xxxx27", "gxuxxxxxxx4"));email.setSSLOnConnect(true);email.addTo("dsds632@qq.com");email.setFrom("15dsdsds027@163.com");email.setSubject("The Beauty Picture!");email.setMsg("Here is an email with a beauty!");// 把附件添加到邮件email.attach(attachment);// 发邮件email.send();System.out.println("邮件发送成功!");}

带URL的

@Testpublic void testURL() throws Exception {// 添加一个附件EmailAttachment attachment = new EmailAttachment();attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));attachment.setDisposition(EmailAttachment.ATTACHMENT);attachment.setDescription("Apache Logo!");attachment.setName("ApacheLogo");// 实例化邮件MultiPartEmail email = new MultiPartEmail();email.setHostName("smtp.163.com");email.setAuthenticator(new DefaultAuthenticator("15ssss7", "gssssss4"));email.setSSLOnConnect(true);email.addTo("10ssdsds@qq.com");email.setFrom("15dsdsdsdsds@163.com");email.setSubject("The Beauty Picture!");email.setMsg("Here is an email with a beauty!");// 把附件添加到邮件email.attach(attachment);// 发邮件email.send();System.out.println("邮件发送成功!");}

下面的是嵌入数据,但是却没能成功

package email;import java.net.URL;import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.HtmlEmail;
import org.junit.Test;public class WithHtmlTest {@Testpublic void sendHTMLFormattedEmail() throws Exception {try {// 实例化邮件HtmlEmail email = new HtmlEmail();email.setHostName("smtp.163.com");email.setAuthentication("1dsadsadsa27@163.com", "gdsadsaddsadsd");email.setSSLOnConnect(true);email.setSSL(true);email.addTo("1adas2@qq.com", "小郭");email.setFrom("156dsadas@163.com", "Me");email.setSubject("Test email with inline image");// embed the image and get the content idURL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");String cid = email.embed(url, "Apache Logo!");// 设置html的内容email.setHtmlMsg("<html>The apache logo - <img src=\"cid:" + cid + "\"></html>");// 设置text的内容email.setTextMsg("Your email client doesn't support HTML messages!");// 发邮件email.send();} catch (Exception e) {e.printStackTrace();}}}

报错的信息如下:

java.lang.NoSuchMethodError: javax.mail.internet.MimeBodyPart.setText(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Vat org.apache.commons.mail.HtmlEmail.build(HtmlEmail.java:586)at org.apache.commons.mail.HtmlEmail.buildMimeMessage(HtmlEmail.java:510)at org.apache.commons.mail.Email.send(Email.java:1447)at email.WithHtmlTest.sendHTMLFormattedEmail(WithHtmlTest.java:35)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

如果你也遇到了这个问题,而且解决了。欢迎留言!我会及时的来修改博客的!

Apache commons email 使用过程中遇到的问题相关推荐

  1. JavaMail| Apache Commons Email介绍

    Apache Commons Email简介 Apache Commons Email旨在提供一个用于发送电子邮件的API.它建立在JavaMail API之上,旨在简化它. 官网: http://c ...

  2. Apache Commons Email 怎样设置接收方发送已读回执?

    Apache Commons Email 怎样设置接收方发送已读回执? 在给别人发邮件时,怎么知道对方是否收到? 类似126/163邮箱里面,发信时 勾选 "[ ] 已读回执" 对 ...

  3. apachecommon连接mysql_使用Apache Commons的DBUtils工具包中的,QueryRunner查询数据库返回结果错误...

    写了一个最简单的测试,就是使用QueryRunner查询一个表中的所有记录,并保存在JavaBean类对象里. 问题如下:查询到的id全为0,使用普通sql查询是正确的:而且使用QueryRunner ...

  4. c++解析csv 存入数组_使用Apache Commons CSV在Java中读写CSV

    介绍 这是专门针对Java读写CSV的库的简短系列文章的第二篇,也是上一篇文章" Core Java读写CSV"的直接续篇. Apache Commons CSV 在Apache的 ...

  5. java 用Apache Commons Email发邮件

    所需jar包:commons-email-1.4.jar和mail-1.4.1jar SimpleEmail 用于发送普通文本邮件 package liu.email;import org.apach ...

  6. guava和commons_使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在...

    guava和commons 最近Reddit上的帖子提出了一个问题:" 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数? "基于问题的标题也被以另一种方式问到,&quo ...

  7. 使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在

    最近Reddit上的帖子提出了一个问题:" 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数? "基于问题的标题也被以另一种方式问到,"一种检查变量是否包含诸如 ...

  8. 一篇关于apache commons类库的详解

    1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默默无闻的.在我看来,成功而默默无闻的那些框架值得我们格外的尊敬和关注,Jakarta C ...

  9. [转]Apache Commons IO入门教程

    Apache Commons IO是Apache基金会创建并维护的Java函数库.它提供了许多类使得开发者的常见任务变得简单,同时减少重复(boiler-plate)代码,这些代码可能遍布于每个独立的 ...

最新文章

  1. PAT 1015__部分正确__已解决
  2. javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
  3. python 字符串中使用制表符('\t')(或换行符('\n'))
  4. 课程设计---约瑟夫环
  5. 是什么在主宰着我们自身的行动?
  6. CGCS2000 VS WGS84
  7. 基于人机环境系统工程的智慧企业建设思考(2)
  8. 零基础学SQL(一、数据库与SQL简介)
  9. 制冷与空调设备运行操作作业题库(含答案)
  10. 国内可以使用的英文搜索引擎
  11. 在线epub转txt格式如何转换
  12. 火山pc实现找图找色模块
  13. Linux通过LVM新增一个物理卷(硬盘),实现多个硬盘合并到一个逻辑硬盘
  14. Improved autoencoder for unsupervised anomaly detection
  15. C++ 应用程序无法正常启动0xc000007b
  16. 第10 章 量化的项目管理
  17. SQL小挑战——第3期(电影院选择连续座位)
  18. jQuery入门(一)--jQuery中的选择器
  19. 一步一步安装 Microsoft .NET Pet Shop 4.0
  20. Excle中如何快速筛选数据

热门文章

  1. iOS网络开发中的同步、异步和请求队列
  2. maven项目的构建命令
  3. 正确设置语言,加速WP应用提交
  4. SpringMVC+Hibernate+Junit4+json基本框架近乎0配置
  5. setting.xml的mirror、mirrorOf和pom.xml的repositories、repository的关系关联*
  6. SQL SERVER 2008权限配置
  7. vue中的v-on事件监听机制
  8. 阵列卡缓存电池充放电问题详解
  9. openmediavault 4.1.3 插件开发
  10. iOS性能分析-Xcode Instruments Allocations 分析APP内存使用情况