zoukankan      html  css  js  c++  java
  • 如何使用EWS获取邮件及其附件

                ExchangeServiceBinding esb = new ExchangeServiceBinding();
                esb.Credentials = new NetworkCredential(username, password, domain);
                esb.Url = url;
    
                ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
    
    
    
                FindItemType findItemRequest = new FindItemType();
                findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
    
    
                ItemResponseShapeType itemProperties = new ItemResponseShapeType();
    
                ////获取邮件地址
                PathToExtendedFieldType PidTagSenderSmtpAddress = new PathToExtendedFieldType();
                PidTagSenderSmtpAddress.PropertyTag = "0x5D01";
                PidTagSenderSmtpAddress.PropertyType = MapiPropertyTypeType.String;
    
    
                // Define which item properties are returned in the response
    
                itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
                findItemRequest.ItemShape = itemProperties;  // Add properties shape to request
    
                itemProperties.AdditionalProperties = new BasePathToElementType[1];
    
                //获取邮件地址的
                itemProperties.AdditionalProperties[0] = PidTagSenderSmtpAddress;
    
    
                // Identify which folders to search to find items
                DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
                folderIDArray[0] = new DistinguishedFolderIdType();
                folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
    
                // Add folders to request
                findItemRequest.ParentFolderIds = folderIDArray;
    
                //Create unread only restriction --------------------------
                RestrictionType restriction = new RestrictionType();
                IsEqualToType isEqualTo = new IsEqualToType();
                PathToUnindexedFieldType pathToFieldType = new PathToUnindexedFieldType();
                pathToFieldType.FieldURI = UnindexedFieldURIType.messageIsRead;
    
    
                FieldURIOrConstantType constantType = new FieldURIOrConstantType();
                ConstantValueType constantValueType = new ConstantValueType();
                constantValueType.Value = "0";
                constantType.Item = constantValueType;
                isEqualTo.Item = pathToFieldType;
                isEqualTo.FieldURIOrConstant = constantType;
                restriction.Item = isEqualTo;
                findItemRequest.Restriction = restriction;
    
    
    
                FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);
                FindItemResponseMessageType folder = (FindItemResponseMessageType)findItemResponse.ResponseMessages.Items[0];
                ArrayOfRealItemsType folderContents = new ArrayOfRealItemsType();
                folderContents = (ArrayOfRealItemsType)folder.RootFolder.Item;
                ItemType[] items = folderContents.Items;
    
                if (items == null)
                {
                    return;
                }
    
               
                ProcessEmail(items[i], basicTime);

    处理邮件

     DateTime createdate;
                int timezone = int.Parse(Helper.GetWebconfig("timezone"));  
                string username = Helper.GetWebconfig("username");
                string password = Helper.GetWebconfig("password");
                string domain = Helper.GetWebconfig("domain");
                string exchangewebservice = Helper.GetWebconfig("exchangewebservice");
                
              
              
    
    
                ExchangeServiceBinding esb = new ExchangeServiceBinding();
                esb.Credentials = new NetworkCredential(username, password, domain);
                esb.Url = exchangewebservice;
    
    
    
    
                string categoryname = "未分类";
                int categoryid = 0;
                string status = "unsolved";
                string priority = "medium";
    
                string title = "";
                string contents = "";
                string email = "";
             
    
           
    
                
               
             
                title = itemType.Subject;
    
    
    
             
               createdate = itemType.DateTimeReceived.AddHours(timezone);
                if (createdate < baseTime)
                {
                    return;
    
                }
    
    
    
    
                #region 附件路径定义
    
    
                string subpath = DateTime.Now.ToString("yyyy-MM");
                string path = Helper.GetWebconfig("helpdesk_webconfig").Replace("web.config", "uploads") + @"\" + subpath + @"\";
    
                string phypath = path;
    
                if (!System.IO.Directory.Exists(phypath))
                {
                    System.IO.Directory.CreateDirectory(phypath);
                }
    
                string filepre = DateTime.Now.ToString("yyyyMMddHHmmss_");
    
                #endregion
    
    
    
    
                #region 获取邮件的内容
                
               
                 GetItemType g = new GetItemType();  
                g.ItemShape = new ItemResponseShapeType();  
                g.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
                g.ItemIds = new BaseItemIdType[] { itemType.ItemId };  
                GetItemResponseType p_mailResponse = esb.GetItem(g);  
                ArrayOfResponseMessagesType arrMail = p_mailResponse.ResponseMessages;  
                ResponseMessageType[] responseMessages = arrMail.Items;
    
                    
             
    
                foreach (ResponseMessageType respmsg in responseMessages)
                {
                     #region 邮件内容
                    if (respmsg is ItemInfoResponseMessageType)
                    {
                        ItemInfoResponseMessageType createItemResp = (respmsg as ItemInfoResponseMessageType);
                        ArrayOfRealItemsType aorit = createItemResp.Items;
                        foreach (MessageType myMessage in aorit.Items)
                        {
    
                            if (myMessage.Body.BodyType1 == BodyTypeType.Text)
                            {
                                contents = myMessage.Body.Value.Replace(Environment.NewLine, "<br />");
                            }
                            else
                            {
                                contents = myMessage.Body.Value;
                            }
    
                            if (myMessage.From != null)
                            {
                                email = myMessage.From.Item.EmailAddress;
                            }
    
                        }
                    }
    
                    #endregion
    
    
    
                    #region 邮件附件
                    if (itemType.HasAttachments)
                    {
                        ItemInfoResponseMessageType res = (ItemInfoResponseMessageType)respmsg;
                        if (res != null)
                        {
                            if (res.ResponseClass == ResponseClassType.Success && res.Items.Items != null && res.Items.Items.Length > 0)
                            {
                                ItemType inboxItem2 = res.Items.Items[0];
    
                                int m = 0;
                                string pname = "";
                                string vname = "";
                                string ext = "";
                                string url = Helper.GetWebconfig("helpdesk_url");
                                string app = Helper.GetApp(url);
                                string absurl = "";
    
    
                                #region 保存附件
    
    
                                AttachmentType[] attachments = inboxItem2.Attachments;
                                for (int attachmentCount = 0; attachmentCount < attachments.Length; attachmentCount++)
                                {
    
                                    GetAttachmentType getAttachmentRequest = new GetAttachmentType();
                                    RequestAttachmentIdType[] attachmentIDArray = new RequestAttachmentIdType[1];
                                    attachmentIDArray[0] = new RequestAttachmentIdType();
                                    attachmentIDArray[0].Id = attachments[attachmentCount].AttachmentId.Id;
                                    getAttachmentRequest.AttachmentIds = attachmentIDArray;
    
                                    GetAttachmentResponseType getAttachmentResponse = esb.GetAttachment(getAttachmentRequest);
    
                                    AttachmentInfoResponseMessageType attachmentResponseMessage = getAttachmentResponse.ResponseMessages.Items[0] as AttachmentInfoResponseMessageType;
                                    if (attachmentResponseMessage != null)
                                    {
                                        if (attachmentResponseMessage.ResponseClass == ResponseClassType.Success && attachmentResponseMessage.Attachments != null && attachmentResponseMessage.Attachments.Length > 0)
                                        {
                                            AttachmentType attachment = attachmentResponseMessage.Attachments[0];
                                            if (attachment is FileAttachmentType)
                                            {
                                                FileAttachmentType fat = (FileAttachmentType)attachment;
                                                ext = Helper.GetExt(fat.Name);
                                                pname = phypath + filepre + m + ext;
                                                vname = url + "uploads/" + subpath + "/" + filepre + m + ext;
                                                absurl = url + "uploads/" + subpath + "/" + filepre + m + ext;
                                                File.WriteAllBytes(pname, fat.Content);
                                                contents = contents.Replace("cid:" + attachment.ContentId, vname);
    
                                                m++;
                                                contents += " <br>附件" + (m + 1) + ": <a href=" + absurl + ">" + absurl + "</a><br>";
    
    
                                            }
                                        }
                                    }
                                }
    
    
    
                                #endregion
    
    
    
                            }
                        }
    
    
                    }
                    #endregion
                
                }
                    
    
    
                
                
                
              
    
                 
                #endregion
  • 相关阅读:
    LeetCode第242题:有效的字母异位词
    commons lang组件介绍和学习
    java中如何将string 转化成long
    java 字符串按小数点分割
    界面优化处理技术之(一)按钮组件优化处理
    系统登陆界面开发及实现之(五)界面版权组件设置
    系统登陆界面开发及实现之(四)界面登录框组件设置
    系统登陆界面开发及实现之(三)界面标题组件设置
    系统登陆界面开发及实现之(二)添加界面背景图片
    转发:base64引起的血案
  • 原文地址:https://www.cnblogs.com/mqingqing123/p/2878479.html
Copyright © 2011-2022 走看看