當前位置:
首頁 > 知識 > apache的commons-email 類庫開發示例

apache的commons-email 類庫開發示例

利用apache的commons-email類庫發送郵件示例代碼,commons-email類庫地址:http://commons.apache.org/proper/commons-email/

1、maven應用

  1. <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
  2. <dependency>
  3. <groupId>org.apache.commons</groupId>
  4. <artifactId>commons-email</artifactId>
  5. <version>1.5</version>
  6. </dependency>

2、示例代碼

  1. package com.demo.email;
  2. import org.apache.commons.mail.*;
  3. import org.apache.commons.mail.resolver.DataSourceUrlResolver;
  4. import java.net.URL;
  5. /**
  6. * Created by wuguowei on 2018/7/30.
  7. */
  8. public class TestApacheEmail {
  9. private static String sHostName = "smtp.mxhichina.com";
  10. private static int nSmtpPort = 465;
  11. private static String sUserName = "your@email.com";
  12. private static String sPassword = "youreamilpassword";
  13. private static String sFromEmail = "your@email.com";
  14. private static String sToEmail = "receive@qq.com";
  15. public static void main(String[] args) throws Exception {
  16. //sendSimpleEmail();
  17. //sendEmailAttachment();
  18. //sendUrlAttachmentEmail();
  19. //sendHtmlEmail();
  20. sendInnerImageHtmlEmail();
  21. }
  22. /**
  23. * 發送純文本郵件
  24. *
  25. * @throws Exception
  26. */
  27. public static void sendSimpleEmail() throws Exception {
  28. Email email = new SimpleEmail();
  29. email.setHostName(sHostName);
  30. email.setSmtpPort(nSmtpPort);
  31. email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
  32. email.setSSLOnConnect(true);
  33. email.setFrom(sFromEmail);
  34. email.setSubject("sendSimpleEmail");
  35. email.setMsg("This is sendSimpleEmail ... :-)");
  36. email.addTo(sToEmail);
  37. email.send();
  38. }
  39. /**
  40. * 發送附件郵件
  41. *
  42. * @throws Exception
  43. */
  44. public static void sendEmailAttachment() throws Exception {
  45. // Create the attachment
  46. EmailAttachment attachment = new EmailAttachment();
  47. attachment.setPath("/Users/wuguowei/Downloads/baidu.png");
  48. //發送url地址
  49. //attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
  50. attachment.setDisposition(EmailAttachment.ATTACHMENT);
  51. attachment.setDescription("Picture of John");
  52. attachment.setName("John");
  53. // Create the email message
  54. MultiPartEmail email = new MultiPartEmail();
  55. email.setHostName(sHostName);
  56. email.setSmtpPort(nSmtpPort);
  57. email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
  58. email.setSSLOnConnect(true);
  59. email.setFrom(sFromEmail, "Me");
  60. email.addTo(sToEmail);
  61. email.setSubject("The sendEmailAttachment");
  62. email.setMsg("Here is sendEmailAttachment");
  63. // add the attachment
  64. email.attach(attachment);
  65. // send the email
  66. email.send();
  67. }
  68. /**
  69. * 發送遠程的url地址的圖片附件
  70. *
  71. * @throws Exception
  72. */
  73. private static void sendUrlAttachmentEmail() throws Exception {
  74. // Create the attachment
  75. EmailAttachment attachment = new EmailAttachment();
  76. //發送url地址
  77. attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
  78. attachment.setDisposition(EmailAttachment.ATTACHMENT);
  79. attachment.setDescription("Picture of John");
  80. attachment.setName("John");
  81. // Create the email message
  82. MultiPartEmail email = new MultiPartEmail();
  83. email.setHostName(sHostName);
  84. email.setSmtpPort(nSmtpPort);
  85. email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
  86. email.setSSLOnConnect(true);
  87. email.setFrom(sFromEmail, "Me");
  88. email.addTo(sToEmail);
  89. email.setSubject("sendUrlAttachmentEmail");
  90. email.setMsg("Here is sendUrlAttachmentEmail");
  91. // add the attachment
  92. email.attach(attachment);
  93. // send the email
  94. email.send();
  95. }
  96. /**
  97. * 發送htmlEmail
  98. *
  99. * @throws Exception
  100. */
  101. private static void sendHtmlEmail() throws Exception {
  102. // Create the email message
  103. HtmlEmail email = new HtmlEmail();
  104. email.setHostName(sHostName);
  105. email.setSmtpPort(nSmtpPort);
  106. email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
  107. email.setSSLOnConnect(true);
  108. email.setFrom(sFromEmail, "Me");
  109. email.addTo(sToEmail);
  110. email.setSubject("sendHtmlEmail");
  111. // embed the image and get the content id
  112. URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
  113. String cid = email.embed(url, "Apache logo");
  114. // set the html message
  115. email.setHtmlMsg("<html>The apache logo - <img src="cid:" + cid + ""></html>");
  116. // set the alternative message
  117. email.setTextMsg("Your email client does not support HTML messages");
  118. // send the email
  119. email.send();
  120. }
  121. /**
  122. * 發送htmlEmail,把圖片轉換為內部圖片發送郵件
  123. *
  124. * @throws Exception
  125. */
  126. private static void sendInnerImageHtmlEmail() throws Exception {
  127. // load your HTML email template,可以cid的方式,或者是具體的url地址
  128. String htmlEmailTemplate = "<html>The apache logo - <img src="http://commons.apache.org/proper/commons-email/images/commons-logo.png"></html>";
  129. // define you base URL to resolve relative resource locations
  130. URL url = new URL("http://www.apache.org");
  131. // create the email message
  132. DataSourceUrlResolver dataSourceUrlResolver = new DataSourceUrlResolver(url);
  133. ImageHtmlEmail email = new ImageHtmlEmail();
  134. email.setDataSourceResolver(dataSourceUrlResolver);
  135. email.setHostName(sHostName);
  136. email.setSmtpPort(nSmtpPort);
  137. email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
  138. email.setSSLOnConnect(true);
  139. email.setFrom(sFromEmail, "Me");
  140. email.addTo(sToEmail);
  141. email.setSubject("Test email with inline image");
  142. // set the html message
  143. email.setHtmlMsg(htmlEmailTemplate);
  144. // set the alternative message
  145. email.setTextMsg("Your email client does not support HTML messages");
  146. // send the email
  147. email.send();
  148. }
  149. }

apache的commons-email 類庫開發示例

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

類型轉換運算符 運算符重載 operator new operator delete
Win7如何簡單的關閉445埠及445埠入侵詳解

TAG:程序員小新人學習 |