欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaWeb中JavaMail創(chuàng)建郵件和發(fā)送郵件

 更新時(shí)間:2022年08月11日 08:33:48   作者:孤傲蒼狼  
這篇文章主要介紹了JavaWeb中JavaMail創(chuàng)建郵件和發(fā)送郵件,較為詳細(xì)的分析了JavaMail發(fā)送郵件的用法,是非常實(shí)用的技巧,需要的朋友可以參考下

一、RFC882文檔簡(jiǎn)單說(shuō)明

RFC882文檔規(guī)定了如何編寫一封簡(jiǎn)單的郵件(純文本郵件),一封簡(jiǎn)單的郵件包含郵件頭和郵件體兩個(gè)部分,郵件頭和郵件體之間使用空行分隔。

郵件頭包含的內(nèi)容有:

from字段   --用于指明發(fā)件人
to字段       --用于指明收件人
subject字段  --用于說(shuō)明郵件主題
cc字段      -- 抄送,將郵件發(fā)送給收件人的同時(shí)抄送給另一個(gè)收件人,收件人可以看到郵件抄送給了誰(shuí)
bcc字段    -- 密送,將郵件發(fā)送給收件人的同時(shí)將郵件秘密發(fā)送給另一個(gè)收件人,收件人無(wú)法看到郵件密送給了誰(shuí)郵件體指的就是郵件的具體內(nèi)容。

二、MIME協(xié)議簡(jiǎn)單介紹

在我們的實(shí)際開(kāi)發(fā)當(dāng)中,一封郵件既可能包含圖片,又可能包含有附件,在這樣的情況下,RFC882文檔規(guī)定的郵件格式就無(wú)法滿足要求了。

MIME協(xié)議是對(duì)RFC822文檔的升級(jí)和補(bǔ)充,它描述了如何生產(chǎn)一封復(fù)雜的郵件。通常我們把MIME協(xié)議描述的郵件稱之為MIME郵件。MIME協(xié)議描述的數(shù)據(jù)稱之為MIME消息。

對(duì)于一封復(fù)雜郵件,如果包含了多個(gè)不同的數(shù)據(jù),MIME協(xié)議規(guī)定了要使用分隔線對(duì)多段數(shù)據(jù)進(jìn)行分隔,并使用Content-Type頭字段對(duì)數(shù)據(jù)的類型、以及多個(gè)數(shù)據(jù)之間的關(guān)系進(jìn)行描述。

三、使用JavaMail創(chuàng)建郵件和發(fā)送郵件

JavaMail創(chuàng)建的郵件是基于MIME協(xié)議的。因此可以使用JavaMail創(chuàng)建出包含圖片,包含附件的復(fù)雜郵件。

3.1、JavaMail API的簡(jiǎn)單介紹

  

  

  

3.2、創(chuàng)建郵件發(fā)送測(cè)試項(xiàng)目

  

3.3、發(fā)送一封只包含文本的簡(jiǎn)單郵件

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: 發(fā)送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發(fā)送郵件的5個(gè)步驟
 //1、創(chuàng)建session
 Session session = Session.getInstance(prop);
 //開(kāi)啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運(yùn)行狀態(tài)
 session.setDebug(true);
 //2、通過(guò)session得到transport對(duì)象
 Transport ts = session.getTransport();
 //3、使用郵箱的用戶名和密碼連上郵件服務(wù)器,發(fā)送郵件時(shí),發(fā)件人需要提交郵箱的用戶名和密碼給smtp服務(wù)器,用戶名和密碼都通過(guò)驗(yàn)證之后才能夠正常發(fā)送郵件給收件人。
 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼");
 //4、創(chuàng)建郵件
 Message message = createSimpleMail(session);
 //5、發(fā)送郵件
 ts.sendMessage(message, message.getAllRecipients());
 ts.close();
 }
 
 /**
 * @Method: createSimpleMail
 * @Description: 創(chuàng)建一封只包含文本的郵件
 * @Anthor:孤傲蒼狼
 *
 * @param session
 * @return
 * @throws Exception
 */ 
 public static MimeMessage createSimpleMail(Session session)
  throws Exception {
 //創(chuàng)建郵件對(duì)象
 MimeMessage message = new MimeMessage(session);
 //指明郵件的發(fā)件人
 message.setFrom(new InternetAddress("gacl@sohu.com"));
 //指明郵件的收件人,現(xiàn)在發(fā)件人和收件人是一樣的,那就是自己給自己發(fā)
 message.setRecipient(Message.RecipientType.TO, new InternetAddress("gacl@sohu.com"));
 //郵件的標(biāo)題
 message.setSubject("只包含文本的簡(jiǎn)單郵件");
 //郵件的文本內(nèi)容
 message.setContent("你好??!", "text/html;charset=UTF-8");
 //返回創(chuàng)建好的郵件對(duì)象
 return message;
 }
}

