在講代碼前先作些準備工做。html
1.在服務器上添加發送郵箱爲白名單。java
2.獲取發送郵箱的主機,端口,受權碼。通常在郵箱的設置裏面都能找到。spring
3.代碼(不知道爲何,複製過來就一行)apache
package com.fairyland.jdp.framework.util;import java.net.URL;import java.util.Properties;import javax.mail.internet.MimeMessage;import org.apache.commons.lang3.StringUtils;import org.springframework.core.io.InputStreamSource;import org.springframework.core.io.UrlResource;import org.springframework.mail.MailException;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;public class MailUtil { // public static JavaMailSenderImpl mailSender = // createMailSender("smtp.263xmail.com", 25, // "InformationSendingMail@metlife.com", // "Hello01", 25000); public static JavaMailSenderImpl mailSender = createMailSender("smtp.qq.com", 25, "1194862954@qq.com", "fkrurxuawayijhhg", 25000); private static JavaMailSenderImpl createMailSender(String host, int port, String username, String password, int timeout) { JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost(host); sender.setPort(port); sender.setUsername(username); sender.setPassword(password); sender.setDefaultEncoding("Utf-8"); Properties p = new Properties(); p.setProperty("mail.smtp.timeout", String.valueOf(timeout)); p.setProperty("mail.smtp.auth", "true"); sender.setJavaMailProperties(p); return sender; } public static boolean sendMail(String title, String html, String recipients, String urls) throws Exception { return sendAttachsMail(title, html, recipients, "", "", urls); } public static void main(String[] args) throws Exception {// sendText("測試", "https://ezt-uat.metlife.com.cn/static/test.xls", "1194862954@qq.com", null, null);// sendAttachsMail("test", "this is a test email!", "1194862954@qq.com", null, null, "file://C:/Users/fangza/Desktop/Test/c.xls"); } /****** 下面都是特殊時候使用,發送郵件使用上面的方法 ************/ // 1.發送簡單的文本郵件 public static boolean sendText(String title, String message, String recipients, String ccrecipients, String bccrecipients) throws MailException { SimpleMailMessage msg = new SimpleMailMessage(); msg.setFrom(mailSender.getUsername()); if (StringUtils.isEmpty(recipients)) { return false; } String[] recipientArr = recipients.split(";"); msg.setTo(recipientArr); if (!StringUtils.isEmpty(ccrecipients)) { String[] ccrecipientArr = ccrecipients.split(";"); msg.setCc(ccrecipientArr); } if (!StringUtils.isEmpty(bccrecipients)) { String[] bccrecipientArr = bccrecipients.split(";"); msg.setBcc(bccrecipientArr); } msg.setSubject(title); msg.setText(message); mailSender.send(msg); return true; } // 2.發送帶附件的郵件 public static boolean sendAttachsMail(String title, String html, String recipients, String ccrecipients, String bccrecipients, String urls) throws Exception { MimeMessage miMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(miMessage, true, "UTF-8"); helper.setFrom(mailSender.getUsername()); if (StringUtils.isEmpty(recipients)) { return false; } String[] recipientArr = recipients.split(";"); helper.setTo(recipientArr); if (!StringUtils.isEmpty(ccrecipients)) { String[] ccrecipientArr = ccrecipients.split(";"); helper.setCc(ccrecipientArr); } if (!StringUtils.isEmpty(bccrecipients)) { String[] bccrecipientArr = bccrecipients.split(";"); helper.setBcc(bccrecipientArr); } helper.setSubject(title); helper.setText(html, true); if (!StringUtils.isEmpty(urls)) { String[] urls1 = urls.split(";"); for (int i = 0; i < urls1.length; i++) { URL url = new URL(urls1[i]); InputStreamSource asInputStreamSource = new UrlResource(url); String[] arr = url.toString().split("/"); String fileTitle = arr[arr.length - 1]; helper.addAttachment(fileTitle, asInputStreamSource); } } mailSender.send(miMessage); return true; }}複製代碼