Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send mail async not working [closed]

i am trying to send email async method , but no email sent

https://dotnetfiddle.net/oFVtN2

like image 676
user4515101 Avatar asked May 26 '26 11:05

user4515101


1 Answers

Because of your 'fire and forget', you are disposing of the mail message immediately; that may be affecting you. Also, you're not disposing of the mail client. You should put the entire operation into the "fire and forget", including the creation and disposal of SmtpClient. Something more like this:

FireAndForgetTask(async cancellationToken =>
{
                using(var smtp = new SmtpClient
                {
                    Host = "myhost",
                    Port = 587,
                    EnableSsl = false,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    Credentials = new NetworkCredential("myemail", "mypass"),
                    Timeout = 50000
                })
                {          
                    using (var message = new MailMessage("myemail", destMail)
                    {
                        Subject = subject,
                        Body = mailBody,
                        IsBodyHtml = html           
                    })
                    {
                       await smtp.SendMailAsync(message);
                    }
                }
}
like image 114
Mark Sowul Avatar answered May 30 '26 11:05

Mark Sowul