【helpdesk】启明星helpdesk7.0版本里,实现邮件提交功能介绍和原理

 

在启明星helpdesk7.0版本里,新增了一个功能:邮件提交。所谓邮件提交就是用户直接发送邮件到IT。当然IT通常会公开一个公共邮件,例如support@dotnetcms.org.下面介绍一下启明星Helpdesk邮件提交的功能介绍,稍后介绍实现原理。

1.用户通过邮件发送邮件到 test@dotnetcms.org 提交问题。

 

2.系统接收到邮件后,将会自动发出一封回复。回复的时间间隔最长10分钟。邮件会提醒用户您的邮件已经收到。用户可以直接回复邮件点击链接查看问题或补充。

 

3.管理员/用户也可以通过helpdesk直接回复,这些回复会以邮件的形式告诉给用户。

4.用户也可以通过helpdesk查看提交的问题。

 

 

技术实现:

在整个邮件提交过程中,核心部分是邮件的接收和发送。目前几乎所有邮件都支持SMTP/POP协议。所有使用这个协议就可以获取或者发送邮件。

另外,在我们实现的过程中,部分企业使用的是Exchange2007.对于微软这个邮件服务器,许多企业默认是不启用SMTP/POP协议,微软使用的Exchange

web service(EWS)接口发送或者接收邮件,所以我们提供了两种方式:SMTP/POP方式和EWS方式。

(注:对于Exchange2003,微软还支持webdav方式,但是在exchange2007不推荐使用该方式。所以我们没有再考虑exchange2003情况,如果您使用的是exchange2003,可以开启SMTP/POP协议)

SMTP发送邮件核心代码:

 

                SmtpClient smtpClient = new SmtpClient();
                MailMessage message = new MailMessage();
                MailAddress fromAddress = new MailAddress(mail, friendlyname);
                smtpClient.Host = smtp;
                smtpClient.Port = int.Parse(smtp_port);
                NetworkCredential nc = new NetworkCredential(username, password);
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = nc;
                smtpClient.EnableSsl = smtp_use_ssl;
                message.From = fromAddress;

                if (email != "")
                {
                    message.To.Add(_email);
                }
                if (_sendto != "")
                {
                    message.CC.Add(_sendto);
                }
                message.Subject = title;
                message.IsBodyHtml = true;
                message.Body = content;
                smtpClient.Send(message);

 

SMTP接收邮件

接收邮件使用了OpenPOP.dll

https://files.cnblogs.com/mqingqing123/OpenPop.rar

            using (Pop3Client client = new Pop3Client())
            {
                client.Connect(pop, int.Parse(pop_port), pop_use_ssl);
                client.Authenticate(username, password);
                int messageCount = client.GetMessageCount();
                List<OpenPop.Mime.Message> allMessages;

                if (messageCount > maxcount)
                {
                    allMessages = new List<OpenPop.Mime.Message>(maxcount);
                    Max = maxcount;
                }
                else
                {
                    allMessages = new List<OpenPop.Mime.Message>(messageCount);
                    Max = messageCount;
                }

 处理邮件的代码

public static void ProcessEmail(OpenPop.Mime.Message msg, DateTime baseTime)
        {

 
            email = msg.Headers.From.Address;
            createdate = msg.Headers.DateSent.AddHours(timezone);
            title = msg.Headers.Subject;
        MessagePart html = msg.FindFirstHtmlVersion();
            if (html != null)
            {
                contents = html.GetBodyAsText();
            }

}

 

 

 

采用EWS方式:

        public static void GetOutlookEmail()
        {


            DateTime basicTime = DateTime.Now.AddMinutes(-10);
            string username = Helper.GetWebconfig("username");
            string password = Helper.GetWebconfig("password");
            string domain = Helper.GetWebconfig("domain");
            string url = Helper.GetWebconfig("exchangewebservice");
            int timezone = int.Parse(Helper.GetWebconfig("timezone"));
            int maxcount = int.Parse(Helper.GetWebconfig("every_time_fetch_max_count"));
 




            ExchangeServiceBinding esb = new ExchangeServiceBinding();
            esb.Credentials = new NetworkCredential(username, password, domain);
            esb.Url = url;
           
 
             


           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;



           int count = items.Length;
           int Max = 0;
      

           if (count > maxcount)
           {

               Max=maxcount;
           }
           else
           {

               Max = count;
           }


           int c = 0;
           for (int i = Max; c < Max; i--)
           {
               try
               {
                   ProcessEmail(items[i-1], basicTime);
               }
               catch(Exception ex)
               {
                   WriteLog(ex.ToString());
               
               }
               c++;
           }
         
            
        
    }

 最后处理获取的邮件

            ////// 获取邮件的内容 //
        

             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)
            {

                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;
                        }

                    }
                }
            }

 采用EWS发送邮件的核心代码

            emailMessage.Subject = title; 
            emailMessage.Body = new BodyType(); 
            emailMessage.Body.BodyType1 = BodyTypeType.HTML; //specify HTML or plain 
            emailMessage.Body.Value = contents;

            CreateItemType emailToSave = new CreateItemType(); 
            emailToSave.Items = new NonEmptyArrayOfAllItemsType(); 
            emailToSave.Items.Items = new ItemType[1]; 
            emailToSave.Items.Items[0] = emailMessage; 
            emailToSave.MessageDisposition = MessageDispositionType.SendAndSaveCopy; 
            emailToSave.MessageDispositionSpecified = true;


            try
            {
                CreateItemResponseType response = esb.CreateItem(emailToSave);
                ResponseMessageType[] rmta = response.ResponseMessages.Items;
            }
            catch(Exception ex)
            {

                WriteLog(ex.ToString()); 
            }

 

posted @ 2013-01-14 09:11  启明星工作室  阅读(1178)  评论(0编辑  收藏  举报