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 使用过程中遇到的问题的更多相关文章

  1. 使用Apache Commons Email 发生邮件

    Apache Commons Email的Maven依赖 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-e ...

  2. 使用Apache commons email发送邮件

    今天研究了以下怎么用java代码发送邮件,用的是Apache的commons-email包. 据说这个包是对javamail进行了封装,简化了操作. 这里讲一下具体用法吧 一.首先你需要有邮箱账号和一 ...

  3. Apache Commons Email 使用网易企业邮箱发送邮件

    最近使用HtmlEmail 发送邮件,使用网易企业邮箱,发送邮件,死活发不出去!原以为是网易企业邮箱,不支持发送邮箱,后面经过研究发现,是apache htmlEmail 的协议导致,apache E ...

  4. org.apache.commons.lang3.StringUtils类中isBlank和isEmpty方法的区别

    相信很多java程序员在写代码的时候遇到判断某字符串是否为空的时候会用到StringUtils类中isBlank和isEmpty方法,这两个方法到底有什么区别呢?我们用一段代码来阐述这个区别吧: @T ...

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

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

  6. 一篇关于apache commons类库的详解[转]

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

  7. apache commons类库的学习

    原文地址http://www.tuicool.com/articles/iyEbquE 1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默 ...

  8. ysoserial分析【一】 之 Apache Commons Collections

    目录 前言 基础知识 Transformer 利用InvokerTransformer造成命令执行 Map TransformedMap LazyMap AnnotationInvocationHan ...

  9. apache commons io包基本功能

    1. http://jackyrong.iteye.com/blog/2153812 2. http://www.javacodegeeks.com/2014/10/apache-commons-io ...

随机推荐

  1. TensorFlow学习笔记(MNIST报错修正 适用Tensorflow1.3)

    在Tensorflow实战Google框架下的深度学习这本书的MNIST的图像识别例子中,每次都要报错   错误如下: Only call `sparse_softmax_cross_entropy_ ...

  2. [HNOI2014]世界树

    题目描述 世界树是一棵无比巨大的树,它伸出的枝干构成了整个世界.在这里,生存着各种各样的种族和生灵,他们共同信奉着绝对公正公平的女神艾莉森,在他们的信条里,公平是使世界树能够生生不息.持续运转的根本基 ...

  3. bzoj 4945: [Noi2017]游戏

    Description Solution 首先我们发现一个位置如果不是 \('x'\),那么就只有两种选择 而 \('x'\) 的个数小于等于 \(8\),直接枚举是哪个就好了 然后就是 \(2-sa ...

  4. 【tyvj】刷题记录(1001~1099)(64/99)

    1001:排序完按照题意做即可. #include<cstdio> #include<iostream> #include<cmath> #include<a ...

  5. ●BZOJ 3963 [WF2011]MachineWorks

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3963 题解: 斜率优化DP,CDQ分治. 先按时间排序.(规定以下内容的第i台机器的卖出时间 ...

  6. ●BZOJ 4518 [Sdoi2016]征途

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4518 题解: 斜率优化DP 首先看看最后答案的形式: 设a[i]为第i天走的距离,那么 $A ...

  7. 基于vfs实现自己的文件系统

    1.Linux 文件系统组成结构 linux文件系统有两个重要的特点:一个是文件系统抽象出了一个通用文件表示层--虚拟文件系统或称做VFS.另外一个重要特点就是它的文件系统支持动态安装(或说挂载等), ...

  8. Codeforces Round #401 (Div. 2)

    和FallDream dalao一起从学长那借了个小号打Div2,他切ABE我做CD,我这里就写下CD题解,剩下的戳这里 AC:All Rank:33 小号Rating:1539+217->17 ...

  9. [bzoj4864][BeiJing 2017 Wc]神秘物质

    来自FallDream的博客,未经允许,请勿转载,谢谢. 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研 ...

  10. bzoj2434阿狸的自动机

    转载自 http://www.cnblogs.com/zj75211/p/6934976.html ●BZOJ 2434: [Noi2011]阿狸的打字机   ●赘述题目 (题意就不赘述了) ●解法: ...