App Engine和接收邮件

时间:2011-11-15 22:33:18

标签: java google-app-engine

我正在处理这些文件:

http://code.google.com/appengine/docs/java/mail/overview.html#Receiving_Mail_in_Java

接收电子邮件。下面是我的Servlet:

package mailserver;

import java.io.IOException; 
import java.util.Properties; 
import javax.mail.MessagingException;
import javax.mail.Session; 
import javax.mail.internet.MimeMessage; 
import javax.servlet.http.*; 

public class MailHandlerServlet extends HttpServlet { 
    public void doPost(HttpServletRequest req, 
                       HttpServletResponse resp) 
            throws IOException { 
        Properties props = new Properties(); 
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message =  null;
        try {
            message = new MimeMessage(session, req.getInputStream());
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        resp.setContentType("text/plain");
        resp.getWriter().println(message);
    }
}

我的appengine-xml:

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <application>myapp</application>
  <version>4</version>

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

  <!--
    By default, App Engine sends requests serially to a given web server.
    To allow App Engine to send multiple requests in parallel specify:

      <threadsafe>true</threadsafe>
  -->

  <!-- Configure java.util.logging -->
  <system-properties>
    <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
  </system-properties>

</appengine-web-app>

和web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <servlet>
  <servlet-name>MailHandlerServlet</servlet-name>
  <servlet-class>mailserver.MailHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>MailHandlerServlet</servlet-name>
  <url-pattern>/_ah/mail/notification@myemail.appspotmail.com</url-pattern>
</servlet-mapping>
<security-constraint>
  <web-resource-collection>
    <url-pattern>/_ah/mail/notification@myemail.appspotmail.com</url-pattern>
  </web-resource-collection>
  <auth-constraint>
    <role-name>admin</role-name>
  </auth-constraint>
</security-constraint>

</web-app>

我的第一个问题是,我没有设置任何类型的额外电子邮件地址:

通过string@appid.appspotmail.com地址

接收收到的电子邮件

我认为这就像一个笼子,字符串可以是我喜欢的任何东西?

我的下一个问题是servlet是否会显示任何内容?目前我刚刚获得The requested URL /mailserver was not found on this server.

任何帮助入门都会很棒。

TIA

1 个答案:

答案 0 :(得分:4)

电子邮件将作为App Engine生成的HTTP请求(POST)发送到您的应用。 string@appid.appspotmail.com是一般语法。您可以选择string,例如对于servlet映射的特定url模式(路由到特定的servlet) - 在你的情况下,MailHandlerServlet只处理notification@myemail.appspotmail.com

  

在此服务器上找不到请求的URL /邮件服务器。

我无法在您的代码中看到该网址的映射。网址路径为/_ah/mail/url-pattern)。

如果HTTP调用/ servlet操作成功,您将获得HTTP状态代码20X。