1. 程式人生 > >Apache commons email 使用過程中遇到的問題

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("[email protected]");            email.setSubject("Test Mail By Commons-Emial");            email.setMsg("Congratulations!\nYou have been admitted, so come here and join us ! :-)"
);            email.addTo("[email protected]");            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("[email protected]");        email.setFrom("[email protected]");        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("[email protected]");                email.setFrom("[email protected]");                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("[email protected]", "gdsadsaddsadsd");            email.setSSLOnConnect(true);            email.setSSL(true);            email.addTo("[email protected]", "小郭");            email.setFrom("[email protected]", "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

如果你也遇到了這個問題,而且解決了。歡迎留言!我會及時的來修改部落格的!