Jasper
Jasper

Reputation: 49

Send mail with smtp SendAsync

this is how when I need to send email gives me error. But the mistake that since gives me is this:

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.

I have accumulated since the MVC and have used class to keep track of ie areas of the page. The reason I have used SendAsync is precisely that it goes a little faster to send email, etc..

This error only happens when I try to send email to users.

public static void NewPassword(string mail, string name, string password)
    {
        MailDefinition oMailDefinition = new MailDefinition();
        oMailDefinition.BodyFileName = "~/MailList/emailskabelon/NewPassword.html";
        oMailDefinition.From = FromMail;

        Dictionary<string, string> oReplacements = new Dictionary<string, string>();
        oReplacements.Add("<<navn>>", name);
        oReplacements.Add("<<password>>", password);

        System.Net.Mail.MailMessage oMailMessage = oMailDefinition.CreateMailMessage(mail, oReplacements, new LiteralControl());
        oMailMessage.Subject = NewpasswordTitle + WebsiteName;
        oMailMessage.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient(AzureApi);
        System.Net.NetworkCredential netcred = new System.Net.NetworkCredential(AzureName, AzurePassword);
        smtp.UseDefaultCredentials = false;
        smtp.EnableSsl = true;

        smtp.Credentials = netcred;
        smtp.Port = Convert.ToInt32("25");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

        using (var smtpClient = new SmtpClient())
        {
            smtp.SendAsync(oMailMessage, null);
        }
    }

I've tried to do like this:

public static async NewPassword(string mail, string name, string password)
        {
            ....
            using (var smtpClient = new SmtpClient())
            {
                await smtp.SendAsync(oMailMessage, null);
            }

i have see here: https://stackoverflow.com/a/35212320/7391454

Upvotes: 2

Views: 10074

Answers (3)

Ε Г И І И О
Ε Г И І И О

Reputation: 12321

Not really replying your original question, but just wanted to stress that you are better off calling an email sending code without keeping the calling thread waiting. Although you are using async/await, in user's perspective, you are still waiting in the browser while the server is done sending emails. It may be few milliseconds, but still it's better to let this handled by a background worker.

So IMO, using HostingEnvironment.QueueBackgroundWorkItem(x=> SendEmail()); would be a better approach.

Having said that, you still have a slight risk of the asynchronous task being terminated if app domain recycles in the middle. But that's highly unlikely in your case I would say. Even if that happens, you can use a cancellation token and work your way around it.

Upvotes: 0

Wouter van Vegchel
Wouter van Vegchel

Reputation: 469

Change your method to:

public async Task SendEmail(string toEmailAddress, string emailSubject, string emailMessage)
{
var message = new MailMessage();
message.To.Add(toEmailAddress);

message.Subject = emailSubject;
message.Body = emailMessage;

using (var smtpClient = new SmtpClient())
{
    await smtpClient.SendMailAsync(message);
}
}

And call it like:

var task = SendEmail(toEmailAddress, emailSubject, emailMessage);
var result = task.WaitAndUnwrapException();

Have a look here Asynchronously sending Emails in C#? and here How to call asynchronous method from synchronous method in C#?

Upvotes: 10

Pavvy
Pavvy

Reputation: 348

You can also try to define your async option inside a separate thread. I believe that you already have inserted the async tag in your page. And if everything is okay then try to put your code in below block.

 this.Page.RegisterAsyncTask(new PageAsyncTask(async ctoken => {
      var result = await SomeOperationAsync(ctoken);
      // result operations.
}));

Upvotes: 0

Related Questions