Sunday 15 December 2013

C#.NET - Sending email through Gmail SMTP server in ASP.NET

Hi welcome to dotnetcookie.blogspot.in

In this article I will show you how to send an email through the Gmail SMTP server.

In this article we should use Gmail address (sample@gmail.com) and password (*******) to send an email.


SendMail.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendMail.aspx.cs" Inherits="SendMail.SendMail" %>

<html>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td colspan="2">
                    <h3>Sending Email in C#.NET using gmail.</h3>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Email</span>
                </td>
                <td>
                    <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Password</span>
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                    <span>To</span>
                </td>
                <td>
                    <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Subject</span>
                </td>
                <td>
                    <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <span>Body</span>
                </td>
                <td>
                    <asp:TextBox ID="txtBody" TextMode="MultiLine" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <asp:Button ID="btnSendEmail" runat="server" Text="SendMail" OnClick="btnSendEmail_Click" />
                </td>
            </tr>
        </table>

    </form>
</body>
</html>

Form


SendMail.aspx.cs

namespace SendMail
{
    public partial class SendMail : System.Web.UI.Page
    {
        protected void btnSendEmail_Click(object sender, System.EventArgs e)
        {
            try
            {
                // retrive all the details from the page.
                var fromAddress = txtFrom.Text;
                var toAddress = txtTo.Text;
                string fromPassword = txtPassword.Text;
                string subject = txtSubject.Text;
                string body = txtBody.Text;
 
                // Create a new SMTP Client instance by passing the SMTP details.
                var smtp = new System.Net.Mail.SmtpClient();
                {
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    smtp.Credentials = new System.Net.NetworkCredential(fromAddress, fromPassword);
                    smtp.Timeout = 20000;
                }
 
                //Send the email using the send method of the System.Net.Mail.SmtpClient
                smtp.Send(fromAddress, toAddress, subject, body);
                Response.Write("<script>alert('Mail Sent Successfully...')</script>");
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Response.Write("<script>alert('The username or password you entered is incorrect.')</script>");                
            }
            catch (System.Exception ex)
            {
                Response.Write("<script>alert('"+ex.Message+"')</script>");               
            }
            finally
            {
                ResetControls();
            }
        }
        
        private void ResetControls()
        {
            txtFrom.Text = string.Empty;
            txtTo.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtSubject.Text = string.Empty;
            txtBody.Text = string.Empty;
        }
    }
 
}

Form


No comments: