一、只帶有純文本的郵件
代碼事例如下:
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());//對象,用實例方法
}
}