private static void DownLoadMailAttachments(ExchangeService service, ItemId itemId)
        {
            EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments)); ;

            if (message.HasAttachments)
            {
                foreach (MailAttachment attachment in message.Attachments)
                {
                    FileAttachment fileAttachment = attachment as FileAttachment;

                    ItemAttachment itemAttachment = attachment as ItemAttachment;

                    if (itemAttachment != null)
                    {
                        itemAttachment.Load(EmailMessageSchema.MimeContent);

                        char[] invalidChars = Path.GetInvalidPathChars();
                        string name = itemAttachment.Name;

                        foreach (char invalidChar in invalidChars)
                        {
                            name = name.Replace(invalidChar, ' ');
                        }

                        name = name.Replace(":", " ");

                        string emailPath = Path.Combine(Path.GetTempPath(), name + ".eml");

                        using (FileStream stream = File.Open(emailPath, FileMode.Create, FileAccess.ReadWrite))
                        {
                            foreach (byte content in itemAttachment.Item.MimeContent.Content)
                            {
                                stream.WriteByte(content);
                            }
                        }

                      
                    }

                    if (fileAttachment != null)
                    {
                        string filePath = Path.Combine(Path.GetTempPath(), fileAttachment.Name);

                        fileAttachment.Load();

                        using (FileStream stream = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite))
                        {
                            foreach (byte content in fileAttachment.Content)
                            {
                                stream.WriteByte(content);
                            }
                        }
                    }
                }
            }

        }

 

相关文章:

  • 2022-02-11
  • 2021-11-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-03
  • 2021-11-27
  • 2021-11-21
猜你喜欢
  • 2021-12-22
  • 2021-09-16
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2021-12-14
  • 2022-12-23
相关资源
相似解决方案