Tuesday, May 7, 2013

How To- Send mail in ASP.Net using C#, VB.Net

Send Mail in C#
This post is basically for freshers or newbies. In application development sometimes we need to send the mail through our application. Here I am explaining how you can send mail in ASP.Net using C#, VB.Net.

In my previous posts, I explained Convert DataTable into List, Constructor Chainning in C#, Convert a Generic List to a Datatable, Get Property Names using Reflection in C#Hard drive information using C#Create Directory/Folder using C#Check Internet Connection using C#SQL Server Database BackUp using C# and some other articles related to C#ASP.Net jQuery, Java Script and SQL Server.

The Microsoft .NET framework provides two namespaces, System.Net and System.Net.Sockets for managed implementation of Internet protocols that applications can use to send or receive data over the Internet.We use the SMTP protocol for sending mail in  C# .

Let's see a sample code.The following C# source code shows how to send an email from a Gmail address using SMTP server. The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 and also using NetworkCredential for password based authentication. Before write the mail sending code add the following name space in your .CS or .VB code file.
C# Code

using System.Net.Mail;
Now write the following C# method for sending the mail-
public static void SendMail(string _fromaddress, string[] _toaddress, string[] _ccadress,string _subject, string _body)
{
  try
  {
     //Create instance of MailMessage Class
     MailMessage msg = new MailMessage();
     msg.From = new MailAddress(_fromaddress);
     foreach(string _address in _toaddress)
     {
        msg.To.Add(_address);
     }
     foreach(string _address in _ccadress)
     {
        msg.CC.Add(_address);
     }
     msg.Subject = _subject;
     msg.Body = _body;
     //for sending body as HTML
     msg.IsBodyHtml = true;

     //Create Instance of SMTP Class
     SmtpClient SmtpServer = new SmtpClient();
     //Assign Host
     SmtpServer.Host = "smtp.gmail.com";
     //Assign Post Number
     SmtpServer.Port = 587;
     //Setting the credential for authentiicate the sender
     SmtpServer.Credentials = new System.Net.NetworkCredential("mymail@gmail.com", "mypass");
     //Enable teh Secure Soket Layer to Encrypte the connection 
     SmtpServer.EnableSsl = true;
     //Sending the message
     SmtpServer.Send(msg);
     Console.WriteLine("Mail Send Successfully!!");
     Console.ReadLine();
  }
  catch (SmtpException ex)
  {
                Console.WriteLine(ex.Message);
  }
}
VB.Net Code

Imports System.Net.Mail
Now write the following VB.Net method for sending the mail-
Public Shared Sub SendMail(_fromaddress As String, _toaddress As String(), _ccadress As String(), _subject As String, _body As String)
Try
 'Create instance of MailMessage Class
 Dim msg As New MailMessage()
 msg.From = New MailAddress(_fromaddress)
 For Each _address As String In _toaddress
  msg.[To].Add(_address)
 Next
 For Each _address As String In _ccadress
  msg.CC.Add(_address)
 Next
 msg.Subject = _subject
 msg.Body = _body
 'for sending body as HTML
 msg.IsBodyHtml = True

 'Create Instance of SMTP Class
 Dim SmtpServer As New SmtpClient()
 'Assign Host
 SmtpServer.Host = "smtp.gmail.com"
 'Assign Post Number
 SmtpServer.Port = 587
 'Setting the credential for authentiicate the sender
 SmtpServer.Credentials = New System.Net.NetworkCredential("mandeep.mca2007@gmail.com", "m@n!$hdub3y")
 'Enable teh Secure Soket Layer to Encrypte the connection 
 SmtpServer.EnableSsl = True
 'Sending the message
 SmtpServer.Send(msg)
 Console.WriteLine("Mail Send Successfully!!")
 Console.ReadLine()
Catch ex As SmtpException
 Console.WriteLine(ex.Message)
End Try
End Sub
I hope this will be helpful for you. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.

No comments:

Post a Comment

If You Enjoyed This Post Please Take 5 Seconds To Share It.

^ Scroll to Top hgpromo