0

几天来,我一直在尝试测试GMail 操作,但它似乎不起作用。由于我没有注册,所以我使用下面的一小段 Java 代码使用 GMail 的 smtp 服务器向我自己发送一封电子邮件。消息的正文是文档的直接副本。

不过,Apps 脚本版本可以工作。

final String username = "david.hatanian@gmail.com";
final String password = "X";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

try {

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(username));
    message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse(username));
    message.setSubject("Testing Subject");
    message.setContent("<html>" +
            "  <head>" +
            "    <script type=\"application/ld+json\">" +
            "    {" +
            "      \"@context\": \"http://schema.org\"," +
            "      \"@type\": \"EmailMessage\"," +
            "      \"description\": \"Check this out\"," +
            "      \"action\": {" +
            "        \"@type\": \"ViewAction\"," +
            "        \"url\":   \"https://www.youtube.com/watch?v=eH8KwfdkSqU\"" +
            "      }" +
            "    }" +
            "    </script>" +
            "  </head>" +
            "  <body>" +
            "    <p>" +
            "      This a test for a Go-To action in Gmail." +
            "    </p>" +
            "  </body>" +
            "</html>", "text/html; charset=utf-8");

    Transport.send(message);

    System.out.println("Done");

} catch (MessagingException e) {
    throw new RuntimeException(e);
}
4

1 回答 1

1

甚至测试电子邮件也需要使用 DKIM/SPF 签名以防止欺骗,我不确定是否有办法使用 SMTP 来做到这一点。

如果您不想使用 Apps 脚本,Google Apps 域(具有适当的 DKIM/SPF 配置)可能是您的最佳选择。

于 2014-01-06T20:43:59.867 回答