我正在開發一個應用程序,它會發出通知電子郵件,我希望在部署該功能之前對電子郵件執行端對端測試。爲此,我的開發機器上運行了一個虛擬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服務從未真正得到聯繫。有什麼方法可以實現我的目標:能夠在電子郵件客戶端中查看應用生成的電子郵件,還是隻需要在「大實驗室」中調試每個新的電子郵件模板?
這可能適用於python,但實際上並沒有幫助java開發環境。 –
是的,你說得對。 –