Recibir notificación de rebote

Para recibir notificaciones de rebote de correo, debes configurar tu aplicación para habilitar las notificaciones de rebote y gestionar las notificaciones entrantes en tu aplicación.

Configurar tu aplicación para recibir notificaciones de rebote de correo

De forma predeterminada, las aplicaciones no reciben notificaciones de rebote de los correos que no se han podido entregar. Para habilitar el servicio de notificaciones de rebote entrante, debes modificar los archivos de configuración appengine-web.xml y web.xml de tu aplicación.

Modifica appengine-web.xmlañadiendo una sección inbound-services para habilitar el servicio de rebote de correo entrante:

<inbound-services>
  <!-- Used to handle incoming mail. -->
  <service>mail</service>
  <!-- Used to handle bounced mail notifications. -->
  <service>mail_bounce</service>
</inbound-services>

Modifica web.xml asignando la URL de rebote /_ah/bounce a tu servlet de gestión de rebotes, de la siguiente manera:

<servlet>
  <servlet-name>bouncehandler</servlet-name>
  <servlet-class>com.example.appengine.mail.BounceHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>bouncehandler</servlet-name>
  <url-pattern>/_ah/bounce</url-pattern>
</servlet-mapping>
<security-constraint>
  <web-resource-collection>
    <web-resource-name>bounce</web-resource-name>
    <url-pattern>/_ah/bounce</url-pattern>
  </web-resource-collection>
  <auth-constraint>
    <role-name>admin</role-name>
  </auth-constraint>
</security-constraint>

Gestionar notificaciones de rebote

La API JavaMail incluye la clase BounceNotificationParser, que se usa para analizar las notificaciones de rebote entrantes, como se muestra a continuación:

import com.google.appengine.api.mail.BounceNotification;
import com.google.appengine.api.mail.BounceNotificationParser;

import java.io.IOException;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BounceHandlerServlet extends HttpServlet {

  private static final Logger log = Logger.getLogger(BounceHandlerServlet.class.getName());

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    try {
      BounceNotification bounce = BounceNotificationParser.parse(req);
      log.warning("Bounced email notification.");
      // The following data is available in a BounceNotification object
      // bounce.getOriginal().getFrom() 
      // bounce.getOriginal().getTo() 
      // bounce.getOriginal().getSubject() 
      // bounce.getOriginal().getText() 
      // bounce.getNotification().getFrom() 
      // bounce.getNotification().getTo() 
      // bounce.getNotification().getSubject() 
      // bounce.getNotification().getText() 
      // ...
    } catch (MessagingException e) {
    // ...
    }
  }
}