2017-09-14 78 views
1

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

回答

0

如果您未將開發服務器配置爲使用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就是:

當在開發服務器中運行的應用程序調用郵件 服務來發送一封電子郵件,該郵件打印到 應用程序日誌。開發服務器不發送電子郵件 消息。

+0

這可能適用於python,但實際上並沒有幫助java開發環境。 –

+0

是的,你說得對。 –

相關問題