import com.jfinal.kit.StrKit; import com.jfinal.log.Log; import org.apache.commons.mail.*; import java.util.ArrayList; import java.util.List; public class EmailKit { private static final Log log = Log.getLog(EmailKit.class); /** * 发送普通纯文本邮件 * * @param fromEmail 发件邮箱 * @param toEmail 收件地址 * @param title 邮件标题 * @param content 邮件内容 * @return */ public static String sendEmail(String fromEmail, String toEmail, String title, String content) { return sendEmail(null, fromEmail, null, toEmail, title, content); } /** * 发送普通纯文本邮件 * * @param emailServer 邮件发送服务器地址 * @param fromEmail 发件邮箱 * @param password 发件邮箱密码 * @param toEmail 收件地址 * @param title 邮件标题 * @param content 邮件内容 * @return */ public static String sendEmail(String emailServer, String fromEmail, String password, String toEmail, String title, String content) { SimpleEmail email = new SimpleEmail(); if (StrKit.notBlank(emailServer)) { email.setHostName(emailServer); } else { // 默认使用本地 postfix 发送,这样就可以将postfix 的 mynetworks 配置为 127.0.0.1 或 127.0.0.0/8 了 email.setHostName("127.0.0.1"); } // 如果密码为空,则不进行认证 if (StrKit.notBlank(password)) { email.setAuthentication(fromEmail, password); } email.setCharset("utf-8"); try { email.addTo(toEmail); email.setFrom(fromEmail); email.setSubject(title); email.setMsg(content); return email.send(); } catch (EmailException e) { log.error(e.getMessage(), e); throw new RuntimeException(e); } } /** * 发送带附件的邮件 * * @param fromEmail 发件邮箱 * @param toEmail 收件地址 * @param title 邮件标题 * @param content 邮件内容 * @param attachments 附件信息列表(List泛型对象为EmailAttachment) * @return */ public static String sendEmail4ATM(String fromEmail, String toEmail, String title, String content, List attachments) { return sendEmail4ATM(null, fromEmail, null, toEmail, title, content, attachments); } /** * 发送带附件的邮件 * * @param emailServer 邮件发送服务器地址 * @param fromEmail 发件邮箱 * @param password 发件邮箱密码 * @param toEmail 收件地址 * @param title 邮件标题 * @param content 邮件内容 * @param attachments 附件信息列表(List泛型对象为EmailAttachment) * @return */ public static String sendEmail4ATM(String emailServer, String fromEmail, String password, String toEmail, String title, String content, List attachments) { MultiPartEmail email = new MultiPartEmail(); if (StrKit.notBlank(emailServer)) { email.setHostName(emailServer); } else { // 默认使用本地 postfix 发送,这样就可以将postfix 的 mynetworks 配置为 127.0.0.1 或 127.0.0.0/8 了 email.setHostName("127.0.0.1"); } // 如果密码为空,则不进行认证 if (StrKit.notBlank(password)) { email.setAuthentication(fromEmail, password); } email.setCharset("utf-8"); try { email.addTo(toEmail); email.setFrom(fromEmail); email.setSubject(title); email.setMsg(content); // 添加附件 if (null != attachments) { for (int i = 0; i < attachments.size(); i++) { EmailAttachment attachment = (EmailAttachment) attachments.get(i); if (null != attachment) { email.attach(attachment); } } } return email.send(); } catch (EmailException e) { log.error(e.getMessage(), e); throw new RuntimeException(e); } } /** * 发送HTML格式的邮件 * * @param fromEmail 发件邮箱 * @param toEmail 收件地址 * @param title 邮件标题 * @param content 邮件内容 * @param htmlMsg HTML格式 * @return */ public static String sendEmail4HTML(String fromEmail, String toEmail, String title, String content, String htmlMsg) { return sendEmail4HTML(null, fromEmail, null, toEmail, title, content, htmlMsg); } /** * 发送HTML格式的邮件 * * @param emailServer 邮件发送服务器地址 * @param fromEmail 发件邮箱 * @param password 发件邮箱密码 * @param toEmail 收件地址 * @param title 邮件标题 * @param content 邮件内容 * @param htmlMsg HTML格式 * @return */ public static String sendEmail4HTML(String emailServer, String fromEmail, String password, String toEmail, String title, String content, String htmlMsg) { HtmlEmail email = new HtmlEmail(); if (StrKit.notBlank(emailServer)) { email.setHostName(emailServer); } else { // 默认使用本地 postfix 发送,这样就可以将postfix 的 mynetworks 配置为 127.0.0.1 或 127.0.0.0/8 了 email.setHostName("127.0.0.1"); } // 如果密码为空,则不进行认证 if (StrKit.notBlank(password)) { email.setAuthentication(fromEmail, password); } email.setCharset("utf-8"); try { email.addTo(toEmail); email.setFrom(fromEmail); email.setSubject(title); email.setTextMsg(content); // 添加HTML email.setHtmlMsg(htmlMsg); return email.send(); } catch (EmailException e) { log.error(e.getMessage(), e); throw new RuntimeException(e); } } public static void main(String[] args) { List attachments = new ArrayList(); EmailAttachment attachment = new EmailAttachment(); attachment.setPath("D:\\sss.docx"); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachments.add(attachment); EmailAttachment attachment1 = new EmailAttachment(); attachment1.setPath("D:\\sss11.docx"); attachment1.setDisposition(EmailAttachment.ATTACHMENT); attachments.add(attachment1); String ret = sendEmail4ATM( "smtp.163.com", // 邮件发送服务器地址 "xxxxxx@163.com", // 发件邮箱 "xxxxxx", // 发件邮箱密码 "xxxxxx@qq.com", // 收件地址 "邮件标题", // 邮件标题 "content", // 邮件内容 attachments); System.out.println("发送返回值: " + ret); } }