亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Exchange C# 上檢測發送電子郵件失敗

如何在 Exchange C# 上檢測發送電子郵件失敗

C#
富國滬深 2021-12-25 18:43:40
我試圖檢測我的應用程序發送的電子郵件是否發送失敗。這意味著我不是要驗證電子郵件,而實際上只是驗證它是否到達了目標郵箱。這是我正在使用的 .Net Framework 應用程序代碼:    public void Send(EmailRequest emailRequest)    {        // Creates the message itselft        EmailMessage message = new EmailMessage(ServiceExchange);        message.Body = new MessageBody(emailRequest.Message);        message.Subject = emailRequest.Subject;        message.ToRecipients.Add(emailRequest.To);        // Create a custom extended property and add it to the message.         Guid myPropertySetId = Guid.NewGuid();        ExtendedPropertyDefinition myExtendedPropertyDefinition = new ExtendedPropertyDefinition(myPropertySetId, "MyExtendedPropertyName", MapiPropertyType.String);        message.SetExtendedProperty(myExtendedPropertyDefinition, "MyExtendedPropertyValue");        // Asynchronously, call        message.SendAndSaveCopy();        // Wait one second (while EWS sends and saves the message).         System.Threading.Thread.Sleep(1000);        // Now, find the saved copy of the message by using the custom extended property.         ItemView view = new ItemView(5);        SearchFilter searchFilter = new SearchFilter.IsEqualTo(myExtendedPropertyDefinition, "MyExtendedPropertyValue");        view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, myExtendedPropertyDefinition);        FindItemsResults<Item> findResults = ServiceExchange.FindItems(WellKnownFolderName.SentItems, searchFilter, view);        if (findResults == null) throw new Exception("Could not find results - findResults is null");        if (findResults.Items == null) throw new Exception("Could not find results - findResults.Items is null");        if (findResults.Items.Count == 0) throw new Exception("Could not find results - findResults.Items is 0");        if (findResults.Items.Count > 1) throw new Exception("findResults items returned more than one result");        ItemId itemID = null;    }   例如:如果我將其發送到有效的電子郵件,一切都很好。這里的目標是檢測故障,以便我們可以聯系業務并檢查為什么我們存儲了錯誤的電子郵件地址。
查看完整描述

2 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

如果您存檔的電子郵件地址無效,則 SendAndSaveCopy() 應該拋出一個異常說明。你可以實現一個 try/catch 語句來檢測任何失敗,并將失敗發送到一個記錄器,它會給你一個文本列表來處理:


public void Send(EmailRequest emailRequest)

{

    try

    {

        // Creates the message itselft

        EmailMessage message = new EmailMessage(ServiceExchange);

        message.Body = new MessageBody(emailRequest.Message);

        message.Subject = emailRequest.Subject;

        message.ToRecipients.Add(emailRequest.To);


        // Create a custom extended property and add it to the message. 

        Guid myPropertySetId = Guid.NewGuid();

        ExtendedPropertyDefinition myExtendedPropertyDefinition = new ExtendedPropertyDefinition(myPropertySetId, "MyExtendedPropertyName", MapiPropertyType.String);

        message.SetExtendedProperty(myExtendedPropertyDefinition, "MyExtendedPropertyValue");


        // Asynchronously, call

        message.SendAndSaveCopy();


        // Wait one second (while EWS sends and saves the message). 

        System.Threading.Thread.Sleep(1000);

    }

    catch (Exception x)

    {

        logger.LogError(x.Message);


    }

}

您可以在此處找到其他示例。


查看完整回答
反對 回復 2021-12-25
?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

我試圖檢測我的應用程序發送的電子郵件是否發送失敗。這意味著我不是要驗證電子郵件,而實際上只是驗證它是否到達了目標郵箱。

一旦您將消息交給 Exchange,它甚至可能不會立即發出。一旦 Exchange 嘗試發送它,它可能會發現遠程服務器不可用并稍后重試。

最終,Exchange 要么讓遠程服務器接受消息,要么放棄。

如果遠程服務器接受該消息,并且您在消息中配置了該標志,則您可以獲得發送回執。然而這并不能意味著該郵件確實送達收件人或收件人閱讀; 這僅意味著 Exchange 能夠將其提供給目標服務器。

許多服務器只是簡單地接受和丟棄發送給不存在的用戶的郵件。其他人將它們扔到全局“垃圾郵件”文件夾中。

如果您在郵件中設置了正確的“回復”地址,并且在 Exchange 管理員設置的超時時間內無法將郵件傳送到遠程服務器,您將收到來自 Exchange 的電子郵件。這通常是幾天。

但是,即使您設置了“已讀回執”標志,收件人也完全有可能閱讀郵件而不發送郵件。遵守是自愿的。

真正非常簡短的回答是“您可以檢測到即時故障,但實際上無法確定消息是否已被讀取或傳送”,并且無論如何都不會在您的代碼中發生。

如果有幫助,有一些錯誤可以立即檢測到,這些錯誤會顯示在您的 try/catch 塊中,但這些是例外而不是規則。


查看完整回答
反對 回復 2021-12-25
  • 2 回答
  • 0 關注
  • 211 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號