最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java Mail與Apache Mail發(fā)送郵件示例

 更新時(shí)間:2014年10月28日 14:56:49   投稿:hebedich  
這篇文章主要介紹了Java Mail與Apache Mail發(fā)送郵件示例的相關(guān)資料,需要的朋友可以參考下

一、郵件簡(jiǎn)介

一封郵件由很多信息構(gòu)成,主要的信息如下,其他的暫時(shí)不考慮,例如抄送等:

1、收件人:收件人的郵箱地址,例如xxx@xx.com

2、收件人姓名:大部分的郵件顯示時(shí)都會(huì)顯示,例如loadfate 779554589@qq.com

3、發(fā)件人:發(fā)件人的郵箱地址

4、發(fā)件人姓名:

5、主題:郵件的標(biāo)題

6、內(nèi)容及附件:郵件的主要內(nèi)容

二、使用Java發(fā)郵件的通用步驟

一般的項(xiàng)目中沒有單獨(dú)的郵件服務(wù)器,一般情況下都是使用別人的服務(wù)器。

1、設(shè)置smtp服務(wù)器:不同的郵件服務(wù)器有不同的地址,例如:smtp.qq.com表示騰訊的smtp服務(wù)器。

2、授權(quán):使用該服務(wù)器的帳號(hào)和密碼登錄該服務(wù)器。

3、創(chuàng)建郵件:創(chuàng)建一份包含所有信息的郵件,比如發(fā)件人、收件人、內(nèi)容等。

4、設(shè)置郵件的屬性:為郵件的屬性添加數(shù)據(jù)。

5、發(fā)送郵件:因?yàn)榉庋b不同,發(fā)送的方式不一致。

三、Java Mail與Apache Mail

Apache Mail是對(duì)Java Mail的封裝,使用起來(lái)更加的簡(jiǎn)便,邏輯層次感更好。

使用Java Mail只需要導(dǎo)入一個(gè)jar包:mail.jar。

使用Apache Mail的時(shí)候需要導(dǎo)入兩個(gè)jar包:mail.jar、commons-email-1.3.1.jar。

四、使用Java Mail發(fā)送郵件

復(fù)制代碼 代碼如下:

 public static void main(String[] args) throws Exception {
         final String user = "779554589";
         final String password = "";
         String fromAddress = "779554589@qq.com";
         String toAddress = "loadfate@163.com";
         String subject = "郵件測(cè)試主題";
         String content = "這是一個(gè)測(cè)試郵件<b>哈哈</b>";
         //配置參數(shù)
         Properties props = new Properties();
         props.setProperty("mail.smtp.auth", "true");
         props.setProperty("mail.transport.protocol", "smtp");
         props.setProperty("mail.host", "smtp.qq.com");
         // 方法一:使用transport對(duì)象發(fā)送郵件
         {
             //通過(guò)參數(shù)生成會(huì)話
             Session session = Session.getInstance(props);
             //啟用調(diào)試模式
             session.setDebug(true);
             //創(chuàng)建一封郵件,并設(shè)置信息
             Message message = new MimeMessage(session);
             message.setFrom(new InternetAddress(fromAddress));
             message.setSubject(subject);
             message.setText(content);
             //創(chuàng)建傳輸
             Transport transport = session.getTransport();
             //連接smtp服務(wù)器
             transport.connect(user, password);
             //發(fā)送
             transport.sendMessage(message, new InternetAddress[] { new InternetAddress(toAddress) });
             transport.close();
         }
         // 方法二:使用Transport類靜態(tài)方法發(fā)送郵件
         {
             //生成Session時(shí)以獲取授權(quán)連接
             Session session = Session.getInstance(props, new Authenticator() {
                 @Override
                 protected PasswordAuthentication getPasswordAuthentication() {
                     return new PasswordAuthentication(user, password);
                 }
             });
             session.setDebug(true);
             //創(chuàng)建一封郵件,并設(shè)置信息
             Message message = new MimeMessage(session);
             message.setSubject(subject);
             message.setFrom(new InternetAddress(fromAddress));
             message.setRecipient(RecipientType.TO, new InternetAddress(toAddress));
             message.setContent(content, "text/html;charset=utf-8");
             //直接發(fā)送,message通過(guò)已經(jīng)授權(quán)的Session生成
             Transport.send(message);
         }
     }

五、使用Apache Mail發(fā)送郵件

