使用Google AppEngine dev服务器发送测试电子邮件
我正在开发一个应用程序,它会发出通知电子邮件,我希望在部署该功能之前对电子邮件执行端对端测试。为此,我的开发机器上运行了一个虚拟SMTP服务,它接受任何传入邮件,并将其存储在POP客户端可以访问的单个框中,而不管发件人和接收者是谁。我已经将其与其他应用程序一起使用,以确保电子邮件得到发送,它们可以被各种客户端读取,等等。我发现,默认情况下,我的应用程序代码可以调用com.google.appengine.api.mail.MailService来发送消息,并且在真实环境中,它实际上会发送电子邮件。然而,在开发环境中,邮件似乎掉到了地板上。我看到这样的工作线程日志消息:使用Google AppEngine dev服务器发送测试电子邮件
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 <[email protected]>
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO: To: Recipient <[email protected]>
Sep 13, 2017 9:05:38 PM com.google.appengine.api.mail.dev.LocalMailService log
INFO: Reply-to: myapp <[email protected]>
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
但似乎没有一种明显的方式让我看到实际的消息。我发现了谷歌文档,导致我写此位的代码示例:
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服务从未真正得到联系。有什么方法可以实现我的目标:能够在电子邮件客户端中查看应用生成的电子邮件,还是只需要在“大实验室”中调试每个新的电子邮件模板?
如果您未将开发服务器配置为使用sendmail或本地SMTP服务器,则会出现该行为。从Mail and the development server:
开发服务器可以被配置为直接从您的计算机发送电子邮件 当您测试您的应用程序的功能, 发送消息。您可以将开发服务器配置为使用您选择的 SMTP服务器。或者,如果Sendmail安装在您的 计算机上并设置为发送电子邮件,则可以告知 开发服务器使用Sendmail。
如果您未配置SMTP服务器或启用Sendmail,当您的 应用程序调用邮件服务时,开发服务器将记录邮件的内容 。该消息不会实际发送。
用于配置本地开发服务器使用sendmail或特定的SMTP服务器都记录在Local Development Server Options的选项:
--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中指出的上述仅适用于蟒蛇沙箱中,Java Mail and the development server就是:
当在开发服务器中运行的应用程序调用邮件 服务来发送一封电子邮件,该邮件打印到 应用程序日志。开发服务器不发送电子邮件 消息。
这可能适用于python,但实际上并没有帮助java开发环境。 –
是的,你说得对。 –