Sending Email Using SMTP Client In .NET

Almost all applications that you build will require some sort of notification sending mechanism. Most of the times the notification is sent as an email to the user. In this post, you can learn about how you can quickly get a module to send Emails for applications that are developed in .NET platform.

The code is written in C# which uses a configured SMTP client to send emails.

Code

public void SendEmail(string toEmail, string subject, string messageBody)
 {
 System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); 
 message.Subject = subject;
 
 message.Body = messageBody;

message.To.Add(toEmail);

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
 smtp.Send(message); 
 }

This method takes receiver’s email, mail subject and message body as parameters and email is sent using the SMTP client.

The SMTP client is configured in web.config file as below:

<system.net>
 <mailSettings>
 <smtp from="[email protected]">
 <network host="mail.yourcompany.com" 
 password="yourpassword" 
 port="8889" 
 userName="[email protected]" 
 enableSsl="false" />
 </smtp>
 </mailSettings>
 </system.net>

 

  • Leave a Reply