使用Google AppEngine dev服务器发送测试电子邮件

时间:2017-09-14 04:16:50

标签: java google-app-engine smtp

我正在开发一个应用程序,它将发送通知电子邮件,我希望在部署该功能之前对电子邮件执行端到端测试。

为此,我在我的开发机器上运行了一个虚拟SMTP服务,该服务接受任何传入邮件,并将其存储在POP客户端可以访问的单个框中,无论发件人和收件人是谁。< / p>

我已经将其与其他应用程序一起使用,以确保发送电子邮件,各种客户端可以读取它们,等等。我看到,默认情况下,我的应用程序代码可以调用com.google.appengine.api.mail.MailService来发送消息,而在实际环境中,它实际上会发送电子邮件。

然而,在开发环境中,邮件似乎被丢弃在地板上。

我看到来自工作线程的日志消息如下:

Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO: MailService.send
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   From: myapp <myapp-sender@myapp.appspotmail.com>
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   To: Recipient <user@test.com>
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   Reply-to: myapp <myapp-sender@myapp.appspotmail.com>
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   Subject: Email Update
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   Body:
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:     Content-type: text/plain
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO:     Data length: 948

但似乎没有一种明显的方式让我看到实际的信息。我确实在Google文档中找到了一个示例,它让我编写了这段代码:

    Properties mailProps = new Properties();
    AbstractConfiguration config = ConfigurationManager.getConfig();
    String smtpHost = config.getString("email.smtp.host");
    __l.debug("Sending email via SMTP connection to host "+smtpHost);
    mailProps.setProperty("mail.smtp.host", smtpHost);
    mailProps.setProperty("mail.smtp.port", config.getString("email.smtp.port", "25"));
    mailProps.setProperty("mail.smtp.connectiontimeout", config.getString("email.smtp.connectiontimeout", "1000"));
    mailProps.setProperty("mail.smtp.timeout", config.getString("email.smtp.timeout", "1000"));
    Session session = Session.getDefaultInstance(mailProps, null);
    try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(config.getString("email.sender.address"), config.getString("email.sender.name")));
        msg.addRecipient(Message.RecipientType.TO,
                         new InternetAddress(toAddress, toName));
        msg.setSubject(title);
        msg.setText(messageText);
        Transport.send(msg);
        __l.info("message has been sent to " + toAddress);
    } catch (Exception e) {
        __l.warn("Exception attempting to send email to "+toAddress+" about "+title, e);
    }

然而,当我在我的开发服务器中运行它时,看起来我仍然使用内置的MailService,我的本地虚拟SMTP服务实际上从未被联系过。

有没有办法实现我的目标,即能够在电子邮件客户端中查看应用程序生成的电子邮件,还是只是调试“大实验室”中的每个新电子邮件模板?

1 个答案:

答案 0 :(得分:0)

如果未将开发服务器配置为使用sendmail或本地SMTP服务器,则会出现此行为。来自Mail and the development server

  

可以将开发服务器配置为发送电子邮件   当您测试应用程序的功能时,直接从您的计算机   发送消息。您可以配置开发服务器以使用   您选择的SMTP服务器。或者,您可以告诉   开发服务器使用Sendmail,如果你的安装了Sendmail   计算机和设置发送电子邮件。

     

如果您没有配置SMTP服务器或启用Sendmail,那么您的   app调用Mail服务,开发服务器将登录   消息的内容。该消息实际上不会被发送。

Local Development Server Options中记录了配置本地开发服务器以使用sendmail或特定SMTP服务器的选项:

--enable_sendmail=yes|no
    Uses the local computer's Sendmail installation for sending email messages.
     

...

--smtp_host=...
    The hostname of the SMTP server to use for sending email messages.
--smtp_port=...
    The port number of the SMTP server to use for sending email messages.
--smtp_user=...
    The username to use with the SMTP server for sending email messages.
--smtp_password=...
    The password to use with the SMTP server for sending email messages.

<强>更新

正如@Stephen B所说,上面只适用于python沙箱,java Mail and the development server只是:

  

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