Google App Engine Mail Service

the sender must be a real existing user

Receive

war/WEB-INF/appengine-web.xml http://bazaar.launchpad.net/~ryoji-kodakari/gt/trunk/view/head:/war/WEB-INF/appengine-web.xml

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

web.xml (for recieving) http://bazaar.launchpad.net/~ryoji-kodakari/gt/trunk/view/head:/war/WEB-INF/web.xml

  <servlet>
    <servlet-name>Mail</servlet-name>
    <servlet-class>com.tidspr.tsr.mail.MailRR</servlet-class>
  </servlet>

  <servlet-name>Mail</servlet-name>
    <url-pattern>/_ah/mail/*</url-pattern>
  </servlet-mapping>

com.tidspr.tsr.mail.MailRR http://bazaar.launchpad.net/~ryoji-kodakari/gt/trunk/view/head:/src/com/tidspr/tsr/mail/MailRR.java

    @Override
    public void doPost(final HttpServletRequest req, final HttpServletResponse res) throws IOException
    {
        final Properties props = new Properties();
        final Session session = Session.getDefaultInstance(props, null);
        try
        {
            final MimeMessage message = new MimeMessage(session, req.getInputStream());
            final String fr = message.getFrom()[0].toString();
            final String to = message.getAllRecipients()[0].toString();
            final String su = message.getSubject();
            final Text tx = new Text((String) message.getContent());
            final java.util.Date da = message.getSentDate();
            final Mail m = new Mail(fr, to, da, su, tx);
            final Key k = KeyFactory.createKey(Mail.class.getSimpleName(), Long.toString(new Date().getTime()));
            m.key(k);
            final PersistenceManager pm = PMF.get().getPersistenceManager();//*PMF
            pm.makePersistent(m);
            pm.flush();
            pm.close();
        }
        catch (final MessagingException e)
        {
            e.printStackTrace();
        }
    }

*PMF

entity.Mail

package com.tidspr.tsr.entity;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Mail
{

    public Date getDate()
    {
        return date;
    }

    public String getFrom()
    {
        return from;
    }

    public String getSubject()
    {
        return subject;
    }

    public Text getText()
    {
        return text;
    }

    public String getTo()
    {
        return to;
    }

    public Key key()
    {
        return key;
    }

    public void key(final Key key)
    {
        this.key = key;
    }

    private void dummy()
    {
        this.from = "";
        this.date = new Date();
        this.subject = "";
        this.text = new Text("");
        this.to = "";
    }

    public Mail(final String from, final String to, final Date date, final String subject, final Text text)
    {
        this.from = from;
        this.to = to;
        this.date = date;
        this.subject = subject;
        this.text = text;
    }

    @Persistent
    private Date date;

    @Persistent
    private String from;

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String subject;

    @Persistent
    private Text text;

    @Persistent
    private String to;

}

now you can receive mail. something like. admin@{yourapp}.appspotmail.com. for example admin@tertesttest.appspotmail.com
/file/Google App Engine Mail Service/gem.png

send

  1. make user account or use your google account(must be app owner)
  2. make program

user creation

appengine > permission > invite admin > send mail admin@{yourapp}.appspotmail.com. for example admin@tertesttest.appspotmail.com
/file/Google App Engine Mail Service/ginv1.png
check datastore viewer > Mail. you've would received the invitation mail from google
/file/Google App Engine Mail Service/cdb.png
open link in the mail.
/file/Google App Engine Mail Service/ginv.png
now. your account confirmed. this mail account admin@tertesttest.appspotmail.com is actually existing, even able to send email.

program

it's useful to make notice when comments time line they commited has been updated to authors or admin@tertesttest.appspotmail.com itself.

CommentPost.java http://bazaar.launchpad.net/~ryoji-kodakari/gt/trunk/view/head:/src/com/tidspr/tsr/CommentPost.java

        final Properties props = new Properties();
        final Session session = Session.getDefaultInstance(props, null);

        String msgBody = "";
        msgBody = "The wiki page you added comment has got another comment.\n\n";
        msgBody += "Wiki Title | " + wiki + "\n";
        msgBody += "Wiki URL   | " + "[http://" + req.getServerName() + "/wiki/" + wiki + "]\n";
        msgBody += "Object     | "
                + "\n===================================================\n" 
                + ((raw.length() > 200) ? raw.substring(0, 200) : raw) 
                + "\n===================================================\n\n\n";
        msgBody += "this message sent from " + req.getServerName() + " by script. please don't reply.";

        final PersistenceManager pm = PMF.get().getPersistenceManager();
        try
        {
            final Map<String, String> us = new HashMap<String, String>();
            final String query = "select from " + Comment.class.getName() + " where wiki=='" + wiki.replace("'", "\\'") + "'";
            final Query que = pm.newQuery(query);
            que.setOrdering("date descending");
            final List<Comment> list = (List<Comment>) que.execute();
            for (final Comment c : list)
            {
                us.put(c.email(), c.name()); // group by email
            }
            final Set<String> ks = us.keySet();
            for (final String email : ks)
            {
                final Message msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress("admin@" + req.getServerName().replace("appspot.com", "appspotmail.com"), req.getServerName() + " Administorator"));
                msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email, us.get(email)));
                msg.setSubject("[" + req.getServerName() + "] " + "Your commented page has got another comment.");
                msg.setText(msgBody);
                Transport.send(msg);
                log.info("[Massage Sent][" + email + "][" + us.get(email) + "]");
            }
        }
        catch (final AddressException e)
        {
            e.printStackTrace();
        }
        catch (final MessagingException e)
        {
            e.printStackTrace();
        }
        catch (final UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

done.


Mail Viewer

/file/Google App Engine Mail Service/mlv.png

because Mail is saved as just a normal record entity. Thus can be displayed as you want.

MailRR.java http://bazaar.launchpad.net/~ryoji-kodakari/gt/trunk/view/head:/src/com/tidspr/tsr/mail/MailRR.java

    @Override
    public void doGet(final HttpServletRequest req, final HttpServletResponse res)
    {
        final com.google.appengine.api.users.User u = UserServiceFactory.getUserService().getCurrentUser();
        if (u == null)
        {
            permission(res);
        }
        final com.google.appengine.api.users.UserService service = UserServiceFactory.getUserService();
        if (!service.isUserAdmin())
        {
            permission(res);
            return;
        }

        final PersistenceManager pm = PMF.get().getPersistenceManager();
        final javax.jdo.Query que = pm.newQuery("SELECT FROM " + com.tidspr.tsr.entity.Mail.class.getName());
        que.setRange(0, 47);// 48
        que.setOrdering("date descending");
        System.out.println(que.toString());
        final List<Mail> list = (List<Mail>) que.execute();
        try
        {
            res.setContentType("text/html");
            res.setCharacterEncoding("UTF-8");
            final PrintWriter out = res.getWriter();

            for (final Mail m : list)
            {
                out.write("<table>");
                out.write("<tr><th width='190'>" + new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(m.getDate()) + "</th><th width='10'> < </th><th width='400'>" + encode(m.getFrom()) + "</th></tr>" + "<tr><td colspan='3'>" + encode(m.getSubject()) + "</td></tr>" + "<tr><td colspan='3'><pre class='mail'>" + m.getText().getValue() + "</pre></td></tr>");
                out.write("</table>");
            }
        }
        catch (final IOException e)
        {
            e.printStackTrace();
        }
    }

    private String encode(final String str)
    {
        return str.replace("<", "<").replace(">", ">");
    }

and use directory or with javascript http://bazaar.launchpad.net/~ryoji-kodakari/gt/trunk/view/head:/src/com/tidspr/tsr/Wiki.java

            out.write("<script src=\"/jquery-1.4.2.min.js\"></script>");
            out.write("function mailreload() {");
            out.write(" $.get('/_ah/mail', function(data) {");
            out.write("             $('#mailtable').html(data);");
            out.write("        });");
            out.write("};");

Dump & Restore & Privacy Problem
Dump /ms.export.json authorization needed
Restore /ms.import.json authorization needed

this app can export & import mails, but doesn't offer any encryption on connection.
so, thus, might be shouldn't use those functions, although it does contains only NickName and Email Address.

Another Worry

When I logged in with Google Account and viewing mails, Google bot do indexing that mail page and cache its contents? If so, it doesn't work at all even there are Authorizations. Would be all such connections must be with https protocol.

    last modified: 11 July 2011 [ View wiki source Close ]
     
SEARCH DIFFERENCE ATTACHMENT RECENT POST