반송 알림 받기

이메일 반송 알림을 받으려면 앱에서 반송 알림을 사용 설정하고 수신한 알림을 처리해야 합니다.

이메일 반송 알림을 받도록 애플리케이션 구성

기본적으로 애플리케이션은 전달할 수 없는 이메일에 대한 반송 알림을 받지 못합니다. 반송 알림 수신 서비스를 사용 설정하려면 애플리케이션 appengine-web.xmlweb.xml 구성 파일을 수정해야 합니다.

반송 알림 수신 서비스를 사용 설정하도록 inbound-services 섹션을 추가하여 appengine-web.xml을 수정합니다.

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

다음과 같이 반송 URL /_ah/bounce를 반송 처리 서블릿에 매핑하여 web.xml을 수정합니다.

<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>

반송 알림 처리

JavaMail API에는 다음과 같이 수신한 반송 알림을 파싱하는 데 사용되는 BounceNotificationParser 클래스가 포함되어 있습니다.

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) {
    // ...
    }
  }
}