Search code examples
asp.net-mvcmvcmailer

how to handle exceptions in MVCMailer in ASP.NET MVC application


I'm using MVCMailer for the first time. I have created the mailviewmodel, view and i was able to send the mail successfully.

_myMailer.SendMyMail(mailerModel).Send();

Now I want to catch an exception when email sending is failed. (When no internet connection is available) I tried to use

var client = new SmtpClientWrapper();
client.SendCompleted += (sender, e) =>
{
    if (e.Error != null || e.Cancelled)
    {
        // Handle Error
    }

    //Use e.UserState
};

which is in GitHub

but this didn't work for me. can anybody please tell me how to achieve this?

Immediate replies appriciated. Thanks


Solution

  • This example is for Asynchronous Email. You are sending synchronous.

    Edit

    You can try this:

    try
    {
        _myMailer.SendMyMail(mailerModel).Send();
    }
    catch (Exception e)
    {
       //do something
    }