0

That's how a few days ago I could send email to the customers. But right now, it comes out with this mistake.

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 done this by sending mail to the customers. I have also looked at others who have been wrong.

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

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

        MailMessage oMailMessage = oMailDefinition.CreateMailMessage(mail, oReplacements, new LiteralControl());
        oMailMessage.Subject = Password;
        oMailMessage.IsBodyHtml = true;

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

        smtp.Credentials = netcred;
        smtp.Port = Port25;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

        using (var smtpClient = new SmtpClient())
        {
            await Task.Delay(1500);
            await Task.Factory.StartNew(() => oMailMessage);
            await smtpClient.SendMailAsync(oMailMessage);
        }

        //smtp.Send(oMailMessage);
    }

What's wrong since it will not send emails to the customer? - That's my question here.

Jasper
  • 23
  • 5
  • 4
    **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.** - Your exception is talking to you, listen to it. – Federico Dipuma May 17 '17 at 16:36

2 Answers2

0

There are many different issues with your code, starting by the async void return type which is not supported on ASP.NET (and your exception is telling you that).

Also you are creating two different instances of SmtpClient, and using the wrong one for sending your email message.

Also, why are you even awaiting two totally useless tasks?

Here it is a fixed version:

public static async Task NewPassword(string mail, string name, string password)
{
    MailDefinition oMailDefinition = new MailDefinition();
    oMailDefinition.BodyFileName = "~/img/emailskabelon/NewPassword.html";
    oMailDefinition.From = OrdklarMail;

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

    MailMessage oMailMessage = oMailDefinition.CreateMailMessage(mail, oReplacements, new LiteralControl());
    oMailMessage.Subject = Password;
    oMailMessage.IsBodyHtml = true;

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

        smtp.Credentials = netcred;
        smtp.Port = Port25;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

        await smtp.SendMailAsync(oMailMessage);
    }
}

Also be aware that you need to declare your page as:

<%@ Page Async="true" %>

And call this method using await inside your page.

Federico Dipuma
  • 17,655
  • 4
  • 39
  • 56
  • So i need to add `<%@ Page Async="true" %>` on the site i send mail from? – Jasper May 17 '17 at 17:23
  • Please [read the documentation](https://learn.microsoft.com/en-us/aspnet/web-forms/overview/performance-and-caching/using-asynchronous-methods-in-aspnet-45). – Federico Dipuma May 17 '17 at 17:24
  • Thanks for help! Good day to u! – Jasper May 17 '17 at 17:27
  • I don't understand what problem you are having, but inside the documentation I posted you before there are all the details on how to use `async` methods inside ASP.NET Web Forms. If you have different problems then create a new question and explain here the details. – Federico Dipuma May 18 '17 at 10:55
0

Another issue could be that somewhere above that code you forgot to use await. I mean that if you have just started a task and moved forward with page execution. As soon as page request is served ASP.NET could mark this thread as finished and GarbageCollector could destroy it at any time after. This will lead that your task will just disappear. This time is not constant, so sometimes your background task may finish and some time not.

Artyom
  • 1,065
  • 8
  • 12