JavaWeb中JavaMail創立郵件和發送郵件。本站提示廣大學習愛好者:(JavaWeb中JavaMail創立郵件和發送郵件)文章只能為提供參考,不一定能成為您想要的結果。以下是JavaWeb中JavaMail創立郵件和發送郵件正文
1、RFC882文檔簡略解釋
RFC882文檔劃定了若何編寫一封簡略的郵件(純文本郵件),一封簡略的郵件包括郵件頭和郵件體兩個部門,郵件頭和郵件體之間應用空行分隔。
郵件頭包括的內容有:
from字段 --用於指明發件人
to字段 --用於指明收件人
subject字段 --用於解釋郵件主題
cc字段 -- 抄送,將郵件發送給收件人的同時抄送給另外一個收件人,收件人可以看到郵件抄送給了誰
bcc字段 -- 密送,將郵件發送給收件人的同時將郵件機密發送給另外一個收件人,收件人沒法看到郵件密送給了誰
郵件體指的就是郵件的詳細內容。
2、MIME協定簡略引見
在我們的現實開辟傍邊,一封郵件既能夠包括圖片,又能夠包括有附件,在如許的情形下,RFC882文檔劃定的郵件格局就沒法知足請求了。
MIME協定是對RFC822文檔的進級和彌補,它描寫了若何臨盆一封龐雜的郵件。平日我們把MIME協定描寫的郵件稱之為MIME郵件。MIME協定描寫的數據稱之為MIME新聞。
關於一封龐雜郵件,假如包括了多個分歧的數據,MIME協定劃定了要應用分隔線對多段數據停止分隔,並應用Content-Type頭字段對數據的類型、和多個數據之間的關系停止描寫。
3、應用JavaMail創立郵件和發送郵件
JavaMail創立的郵件是基於MIME協定的。是以可使用JavaMail創立出包括圖片,包括附件的龐雜郵件。
3.1、JavaMail API的簡略引見
3.2、創立郵件發送測試項目
3.3、發送一封只包括文本的簡略郵件
package me.gacl.main;
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;
/**
* @ClassName: Sendmail
* @Description: 發送Email
* @author: 孤獨蒼狼
* @date: 2015-1-12 下晝9:42:56
*
*/
public class Sendmail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//應用JavaMail發送郵件的5個步調
//1、創立session
Session session = Session.getInstance(prop);
//開啟Session的debug形式,如許便可以檢查到法式發送Email的運轉狀況
session.setDebug(true);
//2、經由過程session獲得transport對象
Transport ts = session.getTransport();
//3、應用郵箱的用戶名和暗碼連上郵件辦事器,發送郵件時,發件人須要提交郵箱的用戶名和暗碼給smtp辦事器,用戶名和暗碼都經由過程驗證以後能力夠正常發送郵件給收件人。
ts.connect("smtp.sohu.com", "gacl", "郵箱暗碼");
//4、創立郵件
Message message = createSimpleMail(session);
//5、發送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createSimpleMail
* @Description: 創立一封只包括文本的郵件
* @Anthor:孤獨蒼狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createSimpleMail(Session session)
throws Exception {
//創立郵件對象
MimeMessage message = new MimeMessage(session);
//指明郵件的發件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//指明郵件的收件人,如今發件人和收件人是一樣的,那就是本身給本身發
message.setRecipient(Message.RecipientType.TO, new InternetAddress("gacl@sohu.com"));
//郵件的題目
message.setSubject("只包括文本的簡略郵件");
//郵件的文本內容
message.setContent("你好啊!", "text/html;charset=UTF-8");
//前往創立好的郵件對象
return message;
}
}
3.4、發送包括內嵌圖片的郵件
package me.gacl.main;
import java.io.FileOutputStream;
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;
/**
* @ClassName: Sendmail
* @Description: 發送Email
* @author: 孤獨蒼狼
* @date: 2015-1-12 下晝9:42:56
*
*/
public class Sendmail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//應用JavaMail發送郵件的5個步調
//1、創立session
Session session = Session.getInstance(prop);
//開啟Session的debug形式,如許便可以檢查到法式發送Email的運轉狀況
session.setDebug(true);
//2、經由過程session獲得transport對象
Transport ts = session.getTransport();
//3、連上郵件辦事器,須要發件人供給郵箱的用戶名和暗碼停止驗證
ts.connect("smtp.sohu.com", "gacl", "郵箱暗碼");
//4、創立郵件
Message message = createImageMail(session);
//5、發送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createImageMail
* @Description: 生成一封郵件注釋帶圖片的郵件
* @Anthor:孤獨蒼狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createImageMail(Session session) throws Exception {
//創立郵件
MimeMessage message = new MimeMessage(session);
// 設置郵件的根本信息
//發件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
//郵件題目
message.setSubject("帶圖片的郵件");
// 預備郵件數據
// 預備郵件注釋數據
MimeBodyPart text = new MimeBodyPart();
text.setContent("這是一封郵件注釋帶圖片<img src='cid:xxx.jpg'>的郵件", "text/html;charset=UTF-8");
// 預備圖片數據
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\1.jpg"));
image.setDataHandler(dh);
image.setContentID("xxx.jpg");
// 描寫數據關系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
message.setContent(mm);
message.saveChanges();
//將創立好的郵件寫入到E盤以文件的情勢停止保留
message.writeTo(new FileOutputStream("E:\\ImageMail.eml"));
//前往創立好的郵件
return message;
}
}
3.5、發送包括附件的郵件
package me.gacl.main;
import java.io.FileOutputStream;
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;
/**
* @ClassName: Sendmail
* @Description: 發送Email
* @author: 孤獨蒼狼
* @date: 2015-1-12 下晝9:42:56
*
*/
public class Sendmail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//應用JavaMail發送郵件的5個步調
//1、創立session
Session session = Session.getInstance(prop);
//開啟Session的debug形式,如許便可以檢查到法式發送Email的運轉狀況
session.setDebug(true);
//2、經由過程session獲得transport對象
Transport ts = session.getTransport();
//3、連上郵件辦事器
ts.connect("smtp.sohu.com", "gacl", "郵箱暗碼");
//4、創立郵件
Message message = createAttachMail(session);
//5、發送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createAttachMail
* @Description: 創立一封帶附件的郵件
* @Anthor:孤獨蒼狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createAttachMail(Session session) throws Exception{
MimeMessage message = new MimeMessage(session);
//設置郵件的根本信息
//發件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
//郵件題目
message.setSubject("JavaMail郵件發送測試");
//創立郵件注釋,為了不郵件注釋中文亂碼成績,須要應用charset=UTF-8指明字符編碼
MimeBodyPart text = new MimeBodyPart();
text.setContent("應用JavaMail創立的帶附件的郵件", "text/html;charset=UTF-8");
//創立郵件附件
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\2.jpg"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName()); //
//創立容器描寫數據關系
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed");
message.setContent(mp);
message.saveChanges();
//將創立的Email寫入到E盤存儲
message.writeTo(new FileOutputStream("E:\\attachMail.eml"));
//前往生成的郵件
return message;
}
}
3.6、發送包括內嵌圖片和附件的龐雜郵件
package me.gacl.main;
import java.io.FileOutputStream;
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;
/**
* @ClassName: Sendmail
* @Description: 發送Email
* @author: 孤獨蒼狼
* @date: 2015-1-12 下晝9:42:56
*
*/
public class Sendmail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//應用JavaMail發送郵件的5個步調
//1、創立session
Session session = Session.getInstance(prop);
//開啟Session的debug形式,如許便可以檢查到法式發送Email的運轉狀況
session.setDebug(true);
//2、經由過程session獲得transport對象
Transport ts = session.getTransport();
//3、連上郵件辦事器
ts.connect("smtp.sohu.com", "gacl", "郵箱暗碼");
//4、創立郵件
Message message = createMixedMail(session);
//5、發送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createMixedMail
* @Description: 生成一封帶附件和帶圖片的郵件
* @Anthor:孤獨蒼狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createMixedMail(Session session) throws Exception {
//創立郵件
MimeMessage message = new MimeMessage(session);
//設置郵件的根本信息
message.setFrom(new InternetAddress("gacl@sohu.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
message.setSubject("帶附件和帶圖片的的郵件");
//注釋
MimeBodyPart text = new MimeBodyPart();
text.setContent("xxx這是女的xxxx<br/><img src='cid:aaa.jpg'>","text/html;charset=UTF-8");
//圖片
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src\\3.jpg")));
image.setContentID("aaa.jpg");
//附件1
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\4.zip"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
//附件2
MimeBodyPart attach2 = new MimeBodyPart();
DataHandler dh2 = new DataHandler(new FileDataSource("src\\波子.zip"));
attach2.setDataHandler(dh2);
attach2.setFileName(MimeUtility.encodeText(dh2.getName()));
//描寫關系:注釋和圖片
MimeMultipart mp1 = new MimeMultipart();
mp1.addBodyPart(text);
mp1.addBodyPart(image);
mp1.setSubType("related");
//描寫關系:注釋和附件
MimeMultipart mp2 = new MimeMultipart();
mp2.addBodyPart(attach);
mp2.addBodyPart(attach2);
//代表注釋的bodypart
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp1);
mp2.addBodyPart(content);
mp2.setSubType("mixed");
message.setContent(mp2);
message.saveChanges();
message.writeTo(new FileOutputStream("E:\\MixedMail.eml"));
//前往創立好的的郵件
return message;
}
}
以上就是應用JavaMail的API創立郵件和發送郵件的全體內容,願望對年夜家的進修有所贊助。