我刚刚尝试了App Engine Hello World 示例作为基础并进行了修改以接受传入的电子邮件,它对我有用。我使用的目标电子邮件地址格式是recipient-user@PROJECT_ID.appspotmail.com
我建议您使用开发应用服务器验证调试并验证以下内容:
当收到请求时,在您的邮件处理程序 servlet 中添加一些日志记录。您可以查看我发布的示例以获取一些指示。
转到http://localhost:8080/_ah/admin/inboundmail
访问Inbound Mail
页面Development Console
并从那里发送测试电子邮件。
如果您查看服务器日志,如果配置正确,您应该会看到来自您的 servlet 的日志(即您在步骤 1 中添加的日志)。
确认开发应用服务器一切正常后,将应用部署到 App Engine 并通过发送真实电子邮件进行测试,并通过 Stackdriver Logging 再次查看服务日志。除了您在步骤 1 中添加的日志之外,您还应该看到针对此类传入邮件请求的 200 条响应。
另请注意,如果在几次重试后没有成功响应,许多电子邮件提供商将开始将邮件限制到目标电子邮件 ID。这是首先使用开发应用程序服务器进行验证的另一个很好的理由。
这是我让它工作的App Engine Hello World 示例的差异:
pom.xml
--- a/appengine/helloworld/pom.xml
+++ b/appengine/helloworld/pom.xml
@@ -33,6 +33,11 @@ Copyright 2015 Google Inc.
<version>2.5</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>javax.mail</groupId>
+ <artifactId>mail</artifactId>
+ <version>1.5.0-b01</version>
+ </dependency>
</dependencies>
<build>
<!-- for hot reload of the web application -->
src/main/java/com/example/appengine/helloworld/MailHandlerServlet.java
--- /dev/null
+++ b/appengine/helloworld/src/main/java/com/example/appengine/helloworld/MailHandlerServlet.java
@@ -0,0 +1,36 @@
+package com.example.appengine.helloworld;
+
+import java.io.IOException;
+import java.util.logging.Logger;
+import java.util.logging.Level;
+import java.util.Properties;
+
+import javax.mail.Address;
+import javax.mail.MessagingException;
+import javax.mail.Session;
+import javax.mail.internet.MimeMessage;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class MailHandlerServlet extends HttpServlet {
+
+ private static final Logger log = Logger.getLogger(MailHandlerServlet.class.getName());
+
+ @Override
+ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ Properties props = new Properties();
+ Session session = Session.getDefaultInstance(props, null);
+ try {
+ MimeMessage message = new MimeMessage(session, req.getInputStream());
+ log.info("Received mail message content type: " + message.getContentType());
+ for (Address addr : message.getFrom()) {
+ log.info("Received mail from: " + addr);
+ }
+ } catch (MessagingException e) {
+ log.log(Level.SEVERE, "Failed to handle incoming message:", e);
+ }
+ // ...
+ }
+}
src/main/webapp/WEB-INF/appengine-web.xml
--- a/appengine/helloworld/src/main/webapp/WEB-INF/appengine-web.xml
+++ b/appengine/helloworld/src/main/webapp/WEB-INF/appengine-web.xml
@@ -13,8 +13,11 @@
-->
<!-- [START config] -->
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>YOUR-PROJECT-ID</application>
<version>YOUR-VERSION-ID</version>
<threadsafe>true</threadsafe>
+ <inbound-services>
+ <service>mail</service>
+ </inbound-services>
</appengine-web-app>
<!-- [END config] -->
src/main/webapp/WEB-INF/web.xml
--- a/appengine/helloworld/src/main/webapp/WEB-INF/web.xml
+++ b/appengine/helloworld/src/main/webapp/WEB-INF/web.xml
@@ -7,8 +7,17 @@
<servlet-name>hello</servlet-name>
<servlet-class>com.example.appengine.helloworld.HelloServlet</servlet-class>
</servlet>
+ <servlet>
+ <servlet-name>mailhandler</servlet-name>
+ <servlet-class>com.example.appengine.helloworld.MailHandlerServlet</servlet-class>
+ </servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
+ <servlet-mapping>
+ <servlet-name>mailhandler</servlet-name>
+ <!-- /_ah/mail/* matches all email addressed to the app -->
+ <url-pattern>/_ah/mail/*</url-pattern>
+ </servlet-mapping>
</web-app>