復(fù)制代碼 代碼如下:

 public class ApacheMailTest {
     // smtp服務(wù)器
     private String hostName = "smtp.qq.com";
     // 帳號(hào)與密碼
     private String userName = "779554589";
     private String password = "這是個(gè)秘密";
     // 發(fā)件人
     private String fromAddress = "779554589@qq.com";
     // 發(fā)件人姓名
     private String fromName = "loadfate";
     public static void main(String[] args) throws Exception {
         // 收件人與收件人名字
         String toAddress = "loadfate@163.com";
         String toName = "loadfate";
         ApacheMailTest test = new ApacheMailTest();
         // 所有的異常都為處理,方便瀏覽
         test.sendSimpleEmail(toAddress, toName);
         test.sendHtmlEmail(toAddress, toName);
         test.sendMultiPartEmail(toAddress, toName);
         System.out.println("發(fā)送完成");
     }
     // 發(fā)送簡(jiǎn)單郵件,類似一條信息
     public void sendSimpleEmail(String toAddress, String toName) throws Exception {
         SimpleEmail email = new SimpleEmail();
         email.setHostName(hostName);// 設(shè)置smtp服務(wù)器
         email.setAuthentication(userName, password);// 設(shè)置授權(quán)信息
         email.setCharset("utf-8");
         email.setFrom(fromAddress, fromName, "utf-8");// 設(shè)置發(fā)件人信息
         email.addTo(toAddress, toName, "utf-8");// 設(shè)置收件人信息
         email.setSubject("測(cè)試主題");// 設(shè)置主題
         email.setMsg("這是一個(gè)簡(jiǎn)單的測(cè)試!");// 設(shè)置郵件內(nèi)容
         email.send();// 發(fā)送郵件
     }
     // 發(fā)送Html內(nèi)容的郵件
     public void sendHtmlEmail(String toAddress, String toName) throws Exception {
         HtmlEmail email = new HtmlEmail();
         email.setHostName(hostName);
         email.setAuthentication(userName, password);
         email.setCharset("utf-8");
         email.addTo(toAddress, toName, "utf-8");
         email.setFrom(fromAddress, fromName, "utf-8");
         email.setSubject("這是一個(gè)html郵件");
         // 設(shè)置html內(nèi)容,實(shí)際使用時(shí)可以從文本讀入寫好的html代碼
         email.setHtmlMsg("<div style='width:100px;height:200px;'>a</div>");
         email.send();
     }
     // 發(fā)送復(fù)雜的郵件,包含附件等
     public void sendMultiPartEmail(String toAddress, String toName) throws Exception {
         MultiPartEmail email = null;
         email = new MultiPartEmail();
         email.setHostName(hostName);
         email.setAuthentication(userName, password);
         email.setCharset("utf-8");
         email.addTo(toAddress, toName, "utf-8");
         email.setFrom(fromAddress, fromName, "utf-8");
         email.setSubject("這是有附件的郵件");
         email.setMsg("<a href='#'>測(cè)試內(nèi)容</a>");
         // 為郵件添加附加內(nèi)容
         EmailAttachment attachment = new EmailAttachment();
         attachment.setPath("D:\\郵件.txt");// 本地文件
         // attachment.setURL(new URL("http://xxx/a.gif"));//遠(yuǎn)程文件
         attachment.setDisposition(EmailAttachment.ATTACHMENT);
         attachment.setDescription("描述信息");
         // 設(shè)置附件顯示名字,必須要編碼,不然中文會(huì)亂碼
         attachment.setName(MimeUtility.encodeText("郵件.txt"));
         // 將附件添加到郵件中
         email.attach(attachment);
         email.send();
     }
 }

 

六、附件
項(xiàng)目文件夾:maildemo

下載地址:http://pan.baidu.com/s/1bn1Y6BX

如果有什么疑問(wèn)或者建議,請(qǐng)聯(lián)系我

文件說(shuō)明:

1、maildemo.zip :maildemo的源代碼

2、mail.jar :Java Mail的jar包

3、commons-email-1.3.1.jar :Apache Mail的jar包

相關(guān)文章

最新評(píng)論

马边| 义马市| 广州市| 岚皋县| 卢氏县| 巨鹿县| 兰州市| 天镇县| 海宁市| 内江市| 横山县| 墨脱县| 曲靖市| 牡丹江市| 三台县| 长治县| 翼城县| 临汾市| 青龙| 和田市| 荆州市| 海林市| 且末县| 同心县| 鹰潭市| 鹤壁市| 富蕴县| 白朗县| 杂多县| 石首市| 涞源县| 顺昌县| 蓬溪县| 关岭| 湖南省| 灵石县| 宿州市| 横山县| 临西县| 无为县| 上林县|