Google App Engineã§åä¿¡ã¡ã¼ã«ã®å¦çãã§ããããã«ãªã£ãã
å
·ä½çãªæé ã¯ãã¡ãã
http://code.google.com/intl/en/appengine/docs/java/mail/receiving.html
æé ã¯ããã
ã¾ããappengine-web.xmlã«æ¬¡ã®è¨å®ã追å
<inbound-services> <service>mail</service> </inbound-services>
ããããã¨ã[email protected]ã«ã¡ã¼ã«ãæ¥ãã /_ah/mail/<address> ã¨ããURLãå¼ã³åºãããããã«ãªãã
ãªã®ã§ã次ã®ãããªãµã¼ãã¬ãããããã³ã°ãweb.xmlã«è¿½å ãã¦ãµã¼ãã¬ããã§å¦çãããã
<servlet> <servlet-name>mailhandler</servlet-name> <servlet-class>MailHandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>mailhandler</servlet-name> <url-pattern>/_ah/mail/*</url-pattern> </servlet-mapping>
ç¹å®ã®ã¡ã¼ã«ã ããåãåãã¨ãã®url-patternã«ã¯ããã¡ã¤ã³åã¾ã§å«ããå¿
è¦ãããã®ã§æ³¨æã
<servlet-mapping> <servlet-name>mailhandler</servlet-name> <url-pattern>/_ah/mail/[email protected]</url-pattern> </servlet-mapping>
ãã®ã¨ãããã®ã¾ã¾ã ã¨/_ah/mail/hogeã«å¤ããã¢ã¯ã»ã¹ããã¦ãã¾ãã®ã§ã管çè
権éãããªãã¨ã¢ã¯ã»ã¹ã§ããªãããã«ããããã®ãããã¯queueã¨ãã¨ä¸ç·ã
<security-constraint> <web-resource-collection> <url-pattern>/_ah/mail/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>admin</role-name> </auth-constraint> </security-constraint>
ãã¨ã¯ããµã¼ãã¬ãããè¨è¿°ããã
åé¡ã¯ããã®ä¸æãæ£ãããªããã¨ã(â»2009/12/3 æåãå¤ãã£ã¦æ£ãããªã£ãããã )
The getContent() method returns an object that implements the Multipart interface. You can then call getCount() to determine the number of parts and getBodyPart(int index) to return a particular body part.
getContentã¯Multipartãimplementsãããªãã¸ã§ã¯ããããªãByteArrayInputStreamãè¿ãã
ã¡ã¼ã«ãplain/textã®å ´åã¯ãããããã®ã¾ã¾èªã¿è¾¼ãã¨æ¬æã«ãªã£ã¦ããã
multipart/alternative(HTMLã¡ã¼ã«)ã¨ãmultipart/mixed(æ·»ä»ã¡ã¼ã«)ã®å ´åã¯ããããByteArrayDataSourceã«æ¸¡ãã¦MimeMultipartãªãã¸ã§ã¯ããçæããã
ã¨ãããã¨ã§ãã¡ã¼ã«ãå¦çããæ®éã®ãµã¼ãã¬ãããæ¸ãã°ããã¨ãããã¨ã«ãªãã
ããã§ã¯ã次ã®ãããªã¨ã³ãã£ãã£ãä¿åããã³ã¼ããæ¸ãã¦ã¿ãã
@PersistenceCapable(identityType = IdentityType.APPLICATION) public class ReceivedMail { @PrimaryKey @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY) Key id; @Persistent String from; @Persistent String subject; @Persistent String text; }
PMFã¯ãJDOã®ããã¥ã¡ã³ãã®ãµã³ãã«ã§å®ç¾©ããã¦ããã¤ãããã¨ã¯é©å½ã«è£å®ãã¦ããã£ã½ãã¯ã©ã¹ãimportããã
public class MailHandlerServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PersistenceManager pm = PMF.get().getPersistenceManager(); Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); try { MimeMessage message = new MimeMessage(session, request.getInputStream()); ReceivedMail mail = new ReceivedMail(); mail.setSubject(message.getSubject()); //ãã¼ã«ã«ãµã¼ãã¼ã§ã¯æååãããã®ã§æ¬¡ã®ãããªã³ã¼ããå¿ è¦ãæ¬çªãµã¼ãã¼ã§ã¯ä¸è¦ //new String(message.getSubject().getBytes("8859_1"), "UTF-8")); mail.setFrom(message.getFrom()[0].toString()); String contentType = message.getContentType(); InputStream is = null; //2009/12/11 æåãããã£ããã¨ã«å¯¾å¿ String mess = ""; if(message.isMimeType("text/plain")){ //ãµã¤ãã®ã¡ã¼ã«ã®å¦ç /* 2009/12/11 ãããæåãå¤ãã£ã¦ãã®ã§ãClassCastExceptionã«ãªãã¾ãã is = (InputStream) message.getContent(); */ mess = (String)message.getContent(); }else{ //HTMLã¡ã¼ã«ãæ·»ä»ã¡ã¼ã«ã®å¦ç /* 2009/12/3 æåãå¤ãã£ãã®ã§ããã§ã¯ClassCastExceptionãçºçãã Multipart content = new MimeMultipart( new ByteArrayDataSource( (InputStream)message.getContent(), message.getContentType())); */ Multipart content = (Multipart)message.getContent(); for(int i = 0; i < content.getCount(); ++i){ BodyPart bp = content.getBodyPart(i); if(!bp.isMimeType("text/plain")) continue; is = bp.getInputStream(); contentType = bp.getContentType(); break; } } if(is != null){ //contentTypeããã¨ã³ã³ã¼ãã£ã³ã°ãåå¾ String encoding = null; String[] elms = contentType.split(";"); for(String elm : elms){ if(elm.trim().startsWith("charset=")){ encoding = elm.trim().substring("charset=".length()); } } Reader r = null; if(encoding != null){ //ã¨ã³ã³ã¼ãã£ã³ã°ãå ¥ã£ã¦ãã if(encoding.startsWith("\"")) encoding = encoding.substring(1); if(encoding.endsWith("\"")) encoding = encoding.substring(0, encoding.length() - 1); r = new InputStreamReader(is, encoding); }else{ //ã¨ã³ã³ã¼ãã£ã³ã°ãå ¥ã£ã¦ããªã r = new InputStreamReader(is); } //2009/12/11 æåãããã£ããã¨ã«å¯¾å¿ //String mess = ""; BufferedReader buf = new BufferedReader(r); for(String line; (line = buf.readLine()) != null;){ mess += line + "\n"; } //2009/12/11 æåãããã£ããã¨ã«å¯¾å¿ //mail.setText(mess); } //2009/12/11 æåãããã£ããã¨ã«å¯¾å¿ mail.setText(mess); pm.makePersistent(mail); } catch (MessagingException ex) { throw new ServletException(ex); }finally{ pm.close(); } } }