Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 861
Jasper Avatar asked Feb 22 '26 23:02

Jasper


1 Answers

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#?

like image 80
Wouter van Vegchel Avatar answered Feb 24 '26 11:02

Wouter van Vegchel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!