1. 程式人生 > >JavaWeb筆記-22-JavaMail

JavaWeb筆記-22-JavaMail

1、JavaMail


是Java提供的一組API,用來收發郵件

郵件伺服器:郵件中轉站

常見郵件伺服器名稱及埠號:
    埠號:
        1)smtp伺服器的埠號為25,伺服器名稱為smtp.xxx.xxx。
        2)pop3伺服器的埠號為110,伺服器名稱為pop3.xxx.xxx。

    伺服器名稱:  
        1)163:smtp.163.com和pop3.163.com;
        2)126:smtp.126.com和pop3.126.com;
        3)qq:smtp.qq.com和pop3.qq.com;
        4)sohu:smtp.sohu.com和pop3.sohu.com;
        5)sina:smtp.sina.com和pop3.sina.com。


郵件協議:
    SMTP:發郵件協議。(Simple Mail Transfer Protocol,簡單郵件傳輸協議)
    POP3:收郵件協議。(Post Office Protocol Version 3,郵局協議第3版)
    IMAP:收發郵件皆可協議。(Internet Message Access Protocol,因特網訊息訪問協議)

2、理解郵件收發過程。


相同伺服器間的收發

不同伺服器間的收發

3、JavaMail核心類介紹:


1)Session :得到Session,表示與伺服器成功連線,與Connection作用相似

      ***得到Session:Session.getInstance(Properties, Authenticator);
        引數1:鍵值對(指定伺服器主機名,指定是否需要認證。)
        引數2:介面認證器,即校驗客戶端身份,介面需要自己實現
    //建立Session程式碼示:
      //得到鍵值對。
          Properites props = new
Properties(); props.setProperty("mail.host", "smtp.163.com"); //指定伺服器主機 props.setProperty("mail.smtp.auth", "true"); //指定是否需要認證 //實現介面 Authenticator auth = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { //返回值為:("使用者名稱","密碼");
return new PasswordAuthentication("itcast_cxf", "itcast"); } }; //得到Session 傳參為鍵值對、實現的介面 Session session = Session.getInstance(props, auth);
2) MimeMessage
    表示一個郵件物件,你可以呼叫它的setFrom(),設定發件人、設定收件人、設定主題、設定正文!


3) TransPort
    它只有一個功能,發郵件!

4、JavaMail傳送郵件


前提:導包(兩個)   mail.jar 、 activation.jar
public class Demo1 {

    //一、簡單郵件收發
    @Test
    public void fun1() throws Exception {
        /*
         * 1. 得到session
         */
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.163.com");
        props.setProperty("mail.smtp.auth", "true");

        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("itcast_cxf", "itcast");
            }
        };

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

        /*
         * 2. 建立MimeMessage ,傳參為Session
         */
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("[email protected]"));//設定發件人
        msg.setRecipients(RecipientType.TO, "[email protected]");//設定收件人
        msg.setRecipients(RecipientType.CC, "itcast_[email protected]");//設定抄送人(地址)
        msg.setRecipients(RecipientType.BCC, "[email protected]");//設定暗送 (地址)

        msg.setSubject("這是來自ITCAST的測試郵件");    //設定郵件標題
        msg.setContent("這就是一封垃圾郵件!", "text/html;charset=utf-8");    //設定郵件正文。

        /*
         * 3. 發郵件!
         */
        Transport.send(msg);
    }


    //示例二、帶有附件的郵件
    @Test
    public void fun2() throws Exception {
        /*
         * 1. 得到session
         */
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.163.com");
        props.setProperty("mail.smtp.auth", "true");

        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("itcast_cxf", "itcast");
            }
        };

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

        /*
         * 2. 建立MimeMessage
         */
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("[email protected]"));//設定發件人
        msg.setRecipients(RecipientType.TO, "[email protected]");//設定收件人

        msg.setSubject("這是來自ITCAST的測試郵件有附件");


        ////////////////////////////////////////////////////////
        /*
         * 當傳送包含附件的郵件時,郵件體就為多部件形式!
         * 1. 建立MimeMultipart (多個部件的部件內容集合)
         *   MimeMultipart是一個集合,用來裝載多個主體部件!
         * 
         * 2. 集合中包含兩個主體部件:1.文字內容部件、2.附件。
         *   主體部件叫MimeBodyPart
         * 
         * 3. 把MimeMultipart(部件集合)設定給MimeMessage的內容!
         */
        MimeMultipart list = new MimeMultipart();//建立多部件主體

        // 建立MimeBodyPart (作為文字部件)
        MimeBodyPart part1 = new MimeBodyPart();
        // 設定主體部件的內容
        part1.setContent("這是一封包含附件的垃圾郵件", "text/html;charset=utf-8");
        // 把主體部件新增到集合中
        list.addBodyPart(part1);


        // 建立MimeBodyPart (作為附件部件)
        MimeBodyPart part2 = new MimeBodyPart();
        part2.attachFile(new File("F:/道奇.jpg"));//建立附件物件,並設定附件的內容
        part2.setFileName(MimeUtility.encodeText("道奇.jpg"));//設定顯示的檔名稱,其中encodeText用來處理中文亂碼問題
        list.addBodyPart(part2);

        msg.setContent(list);//把它設定給郵件作為郵件的內容。


        ////////////////////////////////////////////////////////

        /*
         * 3. 發
         */
        Transport.send(msg);        
    }

    //示例三、
    //使用MailUtils小工具  來自於itcast-tools-1.4.2.jar
    @Test
    public void fun3() throws Exception {
        /*
         * 1. 得到 Session (三個引數)
         *  
         * 引數分別為:伺服器主機名稱、發件使用者名稱、密碼
         */
        Session session = MailUtils.createSession("smtp.163.com", "itcast_cxf", "itcast");

        /*
         * 2. 建立 Mail(郵件)物件 (四個引數)
         * 
         *  引數分別為:發件人、收件人、郵件標題、郵件正文
         */
        Mail mail = new Mail("[email protected]",
                "[email protected],[email protected]",
                "郵件標題", "郵件正文");

        /*
         * 建立兩個附件物件 (兩個引數)
         *
         * 引數分別為: 附件物件(附件內容)、 附件名稱
         */
        AttachBean ab1 = new AttachBean(new File("F:/f/白冰.jpg"), "小美女.jpg");
        AttachBean ab2 = new AttachBean(new File("F:/f/big.jpg"), "我的羽絨服.jpg");

        // 將附件新增到mail中
        mail.addAttach(ab1);
        mail.addAttach(ab2);

        /*
         * 3. 傳送
         */
        MailUtils.send(session, mail);
    }
}

允許使用被禁止的類。

例如sun.misc.BASE64Encoder
這裡寫圖片描述