JavaMail郵件開發,javamail郵件
一、只帶有純文字的郵件
代碼案例如下:
package com.lyh.sendemail;import java.util.Properties;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;//發送郵件public class MessageDemo1 { public static void main(String[] args) throws Exception{ Properties props = new Properties();//key value:配置參數。真正發送郵件時再配置 props.setProperty("mail.transport.protocol", "smtp");//指定郵件發送的協議,參數是規範規定的 props.setProperty("mail.host", "smtp.163.com");//指定發件伺服器的地址,參數是規範規定的// props.setProperty("mail.debug", "true");//郵件發送的偵錯模式,參數是規範規定的 props.setProperty("mail.smtp.auth", "true");//請求伺服器進行身份認證。參數與具體的JavaMail實現有關 Session session = Session.getInstance(props);//發送郵件時使用的環境配置 session.setDebug(true); MimeMessage message = new MimeMessage(session); //設定郵件的頭 message.setFrom(new InternetAddress("xxx@163.com")); message.setRecipients(Message.RecipientType.TO, "xxx@qq.com"); message.setSubject("This is second message"); //設定本文 message.setContent("<h1>hello</h1>", "text/html");// message.setText("<h1>hello</h1>");//純文字 message.saveChanges(); //發送郵件 Transport ts = session.getTransport(); ts.connect("xxx@163.com", "123456"); // 密碼為授權碼不是郵箱的登入密碼 ts.sendMessage(message, message.getAllRecipients());//對象,用執行個體方法} }}
二、帶有圖片的郵件
a、複雜郵件封裝模型
代碼案例
package com.lyh.sendemail;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Message;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;//發送郵件public class MessageDemo2 { public static void main(String[] args) throws Exception{ Properties props = new Properties();//key value:配置參數。真正發送郵件時再配置 props.setProperty("mail.transport.protocol", "smtp");//指定郵件發送的協議,參數是規範規定的 props.setProperty("mail.host", "smtp.163.com");//指定發件伺服器的地址,參數是規範規定的// props.setProperty("mail.debug", "true");//郵件發送的偵錯模式,參數是規範規定的 props.setProperty("mail.smtp.auth", "true");//請求伺服器進行身份認證。參數與具體的JavaMail實現有關 Session session = Session.getInstance(props);//發送郵件時使用的環境配置 session.setDebug(true); MimeMessage message = new MimeMessage(session); //設定郵件的頭 message.setFrom(new InternetAddress("xxx@163.com")); message.setRecipients(Message.RecipientType.TO, "xxx@qq.com"); message.setSubject("This is second message"); //設定本文 //搞出文本部分 MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent("aaa<img src='cid:mm'/>aaa", "text/html"); //搞圖片部分 MimeBodyPart imagePart = new MimeBodyPart(); imagePart.setContentID("mm"); //把磁碟上的檔案加到part中使用到了JAF架構 DataHandler dh = new DataHandler(new FileDataSource("src/0.jpg")); imagePart.setDataHandler(dh); MimeMultipart mp = new MimeMultipart(); mp.addBodyPart(textPart); mp.addBodyPart(imagePart); mp.setSubType("related");//有關係的 message.setContent(mp); message.saveChanges(); //發送郵件 Transport ts = session.getTransport(); ts.connect("xxx@163.com", "123456"); //密碼為授權碼不是郵箱的登入密碼 ts.sendMessage(message, message.getAllRecipients());//對象,用執行個體方法 }}
三、帶有文本、圖片、附件的郵件
代碼案例:
package com.lyh.sendemail;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Message;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 javax.mail.internet.MimeUtility;//發送郵件public class MessageDemo3 { public static void main(String[] args) throws Exception{ Properties props = new Properties();//key value:配置參數。真正發送郵件時再配置 props.setProperty("mail.transport.protocol", "smtp");//指定郵件發送的協議,參數是規範規定的 props.setProperty("mail.host", "smtp.163.com");//指定發件伺服器的地址,參數是規範規定的// props.setProperty("mail.debug", "true");//郵件發送的偵錯模式,參數是規範規定的 props.setProperty("mail.smtp.auth", "true");//請求伺服器進行身份認證。參數與具體的JavaMail實現有關 Session session = Session.getInstance(props);//發送郵件時使用的環境配置// session.setDebug(true); MimeMessage message = new MimeMessage(session); //設定郵件的頭 message.setFrom(new InternetAddress("xxx@163.com")); message.setRecipients(Message.RecipientType.TO, "xxxqq.com"); message.setSubject("這是一封複雜的郵件"); //設定本文 //搞出文本部分 MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent("美女<img src='cid:mm'/>aaa", "text/html;charset=UTF-8"); //搞圖片部分 MimeBodyPart imagePart = new MimeBodyPart(); imagePart.setContentID("mm"); //把磁碟上的檔案加到part中使用到了JAF架構 DataHandler dh = new DataHandler(new FileDataSource("src/0.jpg")); imagePart.setDataHandler(dh); MimeMultipart mp = new MimeMultipart(); mp.addBodyPart(textPart); mp.addBodyPart(imagePart); mp.setSubType("related");//有關係的 MimeBodyPart textImagePart = new MimeBodyPart(); //將 MimeMultipart 添加到 MimeBodyPart實現附件的發送 textImagePart.setContent(mp); //建立附件部分 MimeBodyPart attachmentPart = new MimeBodyPart(); dh = new DataHandler(new FileDataSource("src/賬戶.rar")); String filename = dh.getName(); attachmentPart.setDataHandler(dh); //手工設定檔案名稱 防止亂碼使用 javaMail裡的 MimeUtility進行編碼 attachmentPart.setFileName(MimeUtility.encodeText(filename)); //最終的 MimeMultipart MimeMultipart finalMp = new MimeMultipart(); finalMp.addBodyPart(attachmentPart); finalMp.addBodyPart(textImagePart); finalMp.setSubType("mixed"); message.setContent(finalMp); message.saveChanges(); //發送郵件 Transport ts = session.getTransport(); ts.connect("xxx@163.com", "123456"); //密碼為授權碼不是郵箱的登入密碼 ts.sendMessage(message, message.getAllRecipients());//對象,用執行個體方法 }}