• Exchange Web Service 获取邮件的附件并保存到本地的示例代码


     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);
                                }
                            }
                        }
                    }
                }
    
            }
  • 相关阅读:
    openstack-1基础环境准备
    ELK补充之Filebeat
    ELK补充之logstash
    ELK
    dubbo
    zokeeper+kafka
    rabbitmq
    jenkins补充-编写自动化脚本实现devops 流水线和回滚等操作
    sonar
    python连接数据库之(连接MySQL)
  • 原文地址:https://www.cnblogs.com/xinghuayang/p/3288833.html
Copyright © 2020-2023  润新知