package com.storlead.mail.controller;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-08-08 17:46
 */

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.util.Hashtable;

public class EmailServiceChecker {
    public static void main(String[] args) {
//        String email = "chenkaiqiang@lcjs43.wecom.work";
        String email = "fan@storlead.com";
        String emailService = getEmailService(email);
        System.out.println("邮箱服务提供商: " + emailService);
    }

    public static String getEmailService(String email) {
        if (email == null || !email.contains("@")) {
            return "";
        }

        String domain = email.substring(email.indexOf("@") + 1);

        // 使用域名匹配常见邮箱服务提供商
        switch (domain) {
            case "gmail.com":
                return "Google Gmail";
            case "yahoo.com":
            case "yahoo.co.jp":
                return "yahoo.com";
            case "outlook.com":
                return "outlook.com";
            case "hotmail.com":
                return "hotmail.com";
            case "qq.com":
                return "qq.com";
            case "exmail.qq.com":
                return "exmail.qq.com";
            case "163.com":
                return "163.com";
            case "storlead.com":
                return "exmail.qq.com";
            case "renice-tech.com":
                return "exmail.qq.com";
            // 添加其他常见的邮箱服务提供商...
            default:
                return checkMXRecords(domain);
        }
    }

    public static String checkMXRecords(String domain) {
        try {
            Hashtable<String, String> env = new Hashtable<>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
            DirContext ctx = new InitialDirContext(env);
            Attributes attrs = ctx.getAttributes(domain, new String[]{"MX"});
            Attribute attr = attrs.get("MX");

            if (attr == null) {
                return "";
            }

            for (int i = 0; i < attr.size(); i++) {
                String mxRecord = (String) attr.get(i);
                if (mxRecord.contains("google.com")) {
                    return "gmail.com";
                } else if (mxRecord.contains("outlook.com")) {
                    return "outlook.com";
                } else if (mxRecord.contains("hotmail.com")) {
                    return "hotmail.com";
                }  else if (mxRecord.contains("qq.com") && (mxRecord.contains("mx1") || mxRecord.contains("mx2"))) {
                    return "qq.com";
                } else if (mxRecord.contains("qq.com") && mxRecord.contains("mxbiz")) {
                    return "exmail.qq.com";
                } else if (mxRecord.contains("yahoo.com")) {
                    return "yahoo.com";
                }
            }
        } catch (NamingException e) {
            e.printStackTrace();
            return "";
        }
        return "";
    }
}
