rlalfo / google-http-java-client

Automatically exported from code.google.com/p/google-http-java-client
0 stars 0 forks source link

Gmail api gets timeout when trying to create a draft with a 2MB image attachment #270

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Version of google-http-java-client - 1.19

Java environment - Java 6

Describe the problem.
When trying to create a draft with a 2MB image attachment, the request gets a 
timeout, yet with little images it works just fine.

Code I'm using:
public void createDraft(String to, String bcc, JSONObject details)
    {
        try
        {
            MimeMessage email = new MimeMessage(Session.getDefaultInstance(new Properties(), null));
            email.addRecipient(RecipientType.TO, new InternetAddress(to));

            if(bcc != null)
            {
                email.addRecipient(RecipientType.BCC, new InternetAddress(bcc));
            }

            email.setSubject(details.getString("subject"));

            JSONArray attachments = details.getJSONArray("attachments");

            if(attachments.length() == 0)
            {
                email.setText(details.getString("content"));
            }
            else
            {
                Multipart multipart = new MimeMultipart();

                //content
                MimeBodyPart mimeBodyPart = new MimeBodyPart();
                mimeBodyPart.setContent(details.getString("content"), "text/plain");
                mimeBodyPart.setHeader("Content-Type", "text/plain; charset=\"UTF-8\"");
                multipart.addBodyPart(mimeBodyPart);

                //Attachments
                for(int i = 0; i < attachments.length(); i++)
                {
                    String fileName = attachments.getString(i);
                    String shortFileName = fileName.substring(0, fileName.lastIndexOf("_"));

                    mimeBodyPart = new MimeBodyPart();
                    DataSource source = new FileDataSource(String.format("%s%s", UPLOADS_DIR, fileName));

                    mimeBodyPart.setDataHandler(new DataHandler(source));
                    mimeBodyPart.setFileName(shortFileName);
                    mimeBodyPart.setHeader("Content-Type", String.format("%s; name=\"%s\"", getMimeTypeFromFileName(shortFileName), shortFileName));
                    mimeBodyPart.setHeader("Content-Transfer-Encoding", "base64");

                    multipart.addBodyPart(mimeBodyPart);
                }

                email.setContent(multipart);
            }

            Draft draft = new Draft();
            draft.setMessage(getMessageFromEmail(email));

            if(Trace.debug()) Trace.debug("GmailAPI.createDraft: Creating draft for %s (%d attachments)", to, attachments.length());

            long before = System.currentTimeMillis();
            draft = m_service.users().drafts().create("me", draft).execute();
            long after = System.currentTimeMillis();

            if(Trace.debug()) Trace.debug("Creating draft took %s seconds!", (after - before) / 1000);

            if((draft == null) || (draft.getId() == null))
            {
                if(Trace.error()) Trace.error("GmailAPI.createDraft: Failed creating draft for %s", to);
            }
        }
        catch(Exception e)
        {
            if(Trace.error()) Trace.error("GmailAPI.createDraft: Failed creating draft for %s: %s", to, e, e.getMessage());
        }
    }

How would you expect it to be fixed?
Can you please tell me if there's a better way to create the draft with 
attachments?

Thanks.

Original issue reported on code.google.com by noamsin...@gmail.com on 12 Dec 2014 at 3:57

GoogleCodeExporter commented 9 years ago
Have you tried posting your question on StackOverflow with the 'gmail-api' tag 
[1] ? There are Gmail engineers who would better assist you on that forum.

[1] https://stackoverflow.com/questions/tagged/gmail-api

Original comment by wonder...@google.com on 30 Dec 2014 at 11:19