利用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();
}
}