190 likes | 480 Views
A survey of Gmail API. Introduction (1/2). GmailAgent based on .Net http://sourceforge.net/projects/gmail-api G4j based on JAVA (complete document) http://g4j.sourceforge.net/doc jGmail jGmail is a framework to access all features of Google's GMail service programmatically using Java
E N D
Introduction (1/2) • GmailAgent based on .Net • http://sourceforge.net/projects/gmail-api • G4j based on JAVA (complete document) • http://g4j.sourceforge.net/doc • jGmail • jGmail is a framework to access all features of Google's GMail service programmatically using Java • http://sourceforge.net/projects/jgmail/
Introduction (2/2) • libgmailer based on PHP (2007)(document) • It uses the CURL extension of PHP to handle the HTTP/HTTPS traffic. • http://gmail-lite.sourceforge.net/wordpress/index.php/about/libgmailer/ • libgmail — Python binding for Google's Gmail service (2008) • http://libgmail.sourceforge.net/ • Feedparser • Parse RSS and Atom feeds in Python • http://www.feedparser.org/
G4j (Gmail API based on JAVA) (1/5) • java.lang.Object • siuying.gm.GMConnector • GMConnector() • Create an GMConnector object to connect and request gmail for various tasks • GMConnector(java.lang.String user, java.lang.String passwd, int tz) • Create an GMConnector object to connect and request gmail for various tasks
G4j (2/5) • void setPasswd(java.lang.String passwd) • Set password of the connection • void setProxy(java.lang.String phost, int pport) • Set the HTTP proxy host address and port • void setTimezone(java.lang.String timezone) • Set timezone of the connection • void setUser(java.lang.String user) • Set username of the connection
G4j (3/5) • boolean connect() • Connect and login to gmail • void disconnect() • disconnect to gmail network
G4j (4/5) • java.lang.Object • siuying.gm.structure.GMSendMailBody • voidaddAttachment(java.lang.String file) • Add an attachment • voidaddAttachmentLink(java.lang.String threadId, java.lang.String entryId, java.lang.String attachmentId) • Add an attachment from a file existing in gmail
G4j (5/5) • void addBcc(java.lang.String address) • Add a bcc receiver • void addCc(java.lang.String address) • Add a cc receiver • void addTo(java.lang.String address) • Add a to receiver
To-do • Working • [g4j] Send mail • High Priority • [g4j][design&implementation] New Contact Interface (Design and Implementation) • [g4j][test] Retrive full conversation • [gmailer4j] Send mail • [gmailer4j] Open browser(open links in email and open gmail mailbox) • Mid Priority • [gmailer4j] download all messages from gmail • [gmailer4j] Search email • [gmailer4j] Retrive attachment • [gmailer4j] Browse Contact list, send mail to contact list • [gmailer4j] browse mail with contact list
jGmail(1/3) • package net.sf.jgmail; • import net.sf.jgmail.*; • import net.sf.jgmail.datapack.*; • import java.util.Collection; • import org.apache.commons.httpclient.*;
jGmail(2/3) • public class GMail{ • public GMail(){} • public static void main(String[] args) • { • // create new session and assign username and password • GmailSession myAccount = new GmailSession("user", "passwd"); • // init new adapter • GmailAdapter gmail = new GmailAdapter(myAccount);
jGmail(3/3) • // login and retrieve mailbox info • //GmailAdapter.RequestType loginResult = gmail.refresh(GmailAdapter.ThreadFetchType.AllUnread); • // display mailbox info • //if(loginResult == GmailAdapter.RequestType.Success) { • // show new inbox count • System.out.println("New Threads: " + myAccount.getUnreadThreads() ); • // if new threads exist, show the subject of the first one • if( myAccount.getUnreadThreads().size() > 0) { • GmailThread newThread = (GmailThread)myAccount.getUnreadThreads(); • System.out.println("Latest thread subject: " + newThread.getSubjectHtml() ); • } • //} • } • }
libgmail(1/1) • import libgmailga = libgmail.GmailAccount("google@gmail.com", "mymailismypass")ga.login()folder = ga.getMessagesByFolder('inbox')for thread in folder: print thread.id, len(thread), thread.subject for msg in thread: print " ", msg.id, msg.number, msg.subject print msg.source
Feedparser(1/4) • import urllib # For BasicHTTPAuthentication • import feedparser # For parsing the feed • from textwrap import wrap # For pretty printing assistance • _URL = "https://mail.google.com/gmail/feed/atom" • def auth(): • '''The method to do HTTPBasicAuthentication''' • opener = urllib.FancyURLopener() • f = opener.open(_URL) • feed = f.read() • return feed
Feedparser(2/4) • def fill(text, width): • '''A custom method to assist in pretty printing''' • if len(text) < width: • return text + ' '*(width-len(text)) • else: • return text
Feedparser(3/4) • def readmail(feed): • '''Parse the Atom feed and print a summary''' • atom = feedparser.parse(feed) • print "" • print atom.feed.title • print "You have %s new mails" % len(atom.entries) • # Mostly pretty printing magic • print "+"+("-"*84)+"+" • print "| Sl.|"+" Subject"+' '*48+"|"+" Author"+' '*15+"|" • print "+"+("-"*84)+"+" • for i in xrange(len(atom.entries)): • print "| %s| %s| %s|" % ( • fill(str(i), 3), • fill(wrap(atom.entries[i].title, 50)[0]+"[...]", 55), • fill(wrap(atom.entries[i].author, 15)[0]+"[...]", 21)) • print "+"+("-"*84)+"+"
Feedparser(4/4) • if __name__ == "__main__": • f = auth() # Do auth and then get the feed • readmail(f) # Let the feed be chewed by feedparser