Apache commons email 使用过程中遇到的问题
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 {
@Test
public 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)
带图片的
@Test
public 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的
@Test
public 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 {
@Test
public 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 id
URL 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;)V
at 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 使用过程中遇到的问题的更多相关文章
- 使用Apache Commons Email 发生邮件
Apache Commons Email的Maven依赖 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-e ...
- 使用Apache commons email发送邮件
今天研究了以下怎么用java代码发送邮件,用的是Apache的commons-email包. 据说这个包是对javamail进行了封装,简化了操作. 这里讲一下具体用法吧 一.首先你需要有邮箱账号和一 ...
- Apache Commons Email 使用网易企业邮箱发送邮件
最近使用HtmlEmail 发送邮件,使用网易企业邮箱,发送邮件,死活发不出去!原以为是网易企业邮箱,不支持发送邮箱,后面经过研究发现,是apache htmlEmail 的协议导致,apache E ...
- org.apache.commons.lang3.StringUtils类中isBlank和isEmpty方法的区别
相信很多java程序员在写代码的时候遇到判断某字符串是否为空的时候会用到StringUtils类中isBlank和isEmpty方法,这两个方法到底有什么区别呢?我们用一段代码来阐述这个区别吧: @T ...
- 一篇关于apache commons类库的详解
1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默默无闻的.在我看来,成功而默默无闻的那些框架值得我们格外的尊敬和关注,Jakarta C ...
- 一篇关于apache commons类库的详解[转]
1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默默无闻的.在我看来,成功而默默无闻的那些框架值得我们格外的尊敬和关注,Jakarta C ...
- apache commons类库的学习
原文地址http://www.tuicool.com/articles/iyEbquE 1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默 ...
- ysoserial分析【一】 之 Apache Commons Collections
目录 前言 基础知识 Transformer 利用InvokerTransformer造成命令执行 Map TransformedMap LazyMap AnnotationInvocationHan ...
- apache commons io包基本功能
1. http://jackyrong.iteye.com/blog/2153812 2. http://www.javacodegeeks.com/2014/10/apache-commons-io ...
随机推荐
- Redis事务管理
用过其他关系型数据库(比如msql)的肯定都指定,在关系型数据库里面的事务可以保证多个命令操作要么同时成功,要么同时失败.并且在执行事务的时候,可以有隔离级别. 但是在Redis中的事务,只是保证事务 ...
- 教你从手机中提取system镜像制作线刷救砖包的简单方法
其实在制作刷机包的过程中,有时候没有官方或者第三方提供的救砖包(线刷),那怎么办?常规的方法有两种:(此处为常规方法,回读的方式暂不说明) 1.卡刷包转线刷包 2.dd命令导出分区镜像 ...
- Evensgn 的债务
问题 A: Evensgn 的债务 大致题意:a欠b5元,b欠c5元,那么最小债务总额为a欠c5元,给你关系,求最小债务总额! 不想说话...一句超级大水题,我居然没读懂!!差点想到网络流了...其实 ...
- 【TCP网络协议问题】
题目描述 在如今的网络中,TCP 是一种被广泛使用的网络协议,它在传输层提供了可靠的通信服务.众所周知,网络是存在时延的,例如用户先后向服务器发送了两个指令 op1 和 op2,并且希望服务器先处理指 ...
- bzoj 2734: [HNOI2012]集合选数
题目描述 <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中. 同学们不喜 ...
- NFC Spy:基于Android 4.4及以上手机的非接智能卡跟踪仪
NFC Spy 用来查看读卡器和智能卡之间的指令.数据的交互传输过程,以便 NFC/HCE 开发者分析研究底层通讯协议,定位错误指令. 本程序要使用两部带有 NFC 硬件的 Android 手机,并且 ...
- 使用JdbcTemplate过程中使用到多个参数和like模糊
项目中经常会用到模糊查询,最近使用JdbcTemplate过程中就遇到了. 一开始尝试了拼接的方式去 String sql = "select count(1) from web_users ...
- PTA 银行排队问题之单队列多窗口服务
假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选择时,假设顾客总是选择编号最小的窗口. 本题要求输出 ...
- VS生成项目时,有些文件无法复制到输出目录的解决办法
有时候,我们在生成项目时,发现有些文件如:.jpg的图片文件,无法复制到输出目录中,此时会非常纠结,反复的清理项目,重新生成,依旧不能解决此问题.后来我打开.csproj的项目工程文件时,经过对比发现 ...
- React 关于组件(界面)更新
在最近在学 React , 将组件的UI更新稍微整理了一下.根据业务要求,可能会出现如下的技术实现要求:1.更新自己2.更新子组件3.更新兄弟组件4.更新父组件5.父 call 子 function ...