java實現阿里雲企業郵箱發送郵件


本案列只針對阿里企業郵箱賬號使用

阿里雲企業郵箱登陸登錄入口  https://www.ali-exmail.cn/Land/

一 導入maven 庫

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

 

 二 阿里雲郵箱工具類,直接使用即可

package com.test.aliyun;

import org.apache.commons.lang3.StringUtils;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.security.Security;
import java.util.Properties;

public class AliyunMail {
    public static final String ALIDM_SMTP_HOST = "smtp.qiye.aliyun.com";

    public static final String ALIDM_SMTP_PORT = "25";


    /**
     * @param sendAddress 發件人地址
     * @param sendPassword 發件人密碼
     * @param host 協議
     * @param port 端口
     * @param subject 標題
     * @param content 內容
     * @param filePath 附件地址
     * @param CC 抄送人
     * @throws Exception
     * @throws AddressException
     */
    public static void sendMail(String sendAddress,String sendPassword,String host,String port,String subject,String content,String internetAddress,String filePath,String CC) throws AddressException, Exception {
        //設置SSL連接、郵件環境
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", host);
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", port);//設置端口
        props.setProperty("mail.debug", "true");//啟用調試
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtp.auth", "true");
        //建立郵件會話
        Session session = Session.getDefaultInstance(props, new Authenticator() {   //身份認證
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(sendAddress, sendPassword);//發件人賬號、密碼
            }
        });
        //建立郵件對象
        MimeMessage message = new MimeMessage(session);
        //設置郵件的發件人、收件人、主題
        //附帶發件人名字
        //  message.setFrom(new InternetAddress("from_mail@qq.com", "optional-personal"));
        message.setFrom(new InternetAddress(sendAddress));//發件人賬號
        message.setRecipients(Message.RecipientType.TO, internetAddress);//收件人賬號

        //標題
        message.setSubject(subject);//郵件標題

        //內容
        Multipart multipart = new MimeMultipart();
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setContent(content, "text/html;charset=utf-8");//郵件內容
        multipart.addBodyPart(contentPart);

        //附件部分
        if (StringUtils.isNotBlank(filePath)) {
            BodyPart attachPart = new MimeBodyPart();
            FileDataSource fileDataSource = new FileDataSource(filePath);//附件地址
            attachPart.setDataHandler(new DataHandler(fileDataSource));
            attachPart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));
            multipart.addBodyPart(attachPart);
        }

        message.setContent(multipart);

        //抄送地址
        if (StringUtils.isNotBlank(CC)) {
            InternetAddress[] internetAddressCC = new InternetAddress().parse(CC);
            message.setRecipients(Message.RecipientType.CC, internetAddressCC);
        }

        //發送郵件
        Transport.send(message);
    }
    public static void main(String[] args) {
        try {
            sendMail("企業郵箱賬號", "企業郵箱密碼", ALIDM_SMTP_HOST, ALIDM_SMTP_PORT, "subject test",
                    "Hello Email", "收件人郵箱賬號", "文件路徑", "抄送人郵箱");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 效果圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM