2011-11-15 83 views
2

我經過這裏的文件工作: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/[email protected]</url-pattern> 
</servlet-mapping> 
<security-constraint> 
    <web-resource-collection> 
    <url-pattern>/_ah/mail/[email protected]</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
    <role-name>admin</role-name> 
    </auth-constraint> 
</security-constraint> 

</web-app> 

我的第一個問題,我還沒有建立任何形式的額外電子郵件地址說:

接收傳入的電子郵件在[email protected]地址

我認爲這就像一個catchall和字符串可以是我喜歡的任何東西?

我的下一個問題是servlet會顯示任何東西,如果它工作?目前我剛剛得到The requested URL /mailserver was not found on this server.

任何幫助入門將是偉大的。

TIA

回答

4

電子郵件消息被髮送到由應用程序引擎生成的應用程式作爲HTTP請求(POST)。 [email protected]是一般語法。 string是一些你可以選擇的,例如對於特定的servlet映射url模式(路由到特定的servlet) - 在你的情況下,只有[email protected]將被MailHandlerServlet處理。

在此服務器上找不到請求的URL/mailserver。

我在代碼中看不到該URL的映射。網址路徑是/_ah/mail/url-pattern)。

如果HTTP調用/ servlet操作成功,您將獲得HTTP狀態碼20X。