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

时间:2012-06-09 03:45:07

标签: java google-app-engine email

使用谷歌应用引擎发送包含此代码的简单电子邮件。  但没有任何反应,为了使用邮件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);

4 个答案:

答案 0 :(得分:9)

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

请参阅here

  

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

此外,from地址必须(来自here

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

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

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

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