[c#]exchange回复,全部回复,转发所遇到的问题

摘要

场景:

用户B向A用户发送了一封邮件。

用户A答复邮件时,会默认将B作为接收人。

问题:

在用exchange的回复,全部回复,转发(Reply和Foward方法)邮件的时候,需求是用户可以删除用户B(转发除外),可以自定义接收人。但提供的Reply方法,发现用户B仍会收到答复的邮件。

解决办法

通过反编译查看了Reply和Forward的方法:

通过上面的图可以看到,这两个方法是通过ResponseMessage对象实现发送和添加额外的接收人的。

所以我们可以通过ResponseMessage对象来实现回复和转发,修改接收人信息。代码片段如下:

        /// <summary>
        /// exchange服务对象
        /// </summary>
        private static ExchangeService _exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        /// <summary>
        /// 回复转发操作
        /// </summary>
        /// <param name="email"></param>
        /// <param name="user"></param>
        /// <param name="type"></param>
        public static void Reply_Forword(Email email, UserInfoBase user, string type)
        {
            try
            {
                if (userInfo == null)
                {
                    throw new ArgumentNullException("当前用户信息为空,无法访问exchange服务器");
                }
                _exchangeService.Credentials = new NetworkCredential(userInfo.Name, userInfo.Pwd, userInfo.Doamin);
                _exchangeService.Url = new Uri(WebConfig.ExchangeServerUrl);//邮件内容
                EmailMessage message = EmailMessage.Bind(_exchangeService, new ItemId(email.Uniqueid));
                string[] strTos = email.Mail_to.Split(';');
                message.ToRecipients.Clear();
                message.CcRecipients.Clear();
                message.ReplyTo.Clear();
                message.BccRecipients.Clear();
                ResponseMessage responseMessage = null;
                //发送并且保存
                if (type == "reply")
                {
                    responseMessage = message.CreateReply(false);
                }
                else if (type == "all")
                {
                    responseMessage = message.CreateReply(true);
                }
                else if (type == "forword")
                {
                    responseMessage = message.CreateForward();
                }
                if (responseMessage != null)
                {
                    //接收人
                    foreach (var item in email.Mail_to.Split(';'))
                    {
                        responseMessage.ToRecipients.Add(item);
                    }
                    //抄送人
                    foreach (string item in email.Mail_cc.Split(';'))
                    {
                        responseMessage.CcRecipients.Add(item);
                    }
                    responseMessage.BodyPrefix = new MessageBody(email.body);
                    responseMessage.Subject = email.Subject;
                    responseMessage.SendAndSaveCopy();
                }
               
            }
            catch (Exception ex)
            {
                throw new Exception("发送邮件出错," + ex.Message + ":" + ex.StackTrace);
            }

        }

总结

在查找解决办法的时候,反编译了回复和转发方法的实现方式,发现内部是通过ResponseMessage对象来实现的,就没再去尝试。但通过同事帮忙,找到了MSDN一篇文章

https://msdn.microsoft.com/en-us/library/dd633704(v=exchg.80).aspx

在这篇文章中又提到了ResponseMessage对象。

添加额外的接收人。

最后通过这种方式,修改掉了B用户。

现在回头看看反编译的图,就明白了,在转发邮件的时候,是会修改掉用户B的,转发邮件并不需要默认的接收人,因为你转发的对象还未知。

这也印证了上面为什么ResponseMessage对象可以修改掉用户B的原因。猜测,通过这种方式截获了接收人列表。

最近发现,自己有点眼高手低了。这里记录问题的解决过程,警告自己一下。

原文地址:https://www.cnblogs.com/wolf-sun/p/5695922.html