1.设置下SPF、DKIM 和 DMARC

详情查看:

https://support.google.com/mail/?p=UnsolicitedMessageErrorhttps://support.google.com/mail/?p=UnsolicitedMessageError

2.代码示例

private static MailAccount createMailAccount() {
        return new MailAccount()
                .setFrom("coralapp<gbox@coralapp.it.com>")
                .setUser("") 发送邮箱
                .setPass("") smtp秘钥
                .setHost("smtp.privateemail.com") smtp服务器
                .setPort(465) 端口
                .setSslEnable(true) 开启ssl
                .setCustomProperty("mail.smtp.dkim.enable", "true")
                .setCustomProperty("mail.smtp.dkim.privatekey", privateKey) dkim私钥
                .setCustomProperty("mail.smtp.dkim.selector", "default")
                .setCustomProperty("mail.smtp.headers",
                        "List-Unsubscribe: <https://coralapp.it.com/unsubscribe>\n" +
                                "X-Entity-Ref-ID: " + UUID.randomUUID());
    }

    public void sendCustomizedEmail() {
        try {
            createMimeMessage(
                    "Your temporary access code has been generated", 邮件主题
                    buildEmailContent()
            );
            System.out.println("邮件发送成功");
        } catch (Exception e) {
            handleError(e);
        }
    }

    public static void main(String[] args) {
        AdvancedMailSender advancedMailSender = new AdvancedMailSender();
        advancedMailSender.sendCustomizedEmail();
    }

    private void createMimeMessage(String subject, String content) {
        MailUtil.send(
                account,
                "diyuanpac70211@163.com",
                subject,
                content,
                true);
    }

    private String buildEmailContent() {
        return "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\">\n" +
                "    <meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'self' https://coralapp.it.com;\">\n" +
                "    <title>Account security notice</title>\n" +
                "    <style>\n" +
                "        @media screen and (max-width: 600px) {\n" +
                "            .container { padding: 10px !important; }\n" +
                "            .code { font-size: 1.5em !important; }\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body style=\"margin:0;font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;\">\n" +
                "    <div class=\"container\" style=\"max-width: 600px; margin: 20px auto; padding: 30px; background: #f8f9fa; border-radius: 8px;\">\n" +
                "        <img src=\"https://coralapp.it.com/logo.png\" alt=\"Logo\" width=\"120\" style=\"display: block; margin: 0 auto 30px;\">\n" +
                "      \n" +
                "        <h1 style=\"color: #202124; font-size: 1.25em; margin-bottom: 1.5em;\">Your security code</h1>\n" +
                "      \n" +
                "        <div style=\"background: white; padding: 25px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);\">\n" +
                "            <p style=\"margin-bottom: 1em; color: #5f6368;\">Please use the following temporary code to complete the verification:</p>\n" +
                "            <div class=\"code\" style=\"text-align: center; padding: 15px; background: #f1f3f4; border-radius: 4px; font-size: 1.75em; letter-spacing: 2px; color: #1a73e8; margin: 20px 0;\">\n" +
                "                439678\n" +
                "            </div>\n" +
                "            <p style=\"color: #70757a; font-size: 0.9em; text-align: center;\">(Please modify as soon as possible)</p>\n" +
                "        </div>\n" +
                "\n" +
                "        <div style=\"margin-top: 30px; padding-top: 20px; border-top: 1px solid #e0e0e0;\">\n" +
                "            <p style=\"color: #5f6368; font-size: 0.9em; line-height: 1.6;\">\n" +
                "                This is an automatic email sent by the system. Please do not reply directly。<br>\n" +
                "                <a href=\"https://coralapp.it.com/unsubscribe\" style=\"color: #1a73e8; text-decoration: none;\">Cancellation notice</a> | \n" +
                "                <a href=\"https://coralapp.it.com/privacy\" style=\"color: #1a73e8; text-decoration: none;\">privacy policy</a>\n" +
                "            </p>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";
    }

//    private String buildEmailContent() throws IOException { // 可使用html 读取
//        String htmlContent = new String(Files.readAllBytes(Paths.get("C:\\Users\\lenovo\\Desktop\\coralaApp.html")));
//        return htmlContent.replace("{VERIFICATION_CODE}", "2333");
//    }

    private void handleError(Exception e) {
        System.err.println("发送失败: " + e.getMessage());
        e.printStackTrace();
    }

3.查看邮件被拦截原因

在发件服务器域名 中添加解析 text and cname 详情:

  1. https://postmaster.google.com/

  2. https://dmarcian.com/

4.测试ip是否在黑名单中

  1. https://mxtoolbox.com/

  2. https://check.spamhaus.org/

5.查看邮件内容是否有问题

  1. https://www.mail-tester.com/

6.检查邮件原始请求头

  1. https://toolbox.googleapps.com/apps/messageheader/