I am using Gmail Java SDK for sending email with attachment.
From this document, the maximum of uploading file size is 35 MB. ( https://developers.google.com/gmail/api/v1/reference/users/messages/send ).
However, In the reality, I only can send email with attachment with only 5MB maximum size, beyond that I get 400 Bad Request Too Large error from google.
This is my code for creating the mime message before send:
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
email.setFrom(new InternetAddress(from));
email.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
email.setSubject(SUBJECT_RE + subject);
email.setReplyTo(new Address[]{new InternetAddress(from)});
String references = getMailReferences(messageId, service);
if(StringUtils.isNotEmpty(references)) {
email.setHeader(MAIL_HEADER_REFERENCES, references);
}
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(bodyText, "text/plain");
Multipart multipart = new MimeMultipart("mixed");
multipart.addBodyPart(mimeBodyPart);
for(int i=0 ; i< attachments.size() ; i++) {
EmailAttach attachment = attachments.get(i);
MimeBodyPart mimeBodyPartAttachment = new MimeBodyPart();
InputStream inputStream = new ByteArrayInputStream(attachment.getAttachmentBytes());
DataHandler dataHandler = new DataHandler(new InputStreamDataSource(inputStream, attachment.getFileName()));
mimeBodyPartAttachment.setDataHandler(dataHandler);
mimeBodyPartAttachment.setFileName(dataHandler.getName());
multipart.addBodyPart(mimeBodyPartAttachment);
}
email.setContent(multipart);
Is there any size limitation sending email with attachment with gmail or there is change needed in code to handle big attachment (example handle/creating MimeBodyPart) (>5MB)?
Solved, You need to use the method send with AbstractInputStreamContent parameter and DON'T do encode Base64 for the message content:
Message message = createMessageWithEmail(emailContent, threadId);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
emailContent.writeTo(buffer);
message = service.users().messages().send(userId, message, new ByteArrayContent("message/rfc822", buffer.toByteArray())).execute();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.