我通过Java客户端库使用Gmail API,并且发现Gmail API insert(java.lang.String userId, com.google.api.services.gmail.model.Message content)
方法仅插入小于5Mb的文件。
我尝试使用Insert insert(java.lang.String userId, com.google.api.services.gmail.model.Message content, com.google.api.client.http.AbstractInputStreamContent mediaContent)
插入带有文件(大小为10MB)的消息。
MimeMessage email = GmailAPI.createEmail("toXXX@gmail.com", "fromYYY@gmail.com", "subject", "message body");
FileContent content = new FileContent("message/rfc822", new File("C:\\Users\\user\\someFile"));
Message message = createMessageWithEmail(email);
message = service.users().messages().insert(userId, message, mediaContent).execute();
CreateEmail是快速入门中的简单方法:
public static MimeMessage createEmail(String to,
String from,
String subject,
String bodyText)
throws MessagingException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
email.setFrom(new InternetAddress(from));
email.addRecipient(javax.mail.Message.RecipientType.TO,
new InternetAddress(to));
email.setSubject(subject);
email.setText(bodyText);
return email;
}
该消息出现在我的电子邮件框中,但没有文件。
这是怎么了?
UPD1::插入媒体内容时,仅插入不带附件的电子邮件。我认为我以错误的方式使用它。
答案 0 :(得分:0)
我已经弄清楚了如何使用Java库(通过REST的相同想法)使用可恢复上传:
byte[] bytes = Base64.decodeBase64(message.getRaw());
Insert insertReq;
Message content = new Message();
content.setLabelIds(message.getLabelIds());
try {
insertReq = gmail.users().messages().insert(account, content,
new ByteArrayContent("message/rfc822", bytes, 0, bytes.length));
helper.execute(insertReq, task.getPartnerID(), task.isUnderWhiteLable());
} catch (IOException e) {
Log.error("Failed to insert a message with resumable upload message", e);
}
该方法的描述被证明是很容易理解的,但是事后才知道。我不知道为什么说明这么差:
/**
* Directly inserts a message into only this user's mailbox similar to IMAP APPEND, bypassing most
* scanning and classification. Does not send a message.
*
* Create a request for the method "messages.insert".
*
* This request holds the parameters needed by the the gmail server. After setting any optional
* parameters, call the {@link Insert#execute()} method to invoke the remote operation.
*
* <p>
* This method should be used for uploading media content.
* </p>
*
* @param userId The user's email address. The special value me can be used to indicate the authenticated user.
* [default: me]
* @param content the {@link com.google.api.services.gmail.model.Message} media metadata or {@code null} if none
* @param mediaContent The media HTTP content or {@code null} if none.
* @return the request
* @throws java.io.IOException if the initialization of the request fails
*/
public Insert insert(java.lang.String userId, com.google.api.services.gmail.model.Message content, com.google.api.client.http.AbstractInputStreamContent mediaContent) throws java.io.IOException {
Insert result = new Insert(userId, content, mediaContent);
initialize(result);
return result;
}
答案 1 :(得分:0)
要插入大于5MB的电子邮件,您必须使用分段上传或可恢复上传(有关详细信息,请参见docs)。
在Java Gmail API客户端库中,insert
方法的2参数版本发送简单上传请求,该请求最多可以上传5MB。要发送可恢复的上载请求,请使用3参数版本的insert
方法。使用断点续传,您最多可以插入50MB。
这是一种使用断续上传插入javax.mail.internet.MimeMessage
的方法:
import com.google.api.client.http.ByteArrayContent;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.Message;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import javax.mail.MessagingException;
private Message insertMessage(Gmail gmail, String userId, MimeMessage mimeMessage, List<String> labelIds, String threadId) throws IOException, MessagingException {
// Create Message instance containing email message metadata
Message metadata = new Message()
.setLabelIds(labelIds)
.setThreadId(threadId);
// Create byte array containing email message data (in RFC 2822 format)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mimeMessage.writeTo(baos);
ByteArrayContent rawMessageBytes = new ByteArrayContent("message/rfc822", baos.toByteArray());
return gmail.users().messages()
.insert(userId, metadata, rawMessageBytes)
.setInternalDateSource("dateHeader") // The GMail internal message time is based on the Date header in the email, when valid.
.execute();
}