0

I have a Java appengine standard project which handle incoming mails via activated inbound-service with out any problems.

If I configure and activate the IAP (identity aware proxy) the appengine projects still works fine with out any problems.

But now the incoming mail service rejects every incoming mail.

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)

What I have forgotten? Thanks for any hint!!

----------------------------------------------

appengine-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>
Dev Moerker
  • 111
  • 6

1 Answers1

0

I just tried the App Engine Hello World example as the base and modified to accept incoming emails and it works for me. The destination email address format I used is recipient-user@PROJECT_ID.appspotmail.com

I would recommend you verify debugging using a development app server and verify the following:

  1. Add some logging in your mail handler servlet when it gets the request. You can look at the example I've posted for some pointers.

  2. Go to http://localhost:8080/_ah/admin/inboundmail to access the Inbound Mail page in Development Console and send a test email from there.

  3. If you look at the server logs, you should see the logs from your servlet (i.e. the ones you added in Step 1) if the configuration is correct.

  4. Once you've confirmed everything works with the development app server, deploy your app to App Engine and test by sending a real email and look again at the Service logs through Stackdriver Logging. You should see 200 responses for such incoming mail requests in addition to the logs you added in Step 1.

Also note that many email providers will start throttling mails to a destination email ID if there is no successful response after few retries. This is another good reason to verify using a development app server first.

Here is the diff from the App Engine Hello World example where I got it working:

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>
Tuxdude
  • 47,485
  • 15
  • 109
  • 110
  • Hi, as I mentioned I have no problems with the appengine inbound mail service. The problem occurs, if I activate the new Google Identity Aware Proxy (IAP) for my project. Every request is secured by the IAP (Appengine, Endpoints etc). It seems that the inbound mail service is also "protected" by the IAP. This service didn't work as before anymore. Google Appengine didn't get the mail anyway, with activated IAP mails will be rejected. My Question is still, how should I configure the mail serivce when IAP is activated? – Dev Moerker Jul 10 '17 at 10:45