几天来,我一直在尝试测试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);
}