apache的commons-email 類庫開發示例
知識
08-01
利用apache的commons-email類庫發送郵件示例代碼,commons-email類庫地址:http://commons.apache.org/proper/commons-email/
1、maven應用
- <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-email</artifactId>
- <version>1.5</version>
- </dependency>
2、示例代碼
- package com.demo.email;
- import org.apache.commons.mail.*;
- import org.apache.commons.mail.resolver.DataSourceUrlResolver;
- import java.net.URL;
- /**
- * Created by wuguowei on 2018/7/30.
- */
- public class TestApacheEmail {
- private static String sHostName = "smtp.mxhichina.com";
- private static int nSmtpPort = 465;
- private static String sUserName = "your@email.com";
- private static String sPassword = "youreamilpassword";
- private static String sFromEmail = "your@email.com";
- private static String sToEmail = "receive@qq.com";
- public static void main(String[] args) throws Exception {
- //sendSimpleEmail();
- //sendEmailAttachment();
- //sendUrlAttachmentEmail();
- //sendHtmlEmail();
- sendInnerImageHtmlEmail();
- }
- /**
- * 發送純文本郵件
- *
- * @throws Exception
- */
- public static void sendSimpleEmail() throws Exception {
- Email email = new SimpleEmail();
- email.setHostName(sHostName);
- email.setSmtpPort(nSmtpPort);
- email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
- email.setSSLOnConnect(true);
- email.setFrom(sFromEmail);
- email.setSubject("sendSimpleEmail");
- email.setMsg("This is sendSimpleEmail ... :-)");
- email.addTo(sToEmail);
- email.send();
- }
- /**
- * 發送附件郵件
- *
- * @throws Exception
- */
- public static void sendEmailAttachment() throws Exception {
- // Create the attachment
- EmailAttachment attachment = new EmailAttachment();
- attachment.setPath("/Users/wuguowei/Downloads/baidu.png");
- //發送url地址
- //attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
- attachment.setDisposition(EmailAttachment.ATTACHMENT);
- attachment.setDescription("Picture of John");
- attachment.setName("John");
- // Create the email message
- MultiPartEmail email = new MultiPartEmail();
- email.setHostName(sHostName);
- email.setSmtpPort(nSmtpPort);
- email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
- email.setSSLOnConnect(true);
- email.setFrom(sFromEmail, "Me");
- email.addTo(sToEmail);
- email.setSubject("The sendEmailAttachment");
- email.setMsg("Here is sendEmailAttachment");
- // add the attachment
- email.attach(attachment);
- // send the email
- email.send();
- }
- /**
- * 發送遠程的url地址的圖片附件
- *
- * @throws Exception
- */
- private static void sendUrlAttachmentEmail() throws Exception {
- // Create the attachment
- EmailAttachment attachment = new EmailAttachment();
- //發送url地址
- attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
- attachment.setDisposition(EmailAttachment.ATTACHMENT);
- attachment.setDescription("Picture of John");
- attachment.setName("John");
- // Create the email message
- MultiPartEmail email = new MultiPartEmail();
- email.setHostName(sHostName);
- email.setSmtpPort(nSmtpPort);
- email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
- email.setSSLOnConnect(true);
- email.setFrom(sFromEmail, "Me");
- email.addTo(sToEmail);
- email.setSubject("sendUrlAttachmentEmail");
- email.setMsg("Here is sendUrlAttachmentEmail");
- // add the attachment
- email.attach(attachment);
- // send the email
- email.send();
- }
- /**
- * 發送htmlEmail
- *
- * @throws Exception
- */
- private static void sendHtmlEmail() throws Exception {
- // Create the email message
- HtmlEmail email = new HtmlEmail();
- email.setHostName(sHostName);
- email.setSmtpPort(nSmtpPort);
- email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
- email.setSSLOnConnect(true);
- email.setFrom(sFromEmail, "Me");
- email.addTo(sToEmail);
- email.setSubject("sendHtmlEmail");
- // 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");
- // set the html message
- email.setHtmlMsg("<html>The apache logo - <img src="cid:" + cid + ""></html>");
- // set the alternative message
- email.setTextMsg("Your email client does not support HTML messages");
- // send the email
- email.send();
- }
- /**
- * 發送htmlEmail,把圖片轉換為內部圖片發送郵件
- *
- * @throws Exception
- */
- private static void sendInnerImageHtmlEmail() throws Exception {
- // load your HTML email template,可以cid的方式,或者是具體的url地址
- String htmlEmailTemplate = "<html>The apache logo - <img src="http://commons.apache.org/proper/commons-email/images/commons-logo.png"></html>";
- // define you base URL to resolve relative resource locations
- URL url = new URL("http://www.apache.org");
- // create the email message
- DataSourceUrlResolver dataSourceUrlResolver = new DataSourceUrlResolver(url);
- ImageHtmlEmail email = new ImageHtmlEmail();
- email.setDataSourceResolver(dataSourceUrlResolver);
- email.setHostName(sHostName);
- email.setSmtpPort(nSmtpPort);
- email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
- email.setSSLOnConnect(true);
- email.setFrom(sFromEmail, "Me");
- email.addTo(sToEmail);
- email.setSubject("Test email with inline image");
- // set the html message
- email.setHtmlMsg(htmlEmailTemplate);
- // set the alternative message
- email.setTextMsg("Your email client does not support HTML messages");
- // send the email
- email.send();
- }
- }
※類型轉換運算符 運算符重載 operator new operator delete
※Win7如何簡單的關閉445埠及445埠入侵詳解
TAG:程序員小新人學習 |