java郵件開發--心得3-第一個java mail

來源:互聯網
上載者:User

1.  java mail介紹

2.  jaf介紹

3. 第一個: javaMail 執行個體分析

package com.csdn.itcast;import java.util.Properties;import javax.mail.Address;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class Demo1 {/** * @param args * @throws MessagingException * @throws AddressException *  *             開發時提示找不到某些驅動的事,要看看是不是忘了匯入某些包 */public static void main(String[] args) throws AddressException,MessagingException {// 5.執行個體化session的時候需要傳入一個props屬性Properties props = new Properties();// 設定登入的認證方式props.setProperty("mail.smtp.auth", "true");// 設定傳輸協議props.setProperty("mail.transport.protocol", "smtp");// 4.建立衛星的時候需要傳入一個sessionSession session = Session.getInstance(props);session.setDebug(true);// 1.建立message資訊,好比建立衛星;mimeMessage是message的子類,因為message是抽象類別,所以這裡執行個體化它的子類Message msg = new MimeMessage(session);// 設定郵件的內容和寄件者,注意:這裡寄件者是設定的,固你可以隨意填寫寄件者郵箱,假的都無所謂msg.setText("你好。。");msg.setFrom(new InternetAddress("yang@163.com"));// 2.建立發送郵件的工具,好比建立火箭Transport transport = session.getTransport();transport.connect("smtp.sina.com", 25, "itcast_test", "123456");/* * 3.用火箭發射衛星;sendMessage()方法是Transport的對象調用的方法,send()則是Transport直接調用的方法,即靜態方法 * ; 後者是把transport串連融合一塊的方法,適合發送一條郵件的情況,因為如果發送多條會設定多個重複串連;而前者則相反 */transport.sendMessage(msg, new Address[] { new InternetAddress("itcast_test@sohu.com") });// transport.send(msg, new Address[]{new// InternetAddress("itcast_test@sohu.com")});// 6.關閉資源transport.close();}}4.第二個:複雜一點的案例package com.csdn.itcast;import java.util.Properties;import javax.mail.Address;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMessage.RecipientType;import javax.mail.search.ReceivedDateTerm;public class Demo2 {/** * @param args */public static void main(String[] args) throws Exception{Properties props = new Properties();props.setProperty("mail.smtp.auth", "true");props.setProperty("mail.transport.protocol", "smtp");props.setProperty("mail.host", "smtp.sina.com");//下面用策略模式編寫:這裡在建立session的時候直接傳入一個面對資訊反饋器(new 一個Authenticator匿名內部類),來設定登入名稱和登入密碼Session session = Session.getInstance(props,new Authenticator() {protected PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication("itcast_test","123456");}});session.setDebug(true);Message msg = new MimeMessage(session);msg.setFrom(new InternetAddress("itcast_xxx@sina.com"));msg.setSubject("中文主題");//這裡用InternetAddress.parse()方法與前面案例用到的new Address[] {}達到的效果一樣,parse()可以傳入多個參數返回一個InternetAddress型的數組msg.setRecipients(RecipientType.TO,InternetAddress.parse("itcast_test@sina.com,itcast_test@sohu.com"));msg.setContent("<span style='color:red'>你傻啊。</span>","text/html;charset=UTF-8");Transport.send(msg);}}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.