如何从发送到 google 组邮件地址的电子邮件中下载附件

How to download attachment from email sent to google group mail address

假设我的电子邮件 a@company.com 在 google 组中,该组的电子邮件地址为 group@company.com。我们有一个 g 套件服务帐户,它启用了 Google Apps Domain-wide Delegation。 我们有电子邮件从 b@companyb.com 发送到我们的群组电子邮件 group@company.com,主题为 Report from company b 并在电子邮件中附上报告。

问题是 gmail api 能够列出所有邮件,但无法列出每封电子邮件中的附件。

有什么办法吗?

这是我的代码:

    // here when I create the client, I use my email address `a@company.com`
    using(var client = CreateClient())
    {
        UsersResource.MessagesResource.ListRequest request = client.Users.Messages.List("me");

        request.Q = "from:b@compayb.com AND subject:Report from company b AND has:attachment"

        // List messages.
        var messageIds = request.Execute().Messages?.Select(m => m.Id) ?? new List<string>();

        foreach(var mId in messageIds)
        {
            // https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get
            Message message = client.Users.Messages.Get("me", messageId).Execute();
            IList<MessagePart> parts = message.Payload.Parts;
            foreach (MessagePart part in parts)
            {
                if (!String.IsNullOrEmpty(part.Filename))
                {
                    String attId = part.Body.AttachmentId;
                    MessagePartBody attachPart = client.Users.Messages.Attachments.Get("me", messageId, attId).Execute();

                    // Converting from RFC 4648 base64 to base64url encoding
                    // see http://en.wikipedia.org/wiki/Base64#Implementations_and_history
                    String attachData = attachPart.Data.Replace('-', '+');
                    attachData = attachData.Replace('_', '/');

                    byte[] data = Convert.FromBase64String(attachData);
                    var file = new FileInfo(part.Filename);
                    File.WriteAllBytes(file.FullName, data);
                }
            }
        }
    }

如果我手动将邮件转发到同一地址(因此收件人将是 me),代码会下载附件。

如果您能提供帮助,我将不胜感激。

我发现附件在 child MessagePart 中。所以我写了递归的方法来循环遍历所有的Parts来获取所有的附件。

    // List<FileInfo> Files = new List<FileInfo>();
    // client is created outside this method
    private void GetAttachmentsFromParts(IList<MessagePart> parts, string messageId)
    {
        if (parts == null) return;

        foreach (MessagePart part in parts)
        {
            if (!String.IsNullOrEmpty(part.Filename))
            {
                String attId = part.Body?.AttachmentId ?? null;
                if(String.IsNullOrWhiteSpace(attId)) continue;

                MessagePartBody attachPart = GmailServiceClient.Users.Messages.Attachments.Get("me", messageId, attId).Execute();

                // Converting from RFC 4648 base64 to base64url encoding
                // see http://en.wikipedia.org/wiki/Base64#Implementations_and_history
                String attachData = attachPart.Data.Replace('-', '+');
                attachData = attachData.Replace('_', '/');

                byte[] data = Convert.FromBase64String(attachData);
                var file = new FileInfo(part.Filename);
                Files.Add(file);
                File.WriteAllBytes(file.FullName, data);
            }

            if((part.Parts?.Count ?? 0) > 0)
                GetAttachmentsFromParts(part.Parts, messageId);
        }
    }

所有附件将存储在List<FileInfo> Files