使用Gmail API检索嵌入式/嵌入式附件

时间:2019-01-04 21:15:08

标签: asp.net-web-api gmail-api inline-attachments

我正在尝试使用C#Web API上的Gmail API提取附件。我已经能够提取附件,但是无法提取电子邮件中的嵌入式附件(图像)。

用于提取附件的代码

foreach (Message msg in messages)
                {


                    if (msg.Payload != null && msg.Payload.Parts != null)
                    {
                        Console.WriteLine("History IDs " + msg.HistoryId);

                        IList<MessagePart> parts = msg.Payload.Parts;
                        foreach (MessagePart part in parts)
                        {


                            if (!String.IsNullOrEmpty(part.Filename))
                            {
                                Console.WriteLine(part.Body.AttachmentId);
                                String attId = part.Body.AttachmentId;
                                try
                                {
                                    MessagePartBody attachPart = service.Users.Messages.Attachments.Get(userId, msg.Id, 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);
                                    File.WriteAllBytes(Path.Combine(AppSettings.GetDocsRootDirectory(), part.Filename), data);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                }
                            } else
                            {

                            }
                        }
                    }


                }

还有,有没有办法像我们可以批量请求消息那样批量处理附件?

0 个答案:

没有答案