Send Email using Gmail SMTP server - ASP.NET

Send Email using Gmail SMTP server-ASP.NET

What is an SMTP server?

(pronounced as separate letters) Short for Simple Mail Transfer Protocol, a protocol for sending e-mail messages between servers. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another; the messages can then be retrieved with an e-mail client using either POP or IMAP.
In addition, SMTP is generally used to send messages from a mail client to a mail server. This is why you need to specify both the POP or IMAP server and the SMTP server when you configure your e-mail application.
Let's see one example in ASP.NET 2.0 (Web Application)

In WebMailSend.aspx.cs file
You have to import this namespace for MailMessage.
using System.Net.Mail;
protected void btnSend_Click(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;

    // for attachment
    mail.Attachments.Add(new Attachment(Server.MapPath("~//Attachments//Ashvin-padhiyar-closeup.JPG")));
    mail.Priority = MailPriority.High;

    SmtpClient client = new SmtpClient();
    client.EnableSsl = true;
    client.Send(mail);
    Response.Write("Mail Sent...");
}
catch (Exception exc)
{
    Response.Write(exc.Message);
}
}

In web.config file
<configuration>
   <appSettings/>
   <connectionStrings/>
   <system.web>
       <compilation debug="true">
           <assemblies>
               <add assembly="Accessibility, Version=2.0.0.0, Culture=neutral,
                    PublicKeyToken=B03F5F7F11D50A3A"/>
           </assemblies>
       </compilation>
       <authentication mode="Windows"/>
   </system.web>
   <system.net>
       <mailSettings>
           <smtp from="padhiyarashvin@gmail.com">
               <network host="smtp.gmail.net" port="587" userName ="GmailUserName@gmail.com"
                        password="GmailPassword" defaultCredentials="false"/>
           </smtp>
       </mailSettings>
   </system.net>
</configuration>

Comments

Dynamics said…
nice article. if you are maintaining any template for email.. this may help you.
http://aspnettutorialonline.blogspot.com/2012/05/email-template-from-flat-file-or.html

Popular posts from this blog

Auto Refresh .aspx page - ASP.NET

Auto Sequence Number in Grid Control with Paging

MVC Request Execution Stages - Life Cycle

Overview of MVC

How to Update Dependent Excel Cells Automatically

Paged Data Source - Custom Paging

LINQ - C# Programming

ASP.NET MVC Version History

How to Edit More than 200 Rows in SQL Server 2008