繁体   English   中英

使用Google应用引擎发送电子邮件

[英]Sending email with google app engine

我想使用谷歌应用引擎发送一个包含此代码的简单电子邮件。 但没有任何反应,为了使用邮件api,我必须配置一些东西吗? 这在localhost上运行。 我使用gmail作为邮件主机。

   String host = "smtp.google.com";
String to = "example@yahoo.fr";
String from = "example@gmail.com";
String subject = "this is a test";
String messageText = "test";
boolean sessionDebug = false;
// Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);

// Set debug on the Session
// Passing false will not echo debug info, and passing True will.

mailSession.setDebug(sessionDebug);

// Instantiate a new MimeMessage and fill it with the 
// required information.

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);

// Hand the message to the default transport service
// for delivery.

Transport.send(msg);

在本地运行AppEngine开发服务器时,实际上不会发送通过Mail服务发送的任何内容 - 它只会被记录到控制台

看到这里

当在开发服务器中运行的应用程序调用Mail服务发送电子邮件消息时,该消息将打印到日志中。 Java开发服务器不发送电子邮件。

另外, from地址必须(从这里

  • 应用管理员的电子邮件
  • 使用Google帐户登录的当前登录用户的电子邮件
  • 来自应用的有效电子邮件接收地址

发件人应该是您自己的Gmail电子邮件地址,而不是example@gmail.com

原因是因为SMTP服务器需要对您进行身份验证。

显然,GAE不再允许使用管理员帐户。 您需要使用服务帐户: project-id@appspot.gserviceaccount.com

我以前的项目仍然使用管理员帐户,但最近创建的项目不允许我使用任何管理员帐户。

除了不在localhost上工作的电子邮件或由于发件人电子邮件不是经过身份验证的电子邮件,我发现即使版本不是默认版本,该电子邮件也不起作用。 我无法在任何地方找到这个记录。

例如: nondefaultversion-dot-myapp.appspot.com (电子邮件不工作,没有错误日志) myapp.appspot.com (电子邮件作品)

请确认其他人是否也遇到过这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM