jar包:html
javax.mail-1.5.5.jarjava
maven配置:apache
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.5</version> </dependency>
代碼:服務器
package com.test; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.apache.log4j.Logger; import com.test.MailAuthenticator; public class SendMail { // 日誌記錄 private static Logger logger = Logger.getLogger(SendMail.class); public static MailAuthenticator authenticator; private MimeMessage message; private Session session; private Transport transport; private Properties properties = new Properties(); private String mailHost = null; private String sender_username = null; private String sender_password = null; /** * 構造方法 */ public SendMail() { super(); } /** * 供外界調用的發送郵件接口 */ public boolean sendEmail(String title, String content, List<String> receivers, List<File> fileList) { try { // 初始化smtp發送郵件所需參數 initSmtpParams(); // 發送郵件 doSendHtmlEmail(title, content, receivers, fileList); } catch (Exception e) { logger.error(e); } return true; } /** * 初始化smtp發送郵件所需參數 */ private boolean initSmtpParams() { mailHost = "郵箱smtp服務器"; // 郵箱類型不一樣值也會不一樣 sender_username = "發件人郵箱"; sender_password = "發件人郵箱密碼"; properties.put("mail.smtp.host", mailHost);// mail.envisioncitrix.com properties.put("mail.smtp.auth", "true"); properties.put("mail.transport.protocol", "smtp"); properties.put("mail.smtp.starttls.enable", "true"); properties.put("mail.smtp.ssl.checkserveridentity", "false"); properties.put("mail.smtp.ssl.trust", mailHost); authenticator = new MailAuthenticator(sender_username, sender_password); session = Session.getInstance(properties, authenticator); session.setDebug(false);// 開啓後有調試信息 message = new MimeMessage(session); return true; } /** * 發送郵件 */ private boolean doSendHtmlEmail(String title, String htmlContent, List<String> receivers, List<File> fileList) { try { // 發件人 InternetAddress from = new InternetAddress(sender_username); message.setFrom(from); // 收件人(多個) InternetAddress[] sendTo = new InternetAddress[receivers.size()]; for (int i = 0; i < receivers.size(); i++) { sendTo[i] = new InternetAddress(receivers.get(i)); } message.setRecipients(MimeMessage.RecipientType.TO, sendTo); // 郵件主題 message.setSubject(title); // 添加郵件的各個部份內容,包括文本內容和附件 Multipart multipart = new MimeMultipart(); // 添加郵件正文 BodyPart contentPart = new MimeBodyPart(); contentPart.setContent(htmlContent, "text/html;charset=UTF-8"); multipart.addBodyPart(contentPart); // 遍歷添加附件 if (fileList != null && fileList.size() > 0) { for (File file : fileList) { BodyPart attachmentBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(file); attachmentBodyPart.setDataHandler(new DataHandler(source)); attachmentBodyPart.setFileName(file.getName()); multipart.addBodyPart(attachmentBodyPart); } } // 將多媒體對象放到message中 message.setContent(multipart); // 保存郵件 message.saveChanges(); // SMTP驗證,就是你用來發郵件的郵箱用戶名密碼 transport = session.getTransport("smtp"); transport.connect(mailHost, sender_username, sender_password); // 發送郵件 transport.sendMessage(message, message.getAllRecipients()); System.out.println(title + " Email send success!"); } catch (Exception e) { logger.error(e); } finally { if (transport != null) { try { transport.close(); } catch (MessagingException e) { logger.error(e); } } } return true; } /** * 測試main */ public static void main(String[] args) { // 郵件主題 String title = "郵件主題"; // 郵件正文 String htmlContent = "郵件內容"; // 收件人 List<String> receivers = new ArrayList<String>(); receivers.add("收件人郵箱1"); receivers.add("收件人郵箱2"); // 附件 String fileName1 = "附件路徑1"; File file1 = new File(fileName1); String fileName2 = "附件路徑2"; File file2 = new File(fileName2); List<File> fileList = new ArrayList<File>(); fileList.add(file1); fileList.add(file2); // 執行發送 new SendMail().sendEmail(title, htmlContent, receivers, fileList); } }
代碼:session
package com.test; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * 經過帳號密碼進行身份驗證 */ public class MailAuthenticator extends Authenticator { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } public MailAuthenticator(String username, String password) { this.userName = username; this.password = password; } }