1. 程式人生 > >【javaMail】java傳送帶附件郵件

【javaMail】java傳送帶附件郵件

package com.example.mail;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class SendMail {
    // 收件人電子郵箱
    private static final String to = "******@qq.com";
    // 發件人電子郵箱
    private static final String from = "******@******.com";
    private static final String from_name = "********";
    // 指定傳送郵件的主機為 smtp.qq.com
    private static final String host = "*********";
    private static final String user_name = "*********";
    private static final String pwd = "********";
    private static final String subject = "郵件主題";

    public static void main(String[] args) throws GeneralSecurityException, UnsupportedEncodingException {
        // 獲取系統屬性
        Properties properties = System.getProperties();
        // 設定郵件伺服器
        properties.setProperty("mail.smtp.host", host);
        properties.put("mail.smtp.auth", "true");
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", sf);
        // 獲取預設session物件
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {  //qq郵箱伺服器賬戶、第三方登入授權碼
                return new PasswordAuthentication(user_name, pwd); //發件人郵件使用者名稱、密碼
            }
        });
        try {
            // 建立預設的 MimeMessage 物件
            MimeMessage message = new MimeMessage(session);
            // Set From: 頭部頭欄位
            message.setFrom(new InternetAddress(from));
            message.setFileName(from_name);
            // Set To: 頭部頭欄位
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            // Set Subject: 主題文字
            message.setSubject(subject);
            // 建立訊息部分
            BodyPart messageBodyPart = new MimeBodyPart();
            // 訊息
            messageBodyPart.setText("233333333333333");
            // 建立多重訊息
            Multipart multipart = new MimeMultipart();
            // 設定文字訊息部分
            multipart.addBodyPart(messageBodyPart);
            // 附件部分
            messageBodyPart = new MimeBodyPart();
            //設定要傳送附件的檔案路徑
            String filename = "C:\\Users\\lwz\\Desktop\\********.xlsx";
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            //messageBodyPart.setFileName(filename);
            //處理附件名稱中文(附帶檔案路徑)亂碼問題
            messageBodyPart.setFileName(MimeUtility.encodeText(filename));
            multipart.addBodyPart(messageBodyPart);
            // 傳送完整訊息
            message.setContent(multipart);
            // 傳送訊息
            Transport.send(message);
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}