How to send emails in C# .NET using SMTP

SMTP stands for Simple Mail Transfer Protocol. It is a communication protocol for electronic mail transmission. Mail servers and other message transfer agents use SMTP to send and receive mail messages. The Key Features of SMTP are it is considered a reliable server for sending emails, and it delivers the email more easily and quickly as it is developed from a simple platform.

using System.Net;  
using System.Net.Mail;  
namespace CsharpCode_SendMail {  
    class Program {  
        static string smtpAddress = "";  
        static int portNumber = 587;  
        static bool enableSSL = true;  
        static string emailFromAddress = ""; //Sender Email Address  
        static string password = "password"; //Sender Password  
        static string emailToAddress = ""; //Receiver Email Address  
        static string subject = "Hello";  
        static string body = "Hello, This is Email sending test using gmail.";  
        static void Main(string[] args) {  
        public static void SendEmail() {  
            using(MailMessage mail = new MailMessage()) {  
                mail.From = new MailAddress(emailFromAddress);  
                mail.Subject = subject;  
                mail.Body = body;  
                mail.IsBodyHtml = true;  
                //mail.Attachments.Add(new Attachment("D:\\TestFile.txt"));  //--Uncomment this to send any attachment  
                using(SmtpClient smtp = new SmtpClient(smtpAddress, portNumber)) {  
                    smtp.Credentials = new NetworkCredential(emailFromAddress, password);  
                    smtp.EnableSsl = enableSSL;  

0 Comment's

Comment Form