Sending email with attachments using Gmail API in Android (execute() hangs) -


everything seems work long attachment small.
however, try attach larger file (7mb example), execute() method of send hangs.
tried go on documentation , if understand correctly should use send api performs upload however, didn't figure should provide these parameters.
here email generation method :

public mimemessage tomimemessage(string from, context context) throws messagingexception {     properties props = new properties();     session session = session.getdefaultinstance(props, null);      mimemessage mimemessage = new mimemessage(session);      mimemessage.setfrom(new internetaddress(from));     mimemessage.addrecipient(javax.mail.message.recipienttype.to, new internetaddress(recipient));     mimemessage.setsubject(subject);      mimebodypart mimebodytext = new mimebodypart();     mimebodytext.setcontent(body, "text/html");     mimebodytext.setheader("content-type", "text/plain; charset=\"utf-8\"");      multipart mp = new mimemultipart();     mp.addbodypart(mimebodytext);      if (attachments != null && attachments.size() > 0) {         mimebodypart mimebodyattachments = new mimebodypart();         (uri uri : attachments) {             string filename = uriutils.getfilename(uri, context);             string mimetype = uriutils.getmimetype(uri, context);             log.d(tag, "generating file info, uri=" + uri.getpath() + ", mimetype=" + mimetype);             fileinputstream = uriutils.generatefileinfo(context, uri, mimetype);             if (is == null) {                 throw new messagingexception("failed file uri=" + uri.getpath());             }             try             {                 datasource source = new bytearraydatasource(is, mimetype);                 mimebodyattachments.setdatahandler(new datahandler(source));                 mimebodyattachments.setfilename(filename);                 mimebodyattachments.setheader("content-type", mimetype + "; name=\"" + filename + "\"");             } catch (ioexception e) {                 throw new messagingexception(e.getmessage());             }         }         mimebodyattachments.setheader("content-transfer-encoding", "base64");         mimebodyattachments.setdisposition(mimebodypart.attachment);         mp.addbodypart(mimebodyattachments);     }      mimemessage.setcontent(mp);      return mimemessage; } 

.

message createmessagewithemail(mimemessage mimemessage) throws messagingexception, ioexception {         bytearrayoutputstream bytes = new bytearrayoutputstream();         mimemessage.writeto(bytes);         string encodedemail = base64.encodebase64urlsafestring(bytes.tobytearray());         message message = new message();         message.setraw(encodedemail);         return message;     } 

followed :

mimemessage mimemessage = email.tomimemessage(userid, context); message message = createmessagewithemail(mimemessage); gmail.users.messages messages = service.users().messages(); gmail.users.messages.send send = messages.send(userid, message); send.execute(); // method hangs when using large attachment 

as mentioned, think hitting upper limit of allowed message size. don't think (someone please correct me if i'm wrong) java gmail api client has built in support messages go on size, implement it.

under hood, messages.send-method results in regular http post request:

post https://www.googleapis.com/gmail/v1/users/{user_id}/messages/send content-type: application/json authorization: bearer {access_token}  {   "raw": "{message_url_safe_base64_encoded}" } 

this works ~5 mb total size discovered. if want use max limit of 35 mb need following:

post https://www.googleapis.com/upload/gmail/v1/users/{user_id}/messages/send?uploadtype=multipart content-type: message/rfc822 authorization: bearer {access_token}  "{message_in_rfc822_format}" 

notice upload in url, uploadtype=multipart url parameter, message/rfc822 content-type , message non-encoded in request body. this answer might give inspiration.

so need go around (again, correct me if i'm wrong) java client , use other library make regular http request yourself.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -