SoFunction
Updated on 2025-03-07

C# implements the method of sending emails asynchronously

This article describes the method of C# to implement asynchronous sending of emails. Share it for your reference. The details are as follows:

The following code can implement asynchronous sending of emails. After the email is sent out, the callback function will be automatically called, so that the program will not be stuck when sending the email.

MailMessage m = new MailMessage
  ("item@",
  "raja@",
  "This is the subject for the authorized email.",
  "This is the body of the authorized mail!...");
// Send the message using authorization
SmtpClient client = new SmtpClient("smtp.");
 = new NetworkCredential("user", "password");
 = true;
// Add the event handler
 += new SendCompletedEventHandler(mail_SendCompleted);
// Send the message asynchronously
(m, null);
// To Cancel the send
//();
void mail_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
  if ()
    ("Message cancelled");
  else if ( != null)
    ("Error: " + ());
  else
    ("Message sent");
}

I hope this article will be helpful to everyone's C# programming.