如何从发送到Google Group邮件地址的电子邮件中下载附件 [英] How to download attachment from email sent to google group mail address

查看:305
本文介绍了如何从发送到Google Group邮件地址的电子邮件中下载附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的电子邮件a@company.com在Google组中,该组的电子邮件地址为group@company.com.我们有一个g套件服务帐户,该帐户已启用Google Apps域范围内的委派. 我们有从b@companyb.com发送到组电子邮件group@company.com的电子邮件,主题是Report from company b并将报告附加在电子邮件中.

Let's say my email a@company.com is in the google group which has email address group@company.com. We have a g suite service account which has enabled Google Apps Domain-wide Delegation. We have emails send from b@companyb.com to our group email group@company.com, have subject Report from company b and attach the report in the email.

问题在于gmail api能够列出所有邮件,但不能列出每封电子邮件中的附件.

The issue is that the gmail api is able to list all messages but not able to list the attachments in each email.

有什么办法吗?

这是我的代码:

    // 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),则代码将下载附件.

If I forward the mail manually to the same address (so the receiver will be me), the code downloads the attachment.

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

I'd appreciate if you can help.

推荐答案

我发现附件位于子MessagePart中.因此,我编写了递归方法来遍历所有部件以获取所有附件.

I've found the attachments are in the child MessagePart. So I wrote the recursive method to loop through all Parts to get all attachments.

    // 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

这篇关于如何从发送到Google Group邮件地址的电子邮件中下载附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