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

Arm*_*iye 5 java email 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);
Run Code Online (Sandbox Code Playgroud)

jim*_*imr 9

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

看到这里

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

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

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