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.
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.
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 <[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
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:
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.
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"?