JavaMail郵件開發,javamail郵件

來源:互聯網
上載者:User

JavaMail郵件開發,javamail郵件

一、只帶有純文字的郵件

  代碼案例如下:

package com.lyh.sendemail;import java.util.Properties;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;//發送郵件public class MessageDemo1 {    public static void main(String[] args) throws Exception{        Properties props = new Properties();//key value:配置參數。真正發送郵件時再配置        props.setProperty("mail.transport.protocol", "smtp");//指定郵件發送的協議,參數是規範規定的        props.setProperty("mail.host", "smtp.163.com");//指定發件伺服器的地址,參數是規範規定的//        props.setProperty("mail.debug", "true");//郵件發送的偵錯模式,參數是規範規定的        props.setProperty("mail.smtp.auth", "true");//請求伺服器進行身份認證。參數與具體的JavaMail實現有關                Session session = Session.getInstance(props);//發送郵件時使用的環境配置        session.setDebug(true);        MimeMessage message = new MimeMessage(session);                //設定郵件的頭        message.setFrom(new InternetAddress("xxx@163.com"));        message.setRecipients(Message.RecipientType.TO, "xxx@qq.com");        message.setSubject("This is second message");        //設定本文        message.setContent("<h1>hello</h1>", "text/html");//        message.setText("<h1>hello</h1>");//純文字                message.saveChanges();                //發送郵件        Transport ts = session.getTransport();        ts.connect("xxx@163.com", "123456");       // 密碼為授權碼不是郵箱的登入密碼        ts.sendMessage(message, message.getAllRecipients());//對象,用執行個體方法}    }}

二、帶有圖片的郵件

  a、複雜郵件封裝模型

    

  代碼案例

package com.lyh.sendemail;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;//發送郵件public class MessageDemo2 {    public static void main(String[] args) throws Exception{        Properties props = new Properties();//key value:配置參數。真正發送郵件時再配置                props.setProperty("mail.transport.protocol", "smtp");//指定郵件發送的協議,參數是規範規定的        props.setProperty("mail.host", "smtp.163.com");//指定發件伺服器的地址,參數是規範規定的//        props.setProperty("mail.debug", "true");//郵件發送的偵錯模式,參數是規範規定的        props.setProperty("mail.smtp.auth", "true");//請求伺服器進行身份認證。參數與具體的JavaMail實現有關                Session session = Session.getInstance(props);//發送郵件時使用的環境配置        session.setDebug(true);        MimeMessage message = new MimeMessage(session);                //設定郵件的頭        message.setFrom(new InternetAddress("xxx@163.com"));        message.setRecipients(Message.RecipientType.TO, "xxx@qq.com");        message.setSubject("This is second message");        //設定本文                //搞出文本部分        MimeBodyPart textPart = new MimeBodyPart();        textPart.setContent("aaa<img src='cid:mm'/>aaa", "text/html");                //搞圖片部分        MimeBodyPart imagePart = new MimeBodyPart();        imagePart.setContentID("mm");        //把磁碟上的檔案加到part中使用到了JAF架構        DataHandler dh = new DataHandler(new FileDataSource("src/0.jpg"));        imagePart.setDataHandler(dh);                MimeMultipart mp = new MimeMultipart();        mp.addBodyPart(textPart);        mp.addBodyPart(imagePart);        mp.setSubType("related");//有關係的                message.setContent(mp);        message.saveChanges();                //發送郵件        Transport ts = session.getTransport();        ts.connect("xxx@163.com", "123456"); //密碼為授權碼不是郵箱的登入密碼        ts.sendMessage(message, message.getAllRecipients());//對象,用執行個體方法    }}

三、帶有文本、圖片、附件的郵件

  代碼案例:

   

package com.lyh.sendemail;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import javax.mail.internet.MimeUtility;//發送郵件public class MessageDemo3 {    public static void main(String[] args) throws Exception{        Properties props = new Properties();//key value:配置參數。真正發送郵件時再配置                props.setProperty("mail.transport.protocol", "smtp");//指定郵件發送的協議,參數是規範規定的        props.setProperty("mail.host", "smtp.163.com");//指定發件伺服器的地址,參數是規範規定的//        props.setProperty("mail.debug", "true");//郵件發送的偵錯模式,參數是規範規定的        props.setProperty("mail.smtp.auth", "true");//請求伺服器進行身份認證。參數與具體的JavaMail實現有關                Session session = Session.getInstance(props);//發送郵件時使用的環境配置//        session.setDebug(true);        MimeMessage message = new MimeMessage(session);                //設定郵件的頭        message.setFrom(new InternetAddress("xxx@163.com"));        message.setRecipients(Message.RecipientType.TO, "xxxqq.com");        message.setSubject("這是一封複雜的郵件");        //設定本文                //搞出文本部分        MimeBodyPart textPart = new MimeBodyPart();        textPart.setContent("美女<img src='cid:mm'/>aaa", "text/html;charset=UTF-8");                //搞圖片部分        MimeBodyPart imagePart = new MimeBodyPart();        imagePart.setContentID("mm");        //把磁碟上的檔案加到part中使用到了JAF架構        DataHandler dh = new DataHandler(new FileDataSource("src/0.jpg"));        imagePart.setDataHandler(dh);                MimeMultipart mp = new MimeMultipart();        mp.addBodyPart(textPart);        mp.addBodyPart(imagePart);        mp.setSubType("related");//有關係的                MimeBodyPart textImagePart = new MimeBodyPart();    //將 MimeMultipart 添加到 MimeBodyPart實現附件的發送        textImagePart.setContent(mp);                //建立附件部分        MimeBodyPart attachmentPart = new MimeBodyPart();        dh = new DataHandler(new FileDataSource("src/賬戶.rar"));        String filename = dh.getName();        attachmentPart.setDataHandler(dh);                //手工設定檔案名稱  防止亂碼使用  javaMail裡的 MimeUtility進行編碼        attachmentPart.setFileName(MimeUtility.encodeText(filename));                 //最終的 MimeMultipart        MimeMultipart finalMp = new MimeMultipart();        finalMp.addBodyPart(attachmentPart);        finalMp.addBodyPart(textImagePart);                finalMp.setSubType("mixed");                message.setContent(finalMp);        message.saveChanges();                //發送郵件        Transport ts = session.getTransport();        ts.connect("xxx@163.com", "123456"); //密碼為授權碼不是郵箱的登入密碼        ts.sendMessage(message, message.getAllRecipients());//對象,用執行個體方法        }}

 

 

聯繫我們

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