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

Posted In Asp.net - By admin On Saturday, August 29th, 2009 With 1 Comment
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=”mymail@gmail.com“>
<network host=”smtp.gmail.netport=”587” userName =”mymail@gmail.com
password=”passworddefaultCredentials=”false” />
</smtp>
</mailSettings>
</system.net>
</configuration>

About -

Displaying 1 Comments
Have Your Say

  1. Niraj says:

    Very helpful….
    Thanks for this!

    [Reply]

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>