一:需要引入mail maven jar包
<!--郵件發送包--> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
二:代碼示例
2.1 定義消息實體類
package textAnalysis.dto; /** * Created by 草帽boy on 2016/12/8. */ public class MailInfo { //郵箱服務器 如smtp.163.com private String host ; //用戶郵箱 如**@163 private String formName ; //用戶授權碼 不是用戶名密碼 可以自行查看相關郵件服務器怎么查看 private String formPassword ; //消息回復郵箱 private String replayAddress ; //發送地址 private String toAddress ; //發送主題 private String subject ; //發送內容 private String content ; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getFormName() { return formName; } public void setFormName(String formName) { this.formName = formName; } public String getFormPassword() { return formPassword; } public void setFormPassword(String formPassword) { this.formPassword = formPassword; } public String getReplayAddress() { return replayAddress; } public void setReplayAddress(String replayAddress) { this.replayAddress = replayAddress; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
2.2 兩種方式實現
首先你需要獲取你的授權碼,以@163.com為例 http://blog.csdn.net/hughnes/article/details/52070878
sendTextMail是純文本發送
sendHtmlMail是HTML格式標簽發送 這種方式可以識別html標簽
package textAnalysis.util; import textAnalysis.dto.MailInfo; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.util.Date; import java.util.Properties; /** * Created by 草帽boy on 2016/12/8. */ public class MailSendUtil { private final static String host = "smtp.163.com"; //163的服務器 private final static String formName = "YYYXXX@163.com";//你的郵箱 private final static String password = "***********"; //授權碼 private final static String replayAddress = "YYYXXX@163.com"; //你的郵箱 public static void sendHtmlMail(MailInfo info)throws Exception{ info.setHost(host); info.setFormName(formName); info.setFormPassword(password); //網易郵箱的授權碼~不一定是密碼 info.setReplayAddress(replayAddress); Message message = getMessage(info); // MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象 Multipart mainPart = new MimeMultipart(); // 創建一個包含HTML內容的MimeBodyPart BodyPart html = new MimeBodyPart(); // 設置HTML內容 html.setContent(info.getContent(), "text/html; charset=utf-8"); mainPart.addBodyPart(html); // 將MiniMultipart對象設置為郵件內容 message.setContent(mainPart); Transport.send(message); } public static void sendTextMail(MailInfo info) throws Exception { info.setHost(host); info.setFormName(formName); info.setFormPassword(password); //網易郵箱的授權碼~不一定是密碼 info.setReplayAddress(replayAddress); Message message = getMessage(info); //消息發送的內容 message.setText(info.getContent()); Transport.send(message); } private static Message getMessage(MailInfo info) throws Exception{ final Properties p = System.getProperties() ; p.setProperty("mail.smtp.host", info.getHost()); p.setProperty("mail.smtp.auth", "true"); p.setProperty("mail.smtp.user", info.getFormName()); p.setProperty("mail.smtp.pass", info.getFormPassword()); // 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session Session session = Session.getInstance(p, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass")); } }); session.setDebug(true); Message message = new MimeMessage(session); //消息發送的主題 message.setSubject(info.getSubject()); //接受消息的人 message.setReplyTo(InternetAddress.parse(info.getReplayAddress())); //消息的發送者 message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),"余高峰")); // 創建郵件的接收者地址,並設置到郵件消息中 message.setRecipient(Message.RecipientType.TO,new InternetAddress(info.getToAddress())); // 消息發送的時間 message.setSentDate(new Date()); return message ; } }
三:測試
@Test public void send(){ String mail = "mmmmmmm@qq.com"; //發送對象的郵箱 String title = "我有一句話跟你說"; String content = "<div>你不在學校嗎?</div><br/><hr/><div>記得28號來學校</div>"; MailInfo info = new MailInfo(); info.setToAddress(mail); info.setSubject(title); info.setContent(content); try {
//MailSendUtil.sendTextMail(info); MailSendUtil.sendHtmlMail(info); } catch (Exception e) { System.out.print("'"+title+"'的郵件發送失敗!"); e.printStackTrace(); } }
采用的Junit的方式測試的采用 sendTextMail 的結果是
采用的Junit的方式測試的采用 sendHtmlMai l的結果是