Powered by Blogger.

Saturday, March 1, 2014

Send Email in Asp.net C#.Net



In this article we will discuss about how we can send email in asp.net. Also we will discuss how to send email using gmail in asp.net.

You can also check my previous articles on:

- How to make scaling animation effect using AnimationExtender Control in Asp.Net?

- User Defined Function example in SQL Server 2008

- Tutorial on Tuple in C#.Net 4.0

Asp.Net provides System.Net.Mail namespace to send email in asp.net. So first add the namespace to the page.

using System.Net.Mail

C#.Net code:

MailMessage message = new MailMessage();
message.From = new MailAddress(fromAddress@aspdotnethelp.com, "AspDotNetHelp.com");
message.To.Add(new MailAddress(toAddress@aspdotnethelp.com));
message.Subject = "This is mail Subject";
message.Body = "This is the email body !!!";
message.Priority = MailPriority.Normal;

SmtpClient emailClient = new SmtpClient();
emailClient.Host = "smtp host address";
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential
("networkcredentialid@aspdotnethelp.com", "password");
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = SMTPUserInfo;
message.IsBodyHtml = true;
emailClient.Send(message);

Below is the code to send email using Gmail:

MailMessage message = new MailMessage();
message.From = new MailAddress(Your gamil id, "Friendly disaplay name");
message.To.Add(new MailAddress(toAddress@aspdotnethelp.com));
message.Subject = "This is mail Subject";
message.Body = "This is the email body !!!";
message.Priority = MailPriority.Normal;

SmtpClient emailClient = new SmtpClient();
emailClient.Host = "smtp.gmail.com";
emailClient.Port = 587;
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("your gmail address", "your gmail password");
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = SMTPUserInfo;
message.IsBodyHtml = true;
emailClient.Send(message);

Hope this will work !





0 comments

Post a Comment