java Mail如何發送郵件

來源:互聯網
上載者:User

標籤:線上   情境   host   1.2   htm   使用   The   驗證碼   sendmail   

1.應用情境:在系統需要發送與使用者相關的訊息時,而使用者不線上,可以採取發送郵件的方式,使使用者瞭解最新的系統情況 或者發送驗證碼等驗證情境

2.實驗環境 主要使用mail.jar和activation.jar兩個包

3.使用一份簡單的郵件

public void sendMail() throws AddressException, MessagingException{        /*         * 1.得到session         */                Properties properties=new Properties();        properties.setProperty("mail.host", "smtp.163.com");        properties.setProperty("mail.smtp.auth", "true");                //賬戶資訊測試        Authenticator auth =new Authenticator() {            @Override            protected PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication("zhou_s1", "xxx");//這裡是賬戶名和密碼            }        };                Session session =Session.getInstance(properties,auth);        /*         * 2.建立訊息message         */        MimeMessage message=new MimeMessage(session);                message.setFrom(new InternetAddress("[email protected]"));//設定寄件者        message.setRecipients(RecipientType.TO, "[email protected]");//設定收件者        //message.setRecipients(RecipientType.CC, "[email protected]");//設定抄送        //message.setRecipients(RecipientType.BCC, "[email protected]");//暗送給誰                message.setSubject("這一份來自周松測試的垃圾郵件--javamail");        message.setContent("test test test !!!","text/html;charset=utf-8");                /*         * 3.發送         */        Transport.send(message);    }

4.一份帶有附件的郵件

public void sendMailMore() throws AddressException, MessagingException, IOException{        /*         * 1.得到session         */                Properties properties=new Properties();        properties.setProperty("mail.host", "smtp.163.com");        properties.setProperty("mail.smtp.auth", "true");                //賬戶資訊測試        Authenticator auth =new Authenticator() {            @Override            protected PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication("zhou_s1", "xxx");            }        };                Session session =Session.getInstance(properties,auth);                        /*         * 2.建立訊息message         */        MimeMessage message=new MimeMessage(session);                message.setFrom(new InternetAddress("[email protected]"));//設定寄件者        message.setRecipients(RecipientType.TO, "[email protected]");//設定收件者                message.setSubject("這是一份含有附件的郵件---測試使用--javamail");                /******************************************************/        /*         * 附件的主體流程 附件含有多個組成部分 簡稱多組件         *           */        MimeMultipart list =new MimeMultipart();                //1.郵件主要內容  MimeBodyPart        MimeBodyPart part1 =new MimeBodyPart();                //1.1.內容        part1.setContent("一份含有附件的測試郵件", "text/html;charset=utf-8");                //1.2將主體添加到郵件中去        list.addBodyPart(part1);                //2.1.郵件附件裡面的內容        MimeBodyPart part2 =new MimeBodyPart();                        //2.2.附件        File file =new File("G:/JAVAtest/Merge.mp3");                part2.attachFile(file);        part2.setFileName(MimeUtility.encodeText("Merge.mp3"));        //2.3將主體添加到郵件中去        list.addBodyPart(part2);                    //3.最後將多組件添加到訊息中去        message.setContent(list);                //發送        Transport.send(message);    }

測試完畢!

 

java Mail如何發送郵件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.