package com.ruoshui.bigdata.core.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.security.cert.X509Certificate;
import java.util.Properties;

public class EmailUtil {

    public static void send(String emailUserName,String emailPassword,String emailAuthorization,String SJemailUserName,String title,String msg) throws Exception{
        Properties properties = new Properties();
        //设置QQ邮件服务器
        properties.setProperty("mail.host","smtp.feishu.cn");
        //邮件发送协议
        properties.setProperty("mail.transport.protocol","smtp");
        //需要验证用户名密码
        properties.setProperty("mail.smtp.host","smtp.feishu.cn");

        properties.setProperty("mail.smtp.port","465");
        properties.setProperty("mail.smtp.auth","true");
        properties.setProperty("mail.smtp.ssl.enable","true");
        properties.setProperty("mail.smtp.ssl.trust","*");
        properties.setProperty("mail.smtp.ssl.checkserveridentity","false");
        properties.setProperty("mail.smtp.starttls.enable","true");
        properties.setProperty("mail.smtp.starttls.required","true");
        
        // 核心：指定SSL协议版本（关键解决No appropriate protocol问题）
        // 支持的协议：TLSv1.2（推荐）、TLSv1.1、TLSv1（根据邮件服务器调整）
        javax.net.ssl.SSLContext sslContext = javax.net.ssl.SSLContext.getInstance("TLSv1.2");
        sslContext.init(null, new TrustManager[]{new X509TrustManager() {
            // 信任所有证书（仅测试/内网环境用，生产环境需配置可信证书）
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) {}
            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) {}
            @Override
            public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
        }}, null);
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        properties.put("mail.smtp.ssl.socketFactory", sslSocketFactory);
        // 强制使用指定的协议版本
        properties.put("mail.smtp.ssl.protocols", "TLSv1.2"); // 替换为TLSv1.1/TLSv1（如果服务器仅支持）

        // 2. 创建会话
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(emailUserName, emailAuthorization);
            }
        });
        
        session.setDebug(true);
        
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.feishu.cn",465,emailUserName,emailAuthorization);
        
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(emailUserName));
        message.setRecipient(Message.RecipientType.TO , new InternetAddress(SJemailUserName));
        message.setSubject(title);
        message.setContent(msg,"text/html;charset=UTF-8");
        
        transport.sendMessage(message,message.getAllRecipients());
        transport.close();
    }
}