3.4、發(fā)送包含內(nèi)嵌圖片的郵件

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: 發(fā)送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發(fā)送郵件的5個(gè)步驟
 //1、創(chuàng)建session
 Session session = Session.getInstance(prop);
 //開(kāi)啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運(yùn)行狀態(tài)
 session.setDebug(true);
 //2、通過(guò)session得到transport對(duì)象
 Transport ts = session.getTransport();
 //3、連上郵件服務(wù)器,需要發(fā)件人提供郵箱的用戶名和密碼進(jìn)行驗(yàn)證
 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼");
 //4、創(chuàng)建郵件
 Message message = createImageMail(session);
 //5、發(fā)送郵件
 ts.sendMessage(message, message.getAllRecipients());
 ts.close();
 }
 
 /**
 * @Method: createImageMail
 * @Description: 生成一封郵件正文帶圖片的郵件
 * @Anthor:孤傲蒼狼
 *
 * @param session
 * @return
 * @throws Exception
 */ 
 public static MimeMessage createImageMail(Session session) throws Exception {
 //創(chuàng)建郵件
 MimeMessage message = new MimeMessage(session);
 // 設(shè)置郵件的基本信息
 //發(fā)件人
 message.setFrom(new InternetAddress("gacl@sohu.com"));
 //收件人
 message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
 //郵件標(biāo)題
 message.setSubject("帶圖片的郵件");

 // 準(zhǔn)備郵件數(shù)據(jù)
 // 準(zhǔn)備郵件正文數(shù)據(jù)
 MimeBodyPart text = new MimeBodyPart();
 text.setContent("這是一封郵件正文帶圖片<img src='cid:xxx.jpg'>的郵件", "text/html;charset=UTF-8");
 // 準(zhǔn)備圖片數(shù)據(jù)
 MimeBodyPart image = new MimeBodyPart();
 DataHandler dh = new DataHandler(new FileDataSource("src\\1.jpg"));
 image.setDataHandler(dh);
 image.setContentID("xxx.jpg");
 // 描述數(shù)據(jù)關(guān)系
 MimeMultipart mm = new MimeMultipart();
 mm.addBodyPart(text);
 mm.addBodyPart(image);
 mm.setSubType("related");

 message.setContent(mm);
 message.saveChanges();
 //將創(chuàng)建好的郵件寫入到E盤以文件的形式進(jìn)行保存
 message.writeTo(new FileOutputStream("E:\\ImageMail.eml"));
 //返回創(chuàng)建好的郵件
 return message;
 }
}

3.5、發(fā)送包含附件的郵件

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: 發(fā)送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發(fā)送郵件的5個(gè)步驟
 //1、創(chuàng)建session
 Session session = Session.getInstance(prop);
 //開(kāi)啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運(yùn)行狀態(tài)
 session.setDebug(true);
 //2、通過(guò)session得到transport對(duì)象
 Transport ts = session.getTransport();
 //3、連上郵件服務(wù)器
 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼");
 //4、創(chuàng)建郵件
 Message message = createAttachMail(session);
 //5、發(fā)送郵件
 ts.sendMessage(message, message.getAllRecipients());
 ts.close();
 }
 
 /**
 * @Method: createAttachMail
 * @Description: 創(chuàng)建一封帶附件的郵件
 * @Anthor:孤傲蒼狼
 *
 * @param session
 * @return
 * @throws Exception
 */ 
 public static MimeMessage createAttachMail(Session session) throws Exception{
 MimeMessage message = new MimeMessage(session);
 
 //設(shè)置郵件的基本信息
 //發(fā)件人
 message.setFrom(new InternetAddress("gacl@sohu.com"));
 //收件人
 message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
 //郵件標(biāo)題
 message.setSubject("JavaMail郵件發(fā)送測(cè)試");
 
 //創(chuàng)建郵件正文,為了避免郵件正文中文亂碼問(wèn)題,需要使用charset=UTF-8指明字符編碼
 MimeBodyPart text = new MimeBodyPart();
 text.setContent("使用JavaMail創(chuàng)建的帶附件的郵件", "text/html;charset=UTF-8");
 
 //創(chuàng)建郵件附件
 MimeBodyPart attach = new MimeBodyPart();
 DataHandler dh = new DataHandler(new FileDataSource("src\\2.jpg"));
 attach.setDataHandler(dh);
 attach.setFileName(dh.getName()); //
 
 //創(chuàng)建容器描述數(shù)據(jù)關(guān)系
 MimeMultipart mp = new MimeMultipart();
 mp.addBodyPart(text);
 mp.addBodyPart(attach);
 mp.setSubType("mixed");
 
 message.setContent(mp);
 message.saveChanges();
 //將創(chuàng)建的Email寫入到E盤存儲(chǔ)
 message.writeTo(new FileOutputStream("E:\\attachMail.eml"));
 //返回生成的郵件
 return message;
 }
}

