Home » Asp.net

Send Email System.Net.Mail , SMTP server – Asp.Net

29 August 2009 One Comment Visited 82 times, 5 so far today
Send Email through SMTP server ( Gmail )

E-mail is one of the most common and reliable methods of communication for both personal and business purposes.

One of the big improvements in the configuration and maintenance of ASP.NET Web sites for version 2.0 is the ability to define common settings such as connection strings and mail server in a structured section of the web.config file

C#.net Coding Part

First add below mentioned namespace in code

using System.Net.Mail;

Now write this code in click event of button

protected void btn_OnClick(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
mail.IsBodyHtml = true;
mail.To.Add(txtEmail.Text);
mail.Subject = txtSubject.Text;
mail.Body = txtbody.Text;

// mail.Attachments.Add(new Attachment(Server.MapPath(”~/C:\1236.JPG”)));
// mail.Priority = MailPriority.High;

SmtpClient client = new SmtpClient();
client.EnableSsl = true;   // For Gmail SSL must True
client.Send(mail);
}
catch (Exception exc)
{
Response.Write(exc.Message);
}
}



Web.config Setting

configuration>
appSettings/>
connectionStrings/>
system.web>
compilation debug=”true“>
compilation>
authentication mode=”Windows“/>
system.web>
system.net>
mailSettings>
smtp from=”[email protected]“>
network host=”smtp.gmail.netport=”587” userName =”[email protected]
password=”passworddefaultCredentials=”false” />
smtp>
mailSettings>
system.net>
configuration>

One Comment »

  • Niraj said:

    Very helpful….
    Thanks for this!

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.