如何在C#中处理来自Exchange Server的NDR电子邮件?

时间:2019-07-11 04:12:35

标签: c# asp.net .net exchange-server exchangewebservices

当前,我的应用程序具有处理传入电子邮件并退回并与代码中给定条件不匹配然后返回的功能。但是,我想将另一种类型的电子邮件添加到“处理”中,即来自Microsoft Exchange Server的NDR“未送达报告”。因此,我的应用程序不响应/反弹NDR到Exchange Server,这会导致我的邮箱和Exchange Server之间出现循环。 当Invalid没有特定的

时,以下方法触发
private static void ProcessInvidMsgWithoutNo(string sMsgFrom, string sFromEmail, EmailMsg sMsgReceived, EmailMessage message)
    {
        EmailMsg.MoveToInvalid(message);

        sMsgReceived.IsValid = false;
        SaveMsgReceived(sMsgReceived, 0, string.Empty);

        if (!sFromEmail.Equals(string.Empty))
        {
            ResponseForInvidMsg(sFromEmail);
        }
        else
        {
            curLog.WriteLog(string.Format(CultureInfo.CurrentCulture, MsgEMChecker27, sMsgFrom));
        }
    }

以下方法触发以响应传入的无效消息,如上所述。

private static void ResponseForInvidMsg(string sFromEmail)
    {
        string tErrSubjectMsg = String.Format(CultureInfo.CurrentCulture, "{0}\\Resource\\MsgErrorSubjectAck.html", Entity.GetSetting("DocRootDir"));

        StringBuilder htmlText = new StringBuilder();
        FileStream fsFile = new FileStream(tErrSubjectMsg, FileMode.Open);

        if (fsFile != null)
        {
            StreamReader reader = new StreamReader(fsFile, Encoding.Default);

            string text;
            do
            {
                text = reader.ReadLine();
                if ((text != null) && (text != ""))
                    htmlText.Append(text + "\n");
            } while (text != null);

            reader.Close();
            fsFile.Close();
            fsFile = null;
        }
        else
            htmlText.Append("hello");

        string tToCustomerSubject = ReplyForInvalid;
        string tMessage = htmlText.ToString();

        EmailMsg emTo = new EmailMsg(string.Empty, sFromEmail, tToCustomerSubject, tMessage);
        emTo.MsgType = EmailMsg.TypeSentCustomer;
        emTo.Send(false); //Not save but CC to generic email box
    }

请帮助我找到一种可以停止我的代码以响应Exchange Server NDR的方法。谢谢

1 个答案:

答案 0 :(得分:0)

开始的地方是检查消息的ItemClass,请参见https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-asemail/51d84da6-a2da-41e9-8ca7-eb6c4e72c28d。 NDR,交货报告等应带有Report前缀,例如

REPORT.IPM.NOTE.NDR Non-delivery report for a standard message.
REPORT.IPM.NOTE.DR Delivery receipt for a standard message.