简体   繁体   English

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

[英]Sending test emails with Google AppEngine dev server

I'm developing an application which will send out notification emails and I'd like to perform end-to-end testing on the email before I deploy the capability. 我正在开发一个将发送通知电子邮件的应用程序,我想在部署该功能之前对电子邮件进行端到端测试。

To this end, I've got a dummy SMTP service running on my development machine that accepts any incoming mail and just stores it in a single box that a POP client can access, no matter who the sender and receiver are. 为此,我在开发计算机上运行了一个虚拟SMTP服务,该服务可以接收任何传入的邮件,并将其存储在POP客户端可以访问的单个盒子中,无论发送者和接收者是谁。

I've used this with other applications to make sure that emails get sent, that they're readable by various clients, and so on. 我将其与其他应用程序结合使用,以确保发送电子邮件,确保各种客户端都可以读取电子邮件,等等。 I see that, by default, there is a com.google.appengine.api.mail.MailService that my application code can call into to send messages and, in the real environment, it actually sends email. 我看到,默认情况下,有一个com.google.appengine.api.mail.MailService,我的应用程序代码可以调用该com.google.appengine.api.mail.MailService来发送消息,并且在实际环境中,它实际上是在发送电子邮件。

However, in the dev environment, the mail seems to get dropped on the floor. 但是,在开发环境中,邮件似乎掉在地上。

I see log messages from worker threads like this: 我看到来自工作线程的日志消息是这样的:

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

But there doesn't seem to be an obvious way for me to see the actual message. 但是,似乎没有一种明显的方式可以让我看到实际的消息。 I did find an example in the Google documentation that led me to write this bit of code: 我确实在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);
    }

However, when I run this in my dev server, it looks like I'm still using the built in MailService, and my local dummy SMTP service never actually gets contacted. 但是,当我在开发服务器中运行此程序时,似乎仍在使用内置的MailService,并且实际上从未联系过我的本地虚拟SMTP服务。

Is there any way to achieve my goal of being able to view the app-generated email in an email client, or do I just get to debug every new email template in "the big lab"? 有什么方法可以实现我的目标,即能够在电子邮件客户端中查看由应用程序生成的电子邮件,还是只是调试“大型实验室”中的每个新电子邮件模板?

The behaviour is expected if you didn't configure your development server to use sendmail or your local SMTP server. 如果未将开发服务器配置为使用sendmail或本地SMTP服务器,则会出现此现象。 From Mail and the development server : Mail和开发服务器

The development server can be configured to send email messages directly from your computer when you test a feature of your app that sends messages. 可以将开发服务器配置为在测试应用程序的发送消息功能时直接从计算机发送电子邮件消息。 You can configure the development server to use an SMTP server of your choice. 您可以将开发服务器配置为使用您选择的SMTP服务器。 Alternatively, you can tell the development server to use Sendmail, if Sendmail is installed on your computer and set up for sending email. 或者,如果您的计算机上已安装Sendmail并设置为发送电子邮件,则可以告诉开发服务器使用Sendmail。

If you do not configure an SMTP server or enable Sendmail, when your app calls the Mail service, the development server will log the contents of the message. 如果您没有配置SMTP服务器或启用Sendmail,则当您的应用调用邮件服务时,开发服务器将记录邮件的内容。 The message will not actually be sent. 该消息实际上不会发送。

The options for configuring your local development server to use sendmail or a specific SMTP server are documented in 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. 

Update: 更新:

As @Stephen B noted the above only applies to the python sandbox, the java Mail and the development server is just: 正如@Stephen B指出的那样,以上内容仅适用于python沙箱,java Mail和开发服务器只是:

When an application running in the development server calls the Mail service to send an email message, the message is printed to the application logs. 当开发服务器中运行的应用程序调用邮件服务发送电子邮件时,该消息将打印到应用程序日志中。 The development server does not send the email message. 开发服务器不发送电子邮件。

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

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