1. 程式人生 > >java 傳送附件1是檔案,附件2是圖片,內容含有圖片的郵件

java 傳送附件1是檔案,附件2是圖片,內容含有圖片的郵件

package com.common;

import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class SendImgaeAndAttachMail {

    // 發件人地址
    public static String senderAddress = "[email protected]";
    // 收件人地址
    public static String recipientAddress = "[email protected]";
    // 發件人賬戶名
    public static String senderAccount = "MrZou";
    // 發件人賬戶密碼
    public static String senderPassword = "shenzhen";

    public static void main(String[] args) throws Exception {

        // 1、連線郵件伺服器的引數配置
        Properties props = new Properties();
        // 開啟debug除錯
        props.setProperty("mail.debug", "true");
        // 傳送伺服器需要身份驗證
        props.setProperty("mail.smtp.auth", "true");
        // 傳送伺服器埠,可以不設定,預設是25
        props.setProperty("mail.smtp.port", "25");
        // 設定傳送郵件協議名
        props.setProperty("mail.transport.protocol", "smtp");
        // 設定郵件伺服器主機名
        props.setProperty("mail.host", "smtp.163.com");

        // 2、建立定義整個應用程式所需的環境資訊的 Session 物件
        Session session = Session.getInstance(props, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 在session中設定賬戶資訊,Transport傳送郵件時會使用
                return new PasswordAuthentication(senderAccount, senderPassword);
            }
        });

        // 3、 建立一封郵件的例項物件
        MimeMessage msg = new MimeMessage(session);
        // 設定發件人地址
        msg.setFrom(new InternetAddress(senderAddress));
        // 設定郵件回覆人
        msg.setReplyTo(new Address[] { new InternetAddress(senderAddress) });
        // 設定收件人地址
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientAddress));
        // 設定主題
        msg.setSubject("傳送HTML+內嵌圖片+附件的測試郵件", "utf-8");

        // 4、整封郵件的MINE訊息體
        MimeMultipart msgMultipart = new MimeMultipart("mixed");
        // 設定郵件的MINE訊息體
        msg.setContent(msgMultipart);

        // 5、 建立附件1"節點"
        MimeBodyPart attachment1 = new MimeBodyPart();
        // 讀取本地檔案
        String attch1Path = "D:\\test\\淘淘商城的第一天筆記.doc";
        DataSource ds1 = new FileDataSource(new File(attch1Path.replace("////", "/")));
        DataHandler dh1 = new DataHandler(ds1);
        // 將附件1資料新增到"節點"
        attachment1.setDataHandler(dh1);
        // 設定附件1的檔名
        attachment1.setFileName(MimeUtility.encodeText(dh1.getName()));
        // 把附件1加入到 MINE訊息體中
        msgMultipart.addBodyPart(attachment1);

        // 6、建立附件2"節點"
        MimeBodyPart attachment2 = new MimeBodyPart();
        // 讀取本地檔案
        String attch2Path = "D:\\test\\hello.jpg";
        DataSource ds2 = new FileDataSource(new File(attch2Path.replace("////", "/")));
        DataHandler dh2 = new DataHandler(ds2);
        // 將附件2資料新增到"節點"
        attachment2.setDataHandler(dh2);
        // 設定附件2的檔名
        attachment2.setFileName(MimeUtility.encodeText(dh2.getName()));
        // 附件2加入到 MINE訊息體中
        msgMultipart.addBodyPart(attachment2);

        // 7、建立正文節點,用於儲存最終正文部分
        MimeBodyPart content = new MimeBodyPart();
        // 把正文加入到 MINE訊息體中
        msgMultipart.addBodyPart(content);

        // 8、設定郵件正文
        // 一個Multipart物件包含一個或多個bodypart物件,組成郵件正文
        MimeMultipart bodyMultipart = new MimeMultipart("related");
        // 將上面"related"型的 MimeMultipart 物件作為郵件的正文
        content.setContent(bodyMultipart);

        // 讀取本地圖片,將圖片資料新增到"節點"
        MimeBodyPart imagePart = new MimeBodyPart();
        String imgPath = "D:\\test\\mailTest.jpg";
        DataSource imgds = new FileDataSource(new File(imgPath.replace("////", "/")));
        DataHandler imgdh = new DataHandler(imgds);
        imagePart.setDataHandler(imgdh);
        imagePart.setContentID("mailTest");

        // 建立文字節點
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent("這是一張圖片<br/><img src='cid:mailTest'/>", "text/html;charset=utf-8");

        // 將文字和圖片新增到bodyMultipart
        bodyMultipart.addBodyPart(imagePart);
        bodyMultipart.addBodyPart(htmlPart);

        // 9、根據session物件獲取郵件傳輸物件Transport
        Transport transport = session.getTransport();
        transport.connect();
        // 傳送郵件,併發送到所有收件人地址
        transport.sendMessage(msg, msg.getAllRecipients());

        // 10、關閉郵件連線
        transport.close();
    }
}