同一個目標,同一個夢想

          One Target,One Dream
          posts - 25, comments - 11, trackbacks - 0, articles - 37

          JavaMail的使用之郵件發送

          Posted on 2009-02-22 12:00 J2EE Home工作室 閱讀(143) 評論(0)  編輯  收藏 所屬分類: JavaMail

          對于JavaMail,最基礎的功能就是郵件的發送和接收,在這里,我先講一講郵件的發送。

          在寫具體的程序前,先講一些概念。1.郵件的發送:如果你的郵件地址是a@host.com,而你要用這個郵箱發送一封郵件到to@tohost.com,這個發送過程是怎樣的呢,你以為是先連接到tohost.com這服務器上,然后把郵件發送出去嗎?其實不然。最初,你需要連接到服務器host.com上,當然這個連接可能需要認證,然后是發送郵件到服務器host.com上,關閉連接。在host.com上,你所發送的郵件進入發送隊列中,輪到你要發送的郵件時,host.com主機再聯系tohost.com,將郵件傳輸到服務器tohost.com上。2.一些垃圾郵件的發送:在垃圾郵件中,可能大部分的發件人的地址都是假的,這是怎么回事呢?實際上在發送這些垃圾郵件的時候,這里的host.com有些特別,可能host.com不需要對用戶進行認證,也可能發送垃圾郵件的人本來就控制著服務器host.com,然后控制著host.com向其他服務器,如tohost.com,發送郵件,而發送郵件的內容可以被控制,發件人的地址就可以隨便填寫。

          發送郵件主要包括3個部分:創建連接,創建郵件體,發送郵件

          JavaMail中,是使用會話(Session)來管理連接的。創建一個連接,就需要創建一個會話。在會話中,有兩個重要的因素,一是會話的屬性,二是會話的認證。在我們使用Hotmail等郵件工具的時候,就要設置”SMTP服務器身份驗證,也就是這里的會話的認證。

           

           

          首先,創建一個連接屬性。

          Properties props = new Properties();

          props.put("mail.smtp.host","smtp.126.com"); //設置smtp的服務器地址是smtp.126.com

          props.put("mail.smtp.auth","true");          //設置smtp服務器要身份驗證。

           

           

          在創建一個身份驗證。身份驗證稍微復雜一點,要創建一個Authenticator的子類,并重載getPasswordAuthentication()方法,代碼如下:

          class PopupAuthenticator extends Authenticator {

              public PasswordAuthentication getPasswordAuthentication() {

                  String username = "cqhcp"; //126郵箱登錄帳號

                  String pwd = "12345"; //登錄密碼

                  return new PasswordAuthentication(username, pwd);

              }

          }

          創建身份驗證的實例:

          PopupAuthenticator auth = new PopupAuthenticator();

           

          創建會話: 關于會話的創建,有兩種方法,具體請參看后續的文章,這里只簡單使用一種。

          Session session = Session.getInstance(props, auth);

           

           

          定義郵件地址:

          //發送人地址

          Address addressFrom = new InternetAddress("cqhcp@126.com", "George Bush");

          //收件人地址

          Address addressTo = new InternetAddress("webmaster@javazy.com", "George Bush");

          //抄送地址

          Address addressCopy = new InternetAddress("haocongping@gmail.com", "George Bush");

           

           

          創建郵件體:

          message.setContent("Hello", "text/plain");//或者使用message.setText("Hello");更詳細的信息請參看后續文章.

          message.setSubject("Title");

          message.setFrom(addressFrom);

          message.addRecipient(Message.RecipientType.TO,addressTo);

          message.addRecipient(Message.RecipientType.CC,addressCopy);

          message.saveChanges();

           

           

          發送郵件的過程:

          Transport transport = session.getTransport("smtp");//創建連接

          transport.connect("smtp.126.com", "cqhcp", "12345");//連接服務器

          transport.send(message);//發送信息

          transport.close();//關閉連接

           

           

          整體程序的代碼如下:

          class PopupAuthenticator extends Authenticator {

              public PasswordAuthentication getPasswordAuthentication() {

                  String username = "cqhcp"; //163郵箱登錄帳號

                  String pwd = "12345"; //登錄密碼

                  return new PasswordAuthentication(username, pwd);

              }

          }

           

           

          Properties props = new Properties();

          props.put("mail.smtp.host","smtp.126.com");

          props.put("mail.smtp.auth","true");

          PopupAuthenticator auth = new PopupAuthenticator();

          Session session = Session.getInstance(props, auth);

          MimeMessage message = new MimeMessage(session);

                     

          Address addressFrom = new InternetAddress("cqhcp@126.com", "George Bush");

          Address addressTo = new InternetAddress("webmaster@javazy.com", "George Bush");

          Address addressCopy = new InternetAddress("haocongping@gmail.com", "George Bush");

           

          message.setContent("Hello", "text/plain");//或者使用message.setText("Hello");

          message.setSubject("Title");

          message.setFrom(addressFrom);

          message.addRecipient(Message.RecipientType.TO,addressTo);

          message.addRecipient(Message.RecipientType.CC,addressCopy);

          message.saveChanges();

           

          Transport transport = session.getTransport("smtp");

          transport.connect("smtp.126.com", "cqhcp", "12345");

          transport.send(message);

          transport.close();


          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 寻甸| 永康市| 泰和县| 扬州市| 呼图壁县| 周宁县| 屏东市| 金坛市| 临沂市| 正安县| 汝城县| 万山特区| 陇西县| 丹寨县| 山阳县| 长武县| 仁寿县| 平谷区| 鄂尔多斯市| 和政县| 丰原市| 恭城| 崇信县| 衡南县| 玉环县| 肃宁县| 宽城| 霍山县| 商洛市| 分宜县| 黑水县| 库尔勒市| 绥德县| 林口县| 顺昌县| 凤台县| 蒙阴县| 泰顺县| 大丰市| 大宁县| 北安市|