1. 程式人生 > >傳送帶有附件的郵箱到騰訊企業郵箱

傳送帶有附件的郵箱到騰訊企業郵箱

首先先加入maven依賴

<dependency>
  <groupId>com.sun.mail</groupId>
  <artifactId>javax.mail</artifactId>
  <version>1.5.4</version>
</dependency>

package com.mmall.service.impl;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;

import com.mmall.util.PropertiesUtil;
import com.sun.mail.util.MailSSLSocketFactory;
import org.apache.commons.lang.StringUtils;

public class sendMail {



    /**
     * 傳送帶附件的郵件
     *
     * @param receive
     *            收件人
     * @param subject
     *            郵件主題
     * @param msg
     *            郵件內容
     * @param filename
     *            附件地址
     * @return
     * @throws GeneralSecurityException
     */
    public static boolean sendMail(String receive, String subject, String msg, String filename)
            throws GeneralSecurityException {
        if (StringUtils.isEmpty(receive)) {
            return false;
        }

        // 發件人電子郵箱
        final String from = PropertiesUtil.getProperty("email.username");
        // 發件人電子郵箱密碼
        final String pass = PropertiesUtil.getProperty("email.password");

        // 指定傳送郵件的主機為 smtp.qq.com
        String host = "pop.exmail.qq.com"; // 郵件伺服器,這個是騰訊企業郵箱的主機

        // 獲取系統屬性
        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(from, pass); // 發件人郵件使用者名稱、密碼
            }
        });

        try {
            // 建立預設的 MimeMessage 物件
            MimeMessage message = new MimeMessage(session);

            // Set From: 頭部頭欄位
            message.setFrom(new InternetAddress(from));

            // Set To: 頭部頭欄位
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(receive));

            // Set Subject: 主題文字
            message.setSubject(subject);

            // 建立訊息部分
            BodyPart messageBodyPart = new MimeBodyPart();

            // 訊息
            messageBodyPart.setText(msg);

            // 建立多重訊息
            Multipart multipart = new MimeMultipart();

            // 設定文字訊息部分
            multipart.addBodyPart(messageBodyPart);

            // 附件部分
            messageBodyPart = new MimeBodyPart();
            // 設定要傳送附件的檔案路徑
            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);
            // System.out.println("Sent message successfully....");
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        String receive="
[email protected]
"; String subject ="郵件主題"; String msg ="郵件內容"; String filename ="F:\\stdio\\圖片素材\\10-1.jpg"; try { sendMail.sendMail(receive, subject, msg, filename); } catch (GeneralSecurityException e) { e.printStackTrace(); } } }