Thursday, 28 March 2013

Send Email from your GMAIL account using ASP.Net and C#:

Send Email from your GMAIL account using ASP.Net and C#:

 This article is provide a demo for send Emails  from your GMAIL account using ASP.Net and C#:
First create an aspx file named "SendGmail.aspx" with required input controls and action buttons. Please refer the below screen shot.


Give the id for from textbox as "txtFrom" and "txtTo", "txtSubject", "txtMessage"  respectively.
Then add the following code into the "Send Email" button's OnClick method in thecode behind. Before appropriate name space (if you need). 

Code:
protected void btnSendEmail_Click(object sender, EventArgs e)
    {
        
//Create Mail Message Object with content that you want to send with mail.
        
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(txtFromMail.Text, txtToMail.Text, txtSubject.Text, txtMessage.Text);
        
mailMessage.IsBodyHtml = false;
        
//Proper Authentication Details need to be passed when sending email from gmail
        
System.Net.NetworkCredential mailAuthentication = new
        
System.Net.NetworkCredential("YourGmailID""YourGmailPassword");
        
//Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
        //For different server like yahoo this details changes and you can
        //get it from respective server.
        
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com"587);
        
//Enable SSL
        
mailClient.EnableSsl = true;
        
mailClient.UseDefaultCredentials = false;
        
mailClient.Credentials mailAuthentication;
        
mailClient.Send(mailMessage);
    
}
You can download full code for send email using csharp from here

No comments:

Post a Comment