小编典典

无法测试GMail自定义操作

java

我一直在尝试测试GMail动作几天,但似乎无法正常工作。由于我尚未注册,因此我使用下面的Java小代码使用GMail的smtp服务器向自己发送电子邮件。邮件的正文是文档的直接副本。

不过,Apps Script版本有效。

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);
}

阅读 214

收藏
2020-10-16

共1个答案

小编典典

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

如果您不想使用Apps脚本,则最好的选择是Google Apps域(具有正确的DKIM / SPF配置)。

2020-10-16