18 Sep 2007

Send Mail through SMTP with SSL Authentication like GMail

 

This example will help you to send email from ASP.NET using Gmail SMTP (SSL authentication required):

<%@ Page Language="C#" %>

<%@ Import namespace="System.Net"%>
<%@ Import namespace="System.Net.Mail"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void btnSend_Click(object sender, EventArgs e)
{
    //
    // Set your Gmail account credentials here:
    //
    string fromEmail = "YourEmail@gmail.com";
    string password = "YourPassword";
   
    string toEmail = txtToEmail.Text;
    string subject = txtSubject.Text;
    string body = txtBody.Text;
   
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.Credentials = new NetworkCredential(fromEmail, password);
   
    MailMessage msg = new MailMessage(fromEmail, toEmail, subject, body);
    smtp.Send(msg);
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Send Mail throught SMTP with SSL Authentication (GMail)</title>
</head>
<body>
<form id="frmEmail" runat="server">
    <div>
        Email to: <asp:TextBox ID="txtToEmail" runat="server" />
        <br />
        Subject: <asp:TextBox ID="txtSubject" runat="server" />
        <br />
        <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Rows="10" Columns="50" />
        <br />
        <asp:Button id="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" />
    </div>
</form>
</body>
</html>

You can also use the ASP.NET configuration file (web.config) to store your GMail account:

<?xml version="1.0"?>
<configuration>
    <system.net>
        <mailSettings>
            <smtp>
            <network
                host="smtp.gmail.com"
                port="587"
                defaultCredentials="false"
                userName="YourEmail@gmail.com"
                password="YourPassword" />
        </smtp>
        </mailSettings>
    </system.net>
</configuration>

So the page code becomes:

protected void btnSend_Click(object sender, EventArgs e)
{
    string fromEmail = "YourEmail@gmail.com";
   
    string toEmail = txtToEmail.Text;
    string subject = txtSubject.Text;
    string body = txtBody.Text;
   
    // Appropriate SMTP parameters from web.config:
    SmtpClient smtp = new SmtpClient();
   
    MailMessage msg = new MailMessage(fromEmail, toEmail, subject, body);
    smtp.Send(msg);
}

Download

Send-Mail-through-SMTP-with-SSL-Authentication.zip (1.53 kb)

HTH

Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

trackback

DotNetKicks.com

September 18 2007 19:29

Trackback from DotNetKicks.com

Send Mail through SMTP with SSL Authentication like GMail

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

Gravatar

May 16 2008 14:57