Apache commons email 使用过程中遇到的问题

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

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();        }    }}
  
  
  • 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
  • 30
  • 31

带附件实例(图片和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("邮件发送成功!");    }
  
  
  • 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

带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("邮件发送成功!");    }
  
  
  • 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

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

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();        }    }}
  
  
  • 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
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

报错的信息如下:

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)
  
  
  • 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
  • 30

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

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值