The SmtpClient SendAsync method is a feature of C# programming language that allows users to send emails asynchronously through SMTP servers. This means that the email will be sent in the background while other app tasks can continue.
Example 1: The following code example shows how to send an email with the SmtpClient SendAsync method without attachments.
using System.Net.Mail;
public void SendEmailAsync(string toAddress, string subject, string body, string fromAddress)
{ MailMessage mailMessage = new MailMessage(fromAddress, toAddress, subject, body); SmtpClient client = new SmtpClient("smtp.gmail.com", 587); client.Credentials = new System.Net.NetworkCredential("[email protected]", "your_password_here"); client.EnableSsl = true; client.SendAsync(mailMessage, null); }
Example 2: Another example of using the SmtpClient SendAsync method is for sending emails with attachments.
{ MailMessage mailMessage = new MailMessage(fromAddress, toAddress, subject, body); SmtpClient client = new SmtpClient("smtp.gmail.com", 587); client.Credentials = new System.Net.NetworkCredential("[email protected]", "your_password_here"); client.EnableSsl = true;
Attachment attachment = new Attachment(filePath); mailMessage.Attachments.Add(attachment);
client.SendAsync(mailMessage, null); }
The above examples are part of the System.Net.Mail library.
C# (CSharp) SMTPClient.SendAsync - 2 examples found. These are the top rated real world C# (CSharp) examples of SMTPClient.SendAsync extracted from open source projects. You can rate examples to help us improve the quality of examples.