为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
C#

摘要

场景:

用户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的原因。猜测,通过这种方式截获了接收人列表。

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

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消