工具类所需依赖:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.5</version> </dependency>
工具类方法结构:
简单邮件方法
sendSimpleEmail(String, String, String, String, String, String, String)
sendSimpleEmail(String, String, String, String, String, String[], String)
sendSimpleEmailAndCc(String, String, String, String, String, String, String, List<String>)
sendSimpleEmailAndCc(String, String, String, String, String, String[], String, List<String>)
附件邮件方法
sendMultiPartEmail(String, String, String, String, String, String, String, String[])
sendMultiPartEmail(String, String, String, String, String, String[], String, String[])
sendMultiPartEmailAndCc(String, String, String, String, String, String, String, String[], List<String>)
sendMultiPartEmailAndCc(String, String, String, String, String, String[], String, String[], List<String>)
Html邮件方法
sendHtmlEmail(String, String, String, String, String, String, String)
sendHtmlEmail(String, String, String, String, String, String[], String)
sendHtmlEmailAndCc(String, String, String, String, String, String, String, List<String>)
sendHtmlEmailAndCc(String, String, String, String, String, String[], String, List<String>)
工具代码:
import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.apache.commons.mail.MultiPartEmail; import org.apache.commons.mail.SimpleEmail; /** * Description:邮件工具类 * Copyright:Copyright(c) 2018 * @author imok * @version 2018年7月24日 */ public class MailHelper { private static final SimpleEmail email = new SimpleEmail(); private static final HtmlEmail htmlEmail = new HtmlEmail(); private static final MultiPartEmail multiPartEmail = new MultiPartEmail(); private static void setEmailBasicProperty(String host, String username, String password, String subject, String contents, String fromEmailAddress) throws EmailException{ email.setHostName(host); email.setAuthentication(username, password); email.setFrom(fromEmailAddress, fromEmailAddress); email.setSubject(subject); email.setContent((Object)contents, "text/plain;charset=GBK"); } private static void setHtmlEmailBasicProperty(String host, String username, String password, String subject, String contents, String fromEmailAddress) throws EmailException{ htmlEmail.setHostName(host); htmlEmail.setAuthentication(username, password); htmlEmail.setFrom(fromEmailAddress, fromEmailAddress); htmlEmail.setSubject(subject); htmlEmail.setHtmlMsg(CharHelper.GBKto8859(contents)); } private static void setMultiPartEmailBasicProperty(String host, String username, String password, String subject, String contents, String fromEmailAddress) throws EmailException{ multiPartEmail.setHostName(host); multiPartEmail.setAuthentication(username, password); multiPartEmail.setFrom(fromEmailAddress, fromEmailAddress); multiPartEmail.setSubject(subject); multiPartEmail.setMsg(contents); //注意这个不要使用setContent这个方法 setMsg不会出现乱码 } /** * 简单的发邮件方式 邮件内容只有标题和邮件内容 支持单个个用户发送 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址 * @param fromEmailAddress 发件人的邮件地址 * @throws EmailException */ public static void sendSimpleEmail(String host, String username, String password, String subject, String contents, String toEmailAddress, String fromEmailAddress) throws EmailException { setEmailBasicProperty(host, username, password, subject, contents, fromEmailAddress); email.addTo(toEmailAddress); email.send(); } /** * 简单的发邮件方式 邮件内容只有标题和邮件内容 支持单个个用户发送并支持抄送 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址 * @param fromEmailAddress 发件人的邮件地址 * @param ccAddresses 抄送人的邮件地址 * @throws EmailException */ public static void sendSimpleEmailAndCc(String host, String username, String password, String subject, String contents, String toEmailAddress, String fromEmailAddress,List<String> ccAddresses) throws EmailException, AddressException{ if(ccAddresses != null){ List<InternetAddress> ccAddressesList = new ArrayList<InternetAddress>(); for(String cc : ccAddresses){ ccAddressesList.add(new InternetAddress(cc)); } email.setCc(ccAddressesList); } sendSimpleEmail(host, username, password, subject, contents, toEmailAddress, fromEmailAddress); } /** * 简单的发邮件方式 邮件内容只有标题和邮件内容 支持多个用户批量发送 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址 * @param fromEmailAddress 发件人的邮件地址 * @throws EmailException */ public static void sendSimpleEmail(String host, String username, String password, String subject, String contents, String [] toEmailAddress, String fromEmailAddress) throws EmailException { setEmailBasicProperty(host, username, password, subject, contents, fromEmailAddress); //发送给多个人 for (int i = 0; i < toEmailAddress.length; i++) { email.addTo(toEmailAddress[i], toEmailAddress[i]); } email.send(); } /** * 简单的发邮件方式 邮件内容只有标题和邮件内容 支持多个用户批量发送 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址 * @param fromEmailAddress 发件人的邮件地址 * @param ccAddresses 抄送人的邮件地址 * @throws EmailException * @throws AddressException */ public static void sendSimpleEmailAndCc(String host, String username, String password, String subject, String contents, String [] toEmailAddress, String fromEmailAddress,List<String> ccAddresses) throws EmailException, AddressException { sendSimpleEmail(host, username, password, subject, contents, toEmailAddress, fromEmailAddress); //设置抄送人 if(ccAddresses != null){ List<InternetAddress> ccAddressesList = new ArrayList<InternetAddress>(); for(String cc : ccAddresses){ ccAddressesList.add(new InternetAddress(cc)); } email.setCc(ccAddressesList); } email.send(); } /** * 发送带附件的邮件方式 邮件内容有标题和邮件内容和附件,附件可以是本地机器上的文本,也可以是web上的一个URL 文件, * 当为web上的一个URL文件时,此方法可以将WEB中的URL文件先下载到本地,再发送给收入用户 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址 * @param fromEmailAddress 发件人的邮件地址 * @param multiPaths 附件文件数组 * @throws EmailException */ public static void sendMultiPartEmail(String host, String username, String password, String subject, String contents, String toEmailAddress, String fromEmailAddress, String []multiPaths) throws MalformedURLException, EmailException { List<EmailAttachment> attachmentList = new ArrayList<EmailAttachment>(); if (multiPaths != null) { for (int i = 0; i < multiPaths.length; i++) { EmailAttachment attachment = new EmailAttachment(); if (multiPaths[i].indexOf("http") == -1) //判断当前这个文件路径是否在本地 如果是:setPath 否则 setURL; { attachment.setPath(multiPaths[i]); } else { attachment.setURL(new URL(multiPaths[i])); } attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription(""); attachmentList.add(attachment); } } //发送邮件信息 setMultiPartEmailBasicProperty(host, username, password, subject, contents, fromEmailAddress); multiPartEmail.addTo(toEmailAddress); for (int i = 0; i < attachmentList.size(); i++) //添加多个附件 { multiPartEmail.attach((EmailAttachment) attachmentList.get(i)); } multiPartEmail.send(); } /** * 发送带附件的邮件方式 邮件内容有标题和邮件内容和附件,附件可以是本地机器上的文本,也可以是web上的一个URL 文件, * 当为web上的一个URL文件时,此方法可以将WEB中的URL文件先下载到本地,再发送给收入用户 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址 * @param fromEmailAddress 发件人的邮件地址 * @param multiPaths 附件文件数组 * @param ccAddresses 抄送人的地址列表 * @throws EmailException */ public static void sendMultiPartEmailAndCc(String host, String username, String password, String subject, String contents, String toEmailAddress, String fromEmailAddress, String []multiPaths,List<String> ccAddresses) throws EmailException, AddressException, MalformedURLException{ //设置抄送人 if(ccAddresses != null){ List<InternetAddress> ccAddressesList = new ArrayList<InternetAddress>(); for(String cc : ccAddresses){ ccAddressesList.add(new InternetAddress(cc)); } multiPartEmail.setCc(ccAddressesList); } sendMultiPartEmail(host, username, password, subject, contents, toEmailAddress, fromEmailAddress, multiPaths); } /** * 发送带附件的邮件方式 邮件内容有标题和邮件内容和附件,附件可以是本地机器上的文本,也可以是web上的一个URL 文件, * 当为web上的一个URL文件时,此方法可以将WEB中的URL文件先下载到本地,再发送给收入用户 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址数组 * @param fromEmailAddress 发件人的邮件地址 * @param multiPaths 附件文件数组 * @throws EmailException */ public static void sendMultiPartEmail(String host, String username, String password, String subject, String contents, String[] toEmailAddress, String fromEmailAddress, String []multiPaths) throws MalformedURLException, EmailException { List<EmailAttachment> attachmentList = new ArrayList<EmailAttachment>(); if (multiPaths != null) { for (int i = 0; i < multiPaths.length; i++) { EmailAttachment attachment = new EmailAttachment(); if (multiPaths[i].indexOf("http") == -1) //判断当前这个文件路径是否在本地 如果是:setPath 否则 setURL; { attachment.setPath(multiPaths[i]); } else { attachment.setURL(new URL(multiPaths[i])); } attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription(""); attachmentList.add(attachment); } } //发送邮件信息 setMultiPartEmailBasicProperty(host, username, password, subject, contents, fromEmailAddress); //发送给多个人 for (int i = 0; i < toEmailAddress.length; i++) { multiPartEmail.addTo(toEmailAddress[i], toEmailAddress[i]); } //添加多个附件 for (int i = 0; i < attachmentList.size(); i++) { multiPartEmail.attach((EmailAttachment) attachmentList.get(i)); } multiPartEmail.send(); } /** * 发送带附件的邮件方式 邮件内容有标题和邮件内容和附件,附件可以是本地机器上的文本,也可以是web上的一个URL 文件, * 当为web上的一个URL文件时,此方法可以将WEB中的URL文件先下载到本地,再发送给收入用户 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址数组 * @param fromEmailAddress 发件人的邮件地址 * @param multiPaths 附件文件数组 * @param ccAddresses 抄送人地址列表 * @throws EmailException * @throws AddressException */ public static void sendMultiPartEmailAndCc(String host, String username, String password, String subject, String contents, String[] toEmailAddress, String fromEmailAddress, String []multiPaths, List<String> ccAddresses) throws MalformedURLException, EmailException, AddressException { //设置抄送人 if(ccAddresses != null){ List<InternetAddress> ccAddressesList = new ArrayList<InternetAddress>(); for(String cc : ccAddresses){ ccAddressesList.add(new InternetAddress(cc)); } multiPartEmail.setCc(ccAddressesList); } sendMultiPartEmail(host, username, password, subject, contents, toEmailAddress, fromEmailAddress, multiPaths); } /** * 发送Html格式的邮件,单个收件人 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址 * @param fromEmailAddress 发件人邮件地址 * @throws EmailException */ public static void sendHtmlEmail(String host, String username, String password, String subject, String contents, String toEmailAddress, String fromEmailAddress) throws EmailException { setHtmlEmailBasicProperty(host, username, password, subject, contents, fromEmailAddress); //发送给一人 htmlEmail.addTo(toEmailAddress); htmlEmail.send(); } /** * 发送Html格式的邮件(单个收件人),并抄送 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址 * @param fromEmailAddress 抄送人的邮件地址 * @param ccAddresses * @throws EmailException */ public static void sendHtmlEmailAndCc(String host, String username, String password, String subject, String contents, String toEmailAddress, String fromEmailAddress,List<String> ccAddresses) throws AddressException, EmailException{ //设置抄送人 if(ccAddresses != null){ List<InternetAddress> ccAddressesList = new ArrayList<InternetAddress>(); for(String cc : ccAddresses){ ccAddressesList.add(new InternetAddress(cc)); } htmlEmail.setCc(ccAddressesList); } sendHtmlEmail(host, username, password, subject, contents, toEmailAddress, fromEmailAddress); } /** * 发送Html格式的邮件,多个收件人 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址数组 * @param fromEmailAddress 发件人邮件地址 * @throws EmailException */ public static void sendHtmlEmail(String host, String username, String password, String subject, String contents, String[] toEmailAddress, String fromEmailAddress) throws EmailException { setHtmlEmailBasicProperty(host, username, password, subject, contents, fromEmailAddress); //发送给多个人 for (int i = 0; i < toEmailAddress.length; i++) { htmlEmail.addTo(toEmailAddress[i], toEmailAddress[i]); } htmlEmail.send(); } /** * 发送Html格式的邮件,多个收件人,抄送邮件 * * @param host 邮件服务器地址 * @param username 连接邮件服务器的用户名称 * @param password 连接邮件服务器的用户密码 * @param subject 邮件的主题 * @param contents 邮件的内容 * @param toEmailAddress 收件人的邮件地址数组 * @param fromEmailAddress 发件人邮件地址 * @param ccAddresses 抄送人邮件地址 * @throws EmailException * @throws AddressException */ public static void sendHtmlEmailAndCc(String host, String username, String password, String subject, String contents, String[] toEmailAddress, String fromEmailAddress, List<String> ccAddresses) throws EmailException, AddressException { //设置抄送人 if(ccAddresses != null){ List<InternetAddress> ccAddressesList = new ArrayList<InternetAddress>(); for(String cc : ccAddresses){ ccAddressesList.add(new InternetAddress(cc)); } htmlEmail.setCc(ccAddressesList); } sendHtmlEmail(host, username, password, subject, contents, toEmailAddress, fromEmailAddress); } }