How to Send Emails in C# via SMTP
By FoxLearn 1/9/2025 4:47:07 AM 103
Email is an effective way to communicate with users, and many applications need email-sending functionality. Developers can incorporate this functionality into their applications using various methods, with SMTP (Simple Mail Transfer Protocol) being a primary approach for sending emails in C#.
Sending Emails in C# With SMTP?
To send emails in C# applications, developers typically use an SMTP server, but C# alone cannot interact with SMTP servers. The .Net framework is required for this, as it provides the necessary classes for sending emails to an SMTP server, which then delivers them to recipients.
To get started, developers need the following prerequisites:
- The namespaces
System.Net
andSystem.Net.Mail
. - A valid email account with an SMTP server (e.g., Gmail, Outlook, or Maileroo).
- A .Net development environment like Visual Studio.
- Basic knowledge of C#/.Net programming.
To start sending emails in C# using SMTP, first set up your project, either by using an existing C# console application or creating a new one. Ensure you have the necessary NuGet package, System.Net.Mail
, installed to work with emails.
Import Necessary Namespaces
The System.Net.Mail
namespace is used for email-related classes, and System.Net
handles network credentials.
using System; using System.Net; using System.Net.Mail;
Create a MailMessage Object
This class is used to construct the email. You set the sender, recipient, subject, and body of the message.
MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress("[email protected]"); mailMessage.To.Add("[email protected]"); mailMessage.Subject = "Subject"; mailMessage.Body = "This is a test email sent in C#";
Configure the SMTP Client
Use SmtpClient
to configure SMTP settings such as the server host, port, credentials, and SSL. Replace the example credentials with those of your SMTP server.
SmtpClient smtpClient = new SmtpClient(); smtpClient.Host = "smtp.maileroo.com"; smtpClient.Port = 587; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential("SenderEmail", "SenderPassword"); smtpClient.EnableSsl = true;
Sending Attachments
To send attachments, create an Attachment
object and add it to the MailMessage
using the Attachments.Add
method.
Attachment attachment = new Attachment("your file.txt"); message.Attachments.Add(attachment); smtpClient.Send(message);
Once that's done, you can begin sending emails using the SMTP protocol by writing the appropriate code.
using System; using System.Net; using System.Net.Mail; class Program { static void Main(string[] args) { // Define email parameters var senderEmail = "[email protected]"; var recipientEmail = "[email protected]"; var subject = "Hello world"; var body = "This is a test email sent using C#.Net"; var smtpHost = "smtp.maileroo.com"; var smtpPort = 587; var senderPassword = "SenderPassword"; // Create the mail message var mailMessage = new MailMessage(senderEmail, recipientEmail, subject, body); // Set up the SMTP client var smtpClient = new SmtpClient(smtpHost) { Port = smtpPort, UseDefaultCredentials = false, Credentials = new NetworkCredential(senderEmail, senderPassword), EnableSsl = true }; // Send the email and handle any exceptions try { smtpClient.Send(mailMessage); Console.WriteLine("Email Sent Successfully."); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } }
Configuring Email Settings in the Web.Config File
Configuring email settings in the web.config
or app.config
file allows you to separate configuration details from your code, making maintenance easier. When sending emails in C# .NET using SMTP, you can store SMTP server information, credentials, and other email-related settings in this file. This enables you to modify configurations without changing the code itself.
<system.net> <mailSettings> <smtp from="[email protected]"> <network SmtpServer="smtp.maileroo.com" SmtpPort="587" SmtpUserName="your_email_address" SmtpPassword="your_email_password" enableSsl="true" /> </smtp> </mailSettings> </system.net>
Make sure to replace SmtpServer
, SmtpPort
, SmtpUserName
, and SmtpPassword
with your actual SMTP server details.
Sending an Email Using the Stored Configuration
With the email settings stored in the web.config
file, you can send emails in your C# .NET code without hardcoding the sender's details, as shown below:
MailMessage message = new MailMessage(); message.To.Add(new MailAddress("[email protected]")); message.Subject = "Hello world"; message.Body = "This is a test email sent using C#.Net."; Attachment attachment = new Attachment("path_to_attachment_file.txt"); message.Attachments.Add(attachment); SmtpClient smtpClient = new SmtpClient(); smtpClient.Send(message);
Configuring email settings in the web.config
or app.config
file allows you to separate configuration details from your code, making maintenance easier. When sending emails in C# .NET using SMTP, you can store SMTP server information, credentials, and other email-related settings in this file. This enables you to modify configurations without changing the code itself.
Example Configuration:
<code><system.net> <mailSettings> <smtp from="[email protected]"> <network SmtpServer="smtp.maileroo.com" SmtpPort="587" SmtpUserName="your_email_address" SmtpPassword="your_email_password" enableSsl="true" /> </smtp> </mailSettings> </system.net> </code>
Make sure to replace SmtpServer
, SmtpPort
, SmtpUserName
, and SmtpPassword
with your actual SMTP server details.
Sending an Email Using the Stored Configuration:
With the email settings stored in the web.config
file, you can send emails in your C# .NET code without hardcoding the sender's details, as shown below:
<code>MailMessage message = new MailMessage(); message.To.Add(new MailAddress("[email protected]")); message.Subject = "Hello world"; message.Body = "This is a test email sent using C#.Net."; Attachment attachment = new Attachment("path_to_attachment_file.txt"); message.Attachments.Add(attachment); SmtpClient smtpClient = new SmtpClient(); smtpClient.Send(message); </code>
This approach makes your application more flexible and easier to maintain by externalizing email configuration.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#