如何使用适用于.NET的API客户端库从Gmail发送包含多个附件的电子邮件

时间:2016-10-25 11:01:09

标签: google-api gmail-api google-api-dotnet-client

我的应用使用Google API client library for .NET发送带附件的电子邮件。

使用Send()时,我遇到了附件文件大小方面的一些限制。所以,我想切换到Resumable upload因为上传方法可能有所帮助。但它几乎没有记录。 查看源代码,我想使用different Send() overload可能是前进的方法,但我无法弄清楚如何正确使用它。

所以,不要将文件附加到消息中并像这样调用它:

var gmailResult = gmail.Users.Messages.Send(new Message
{
    Raw = base64UrlEncodedMessage
}, "me").Execute();

我不应该将文件附加到邮件中并执行类似的操作吗?

var gmailResult = gmail.Users.Messages.Send(new Message
{
    Raw = base64UrlEncodedMessage
}, "me", fileStream, contentType).Upload();
  1. 第二个版本不会返回任何API错误,但什么都不做。我显然在这里遗漏了一些东西。

  2. 如何附加多个附件?

1 个答案:

答案 0 :(得分:1)

This is kind of an old question, but putting an answer here just in case anyone else needs it:

I was able to achieve this by converting my mime message into a stream (attachment(s) included), and then calling this overload on Send:

UsersResource.MessagesResource.SendMediaUpload googleSendRequest = service.Users.Messages.Send(null, "youremail@gmail.com", mimeMessageStream, "message/rfc822");
IUploadProgress created = googleSendRequest.Upload();

This will upload all of the attachments with the email message content and then send it off. I was able to send two 5 megabyte attachments in an email. Previously I was not able to send even one of those via the other Send method that takes in a base64 encoded mime message.