android - Get sender mail in gmail-api -
i trying figure out went through gmail-api developer guide.the message part in gmail-api not contain detail sender appreciated thanks.
ok did it.get message payload , headers payload loop through headers name "from"
format of header of form header=[name:"somename" value:"somevalue"]
here code hope helps someone
private list<messagereader> getdatafromapi() throws ioexception { string user = "me"; list<messagereader> labels = new arraylist<messagereader>(); listmessagesresponse listresponse = mactivity.mservice.users().messages().list(user).setq(query).execute(); //int i=0; (message label : listresponse.getmessages()) { message m = mactivity.mservice.users().messages().get(user, label.getid()).execute(); string =""; try{ list<messagepart> parts =m.getpayload().getparts(); list<messagepartheader> headers = m.getpayload().getheaders(); messagereader mreader = readparts(parts); mreader.setdate(m.getinternaldate()); for(messagepartheader header:headers){ string name = header.getname(); if(name.equals("from")||name.equals("from")){ mreader.setsender(header.getvalue()); break; } } labels.add(mreader); }catch(exception e){ a+="the following error occurred:\n" + e.getmessage(); } } return labels;//extractdata(labels); } private messagereader readparts(list<messagepart> parts){ messagereader mreader = new messagereader(); int cnt =0; for(messagepart part:parts){ try{ string mime = part.getmimetype(); if(mime.contentequals("text/plain")){ string s = new string(base64.decodebase64(part.getbody().getdata().getbytes())); mreader.settext(s); }else if(mime.contentequals("text/html")){ string s = new string(base64.decodebase64(part.getbody().getdata().getbytes())); mreader.sethtml(s); }else if(mime.contentequals("multipart/alternative")){ list<messagepart> subparts =part.getparts(); messagereader subreader = readparts(subparts); mreader.settext(subreader.gettext()); mreader.sethtml(subreader.gethtml()); }else if(mime.contentequals("application/octet-stream")){ cnt++; mreader.setno_of_files(cnt); } }catch(exception e){ // file here } } return mreader; } public class messagereader { private string text; private string html; int no_of_files; private string sender; private long date; // file data made public string getsender() { return sender; } public void setsender(string sender) { this.sender = sender; } public long getdate() { return date; } public void setdate(long date) { this.date = date; } public string gettext() { return text; } public void settext(string text) { this.text = text; } public string gethtml() { return html; } public void sethtml(string html) { this.html = html; } public int getno_of_files() { return no_of_files; } public void setno_of_files(int no_of_files) { this.no_of_files = no_of_files; } }
Comments
Post a Comment