3.6、發(fā)送包含內(nèi)嵌圖片和附件的復(fù)雜郵件

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: 發(fā)送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發(fā)送郵件的5個(gè)步驟
 //1、創(chuàng)建session
 Session session = Session.getInstance(prop);
 //開(kāi)啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運(yùn)行狀態(tài)
 session.setDebug(true);
 //2、通過(guò)session得到transport對(duì)象
 Transport ts = session.getTransport();
 //3、連上郵件服務(wù)器
 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼");
 //4、創(chuàng)建郵件
 Message message = createMixedMail(session);
 //5、發(fā)送郵件
 ts.sendMessage(message, message.getAllRecipients());
 ts.close();
 }
 
 /**
 * @Method: createMixedMail
 * @Description: 生成一封帶附件和帶圖片的郵件
 * @Anthor:孤傲蒼狼
 *
 * @param session
 * @return
 * @throws Exception
 */ 
 public static MimeMessage createMixedMail(Session session) throws Exception {
 //創(chuàng)建郵件
 MimeMessage message = new MimeMessage(session);
 
 //設(shè)置郵件的基本信息
 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()));
 
 //描述關(guān)系:正文和圖片
 MimeMultipart mp1 = new MimeMultipart();
 mp1.addBodyPart(text);
 mp1.addBodyPart(image);
 mp1.setSubType("related");
 
 //描述關(guān)系:正文和附件
 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"));
 //返回創(chuàng)建好的的郵件
 return message;
 }
}

以上就是使用JavaMail的API創(chuàng)建郵件和發(fā)送郵件的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • 詳解SpringBoot整合MyBatis詳細(xì)教程

    詳解SpringBoot整合MyBatis詳細(xì)教程

    這篇文章主要介紹了詳解SpringBoot整合MyBatis詳細(xì)教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • @FeignClient的使用和Spring?Boot的版本適配方式

    @FeignClient的使用和Spring?Boot的版本適配方式

    這篇文章主要介紹了@FeignClient的使用和Spring?Boot的版本適配方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 深入理解JVM之Class類文件結(jié)構(gòu)詳解

    深入理解JVM之Class類文件結(jié)構(gòu)詳解

    這篇文章主要介紹了深入理解JVM之Class類文件結(jié)構(gòu),結(jié)合實(shí)例形式詳細(xì)分析了Class類文件結(jié)構(gòu)相關(guān)概念、原理、結(jié)構(gòu)、常用方法與屬性,需要的朋友可以參考下
    2019-09-09
  • Springcloud中的Nacos?Config服務(wù)配置流程分析

    Springcloud中的Nacos?Config服務(wù)配置流程分析

    這篇文章主要介紹了Springcloud中的Nacos?Config服務(wù)配置,本文以用戶微服務(wù)為例,進(jìn)行統(tǒng)一的配置,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • 使用ServletInputStream()輸入流讀取圖片方式

    使用ServletInputStream()輸入流讀取圖片方式

    這篇文章主要介紹了使用ServletInputStream()輸入流讀取圖片方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 一文詳解RabbitMQ如何保證消息可靠性

    一文詳解RabbitMQ如何保證消息可靠性

    這篇文章將詳細(xì)介紹RabbitMQ的消息可靠性機(jī)制,如消息丟失,消息重復(fù)性消費(fèi),消息積壓等問(wèn)題,具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-07-07
  • 詳解SpringMVC @RequestBody接收J(rèn)son對(duì)象字符串

    詳解SpringMVC @RequestBody接收J(rèn)son對(duì)象字符串

    這篇文章主要介紹了詳解SpringMVC @RequestBody接收J(rèn)son對(duì)象字符串,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • IDEA類和方法注釋模板設(shè)置(非常詳細(xì))

    IDEA類和方法注釋模板設(shè)置(非常詳細(xì))

    這篇文章主要介紹了IDEA類和方法注釋模板設(shè)置(非常詳細(xì)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • 使用springboot整合RateLimiter限流過(guò)程

    使用springboot整合RateLimiter限流過(guò)程

    這篇文章主要介紹了使用springboot整合RateLimiter限流過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 解決@Autowired注入空指針問(wèn)題(利用Bean的生命周期)

    解決@Autowired注入空指針問(wèn)題(利用Bean的生命周期)

    這篇文章主要介紹了解決@Autowired注入空指針問(wèn)題(利用Bean的生命周期),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02

最新評(píng)論