Keywords: C# | email | System.Net.Mail
Abstract: This article provides an in-depth exploration of email sending in C# applications, focusing on the System.Net.Mail namespace. It explains the usage of MailMessage and SmtpClient classes, covering HTML email support, authentication setup, and bulk email handling, with reorganized code examples for practical implementation.
Introduction
In C# development, sending emails is a frequent requirement for functionalities such as notifications, reports, and mass communication. Unlike the limitations of older technologies like MAPI in VB 6, the .NET framework offers robust solutions through the System.Net.Mail namespace, supporting HTML emails and eliminating manual send steps.
Core Components: MailMessage and SmtpClient
The MailMessage class in the System.Net.Mail namespace is used to construct email content, including sender, recipient, subject, and body properties, while the SmtpClient class handles sending emails via an SMTP server. This separation ensures clear and maintainable email configuration and sending logic.
Email Sending Steps
The basic process for sending an email involves initializing SmtpClient, setting up authentication, creating a MailMessage instance with configured properties, and finally calling the Send method. For HTML emails, set the Body to an HTML string and the IsBodyHtml property to true.
Code Example: Sending an HTML Email
The following code, restructured from the best answer, demonstrates how to send an HTML-formatted email with error handling for robustness.
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
...
try
{
SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net");
mySmtpClient.UseDefaultCredentials = false;
NetworkCredential basicAuthenticationInfo = new NetworkCredential("username", "password");
mySmtpClient.Credentials = basicAuthenticationInfo;
MailAddress from = new MailAddress("test@example.com", "TestFromName");
MailAddress to = new MailAddress("test2@example.com", "TestToName");
MailMessage myMail = new MailMessage(from, to);
MailAddress replyTo = new MailAddress("reply@example.com");
myMail.ReplyToList.Add(replyTo);
myMail.Subject = "Test message";
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
myMail.Body = "<b>Test Mail</b><br>using <b>HTML</b>.";
myMail.BodyEncoding = System.Text.Encoding.UTF8;
myMail.IsBodyHtml = true;
mySmtpClient.Send(myMail);
}
catch (SmtpException ex)
{
throw new ApplicationException("SmtpException has occurred: " + ex.Message);
}
catch (Exception ex)
{
throw ex;
}In this code, the Body property value is an HTML string, where <b> and <br> tags are escaped as text content to prevent HTML parsing errors.
Bulk Email Sending Strategies
For scenarios involving 100-200 emails, it is efficient to reuse the SmtpClient instance and create multiple MailMessage objects in a loop. Consider asynchronous sending or threading to avoid application blocking, and implement retry mechanisms and error logging.
Error Handling and Best Practices
Always wrap email sending code in try-catch blocks to catch exceptions like SmtpException, ensuring application stability. Additionally, properly configure SMTP servers and authentication, and adhere to anti-spam policies to enhance email delivery success.