GAE与IAP(身份识别代理)和邮件入站服务

时间:2017-07-07 15:08:35

标签: java email google-app-engine google-iap

我有一个Java appengine标准项目,通过激活的入站服务处理传入的邮件,没有任何问题。

如果我配置并激活 IAP(身份识别代理),那么appengine项目仍可以正常工作而不会出现任何问题。

但现在收到的邮件服务会拒绝每封收到的邮件。

Message not delivered

There was a problem delivering your message to
<recipient>@<projectid>.appspotmail.com. See the technical details
below. 

Final-Recipient: rfc822; [user]@[projectid].appspotmail.com 
Action: failed
Status: 5.0.0 
Last-Attempt-Date: Thu, 06 Jul 2017 07:46:33 -0700 (PDT)

我忘记了什么?谢谢你的任何暗示!!

---------------------------------------------- < / H2>

应用服务引擎-web.xml中

<inbound-services>
        <service>mail</service>
</inbound-services>

web.xml

<servlet>
        <servlet-name>MailhandlerServlet</servlet-name>
        <servlet-class>com.company.appengine.gae.mail.MailHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>MailhandlerServlet</servlet-name>
        <!-- /_ah/mail/* matches all email addressed to the app -->
        <url-pattern>/_ah/mail/*</url-pattern>
</servlet-mapping>

1 个答案:

答案 0 :(得分:0)

我刚试过App Engine Hello World example作为基础和modified to accept incoming emails,它对我有用。我使用的目标电子邮件地址格式为recipient-user@PROJECT_ID.appspotmail.com

我建议您使用开发应用服务器验证调试并验证以下内容:

  1. 在收到请求时,在邮件处理程序servlet中添加一些日志记录。您可以查看我发布的示例以获取一些指示。

  2. 转到http://localhost:8080/_ah/admin/inboundmail访问Inbound Mail中的Development Console页面,然后从那里发送测试电子邮件。

  3. 如果您查看服务器日志,如果配置正确,您应该会看到servlet中的日志(即您在步骤1中添加的日志)。

  4. 一旦您确认所有内容都适用于开发应用服务器,请将您的应用部署到App Engine并通过发送真实电子邮件进行测试,然后通过Stackdriver Logging再次查看服务日志。除了您在步骤1中添加的日志外,您还应该看到200个此类传入邮件请求的响应。

  5. 另请注意,如果几次重试后没有成功的响应,许多电子邮件提供商将开始限制邮件到目标电子邮件ID。这是首先使用开发应用服务器进行验证的另一个好理由。

    这是App Engine Hello World example的差异,我开始工作:

    的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 /主/ JAVA / COM /示例/应用服务引擎/的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 /主/ web应用/ WEB-INF /应用服务引擎-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 /主/ web应用/ 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>