我正在使用Gmail Java SDK发送带有附件的电子邮件。
根据该文档,上传文件的最大大小为35 MB。 (https://developers.google.com/gmail/api/v1/reference/users/messages/send)。
但是,实际上,我只能发送最大大小为5MB且带有附件的电子邮件,除此之外,我还会从Google收到400错误的请求太大错误。
这是我在发送前创建mime消息的代码:
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);
在发送带有gmail附件的电子邮件时是否存在大小限制,或者是否需要更改代码以处理大附件(示例句柄/创建MimeBodyPart)(> 5MB)?
答案 0 :(得分:0)
已解决,您需要使用带有AbstractInputStreamContent参数的send方法,并且不要对消息内容进行Base64编码:
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();