Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have been trying to test GMail actions for a few days but it does not seem to work. Since I am not registered I use the little piece of Java code below to send an email from myself to myself using GMail's smtp servers. The message's body is a direct copy from the documentation.

The Apps Script version works, though.

final String username = "[email protected]";
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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
157 views
Welcome To Ask or Share your Answers For Others

1 Answer

Even test emails need to be signed with DKIM/SPF in order to prevent spoofing, and I'm not sure if there's a way to do that with SMTP.

If you don't want to use Apps Script, a Google Apps domain (with proper DKIM/SPF configuration) is probably your best bet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...