我正在开发 Google App Engine 应用程序。
我希望接收“%username%@appid.appspotmail.com”下的邮件,其中 %username% 属于该应用程序的用户。
我只是不知道在 web.xml
中定义什么文件。
任何类似的解决方案,例如发送邮件至:
- '%用户名% [email protected] '
- 'usermailbox.%username%@appid.appspotmail.com'
是可以接受的(如果使用通配符更容易的话)。
我已经尝试过(按照 Gopi 的建议)
将相关 servlet 映射到 <url-pattern>/_ah/mail/user.*</url-pattern>
web.xml
内文件。它不起作用。
客户端收到退回消息,而服务器日志确实显示应用程序收到的相关请求,但被拒绝并显示 404。否“没有与此 URL 匹配的处理程序。” INFO 将添加到日志条目中。此外,当获取生成的 URL 时,我没有得到“此页面不支持 GET”,而是得到一个普通的 404。
但是,如果我发送邮件说“[email protected]” ',日志显示 404(应该是这样,因为它没有映射到 web.xml 中)。此外,对于此类请求,会显示“没有与此 URL 匹配的处理程序”。 INFO 将添加到相关日志条目中。
不用说,在配置的服务下找到传入邮件。
最佳答案
当 App Engine 开始使用真正的 Java Web 服务器时,这种变化就发生了(所以 Toby 的解释是正确的……遗憾的是,我似乎无法恢复登录来投票!)。我的建议是使用过滤器。我在为 GAE 编写玩具应用程序时使用了下面的过滤器。一旦在本文末尾定义了基类,您就可以创建一系列邮件处理程序(如下所示)。您所要做的就是在 web.xml 中注册每个过滤器来处理/_ah/mail/*。
public class HandleDiscussionEmail extends MailHandlerBase {
public HandleDiscussionEmail() { super("discuss-(.*)@(.*)"); }
@Override
protected boolean processMessage(HttpServletRequest req, HttpServletResponse res)
throws ServletException
{
MimeMessage msg = getMessageFromRequest(req);
Matcher match = getMatcherFromRequest(req);
...
}
}
public abstract class MailHandlerBase implements Filter {
private Pattern pattern = null;
protected MailHandlerBase(String pattern) {
if (pattern == null || pattern.trim().length() == 0)
{
throw new IllegalArgumentException("Expected non-empty regular expression");
}
this.pattern = Pattern.compile("/_ah/mail/"+pattern);
}
@Override public void init(FilterConfig config) throws ServletException { }
@Override public void destroy() { }
/**
* Process the message. A message will only be passed to this method
* if the servletPath of the message (typically the recipient for
* appengine) satisfies the pattern passed to the constructor. If
* the implementation returns <code>false</code>, control is passed
* o the next filter in the chain. If the implementation returns
* <code>true</code>, the filter chain is terminated.
*
* The Matcher for the pattern can be retrieved via
* getMatcherFromRequest (e.g. if groups are used in the pattern).
*/
protected abstract boolean processMessage(HttpServletRequest req, HttpServletResponse res) throws ServletException;
@Override
public void doFilter(ServletRequest sreq, ServletResponse sres, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) sreq;
HttpServletResponse res = (HttpServletResponse) sres;
MimeMessage message = getMessageFromRequest(req);
Matcher m = applyPattern(req);
if (m != null && processMessage(req, res)) {
return;
}
chain.doFilter(req, res); // Try the next one
}
private Matcher applyPattern(HttpServletRequest req) {
Matcher m = pattern.matcher(req.getServletPath());
if (!m.matches()) m = null;
req.setAttribute("matcher", m);
return m;
}
protected Matcher getMatcherFromRequest(ServletRequest req) {
return (Matcher) req.getAttribute("matcher");
}
protected MimeMessage getMessageFromRequest(ServletRequest req) throws ServletException {
MimeMessage message = (MimeMessage) req.getAttribute("mimeMessage");
if (message == null) {
try {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
message = new MimeMessage(session, req.getInputStream());
req.setAttribute("mimeMessage", message);
} catch (MessagingException e) {
throw new ServletException("Error processing inbound message", e);
} catch (IOException e) {
throw new ServletException("Error processing inbound message", e);
}
}
return message;
}
}
关于google-app-engine - 应用程序引擎传入邮件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3567057/