Send emails using Gmail Credentials with ASP.NET and C# code

Sending email using asp.net with Gmail Credentials

Some times we don't have smtp server to send mails using ASP.NET. Here we are using gmail credentials for sending mail using gmail smtp server.

Output:
Send emails using Gmail Credentials with ASP.NET and C# code
Send emails using Gmail Credentials with ASP.NET and C# code


ASP.NET Code
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table style="width:100%;">
            <tr>
                <td>
                    GMail From Id</td>
                <td>
                    <asp:TextBox ID="txtFromEMailId" runat="server" Height="24px" Width="390px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    GMail From Id password</td>
                <td>
                    <asp:TextBox ID="txtEMailPassword" runat="server" Height="21px" 
                        TextMode="Password" Width="391px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    To Email Id</td>
                <td>
                    <asp:TextBox ID="txtToEMailId" runat="server" Height="22px" Width="390px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Subject</td>
                <td>
                    <asp:TextBox ID="txtEMailSubject" runat="server" Height="20px" 
                        ontextchanged="TextBox4_TextChanged" Width="392px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    EMail Body</td>
                <td>
                    <asp:TextBox ID="txtEMailBody" runat="server" Height="199px" 
                        TextMode="MultiLine" Width="401px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnSendEMail" runat="server" onclick="btnSendEMail_Click" 
                        Text="Send Email" />
                </td>
                <td>
                    <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>



C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void TextBox4_TextChanged(object sender, EventArgs e)
    {

    }
    protected void btnSendEMail_Click(object sender, EventArgs e)
    {
        try
        {
            bool status=SendEmail(txtFromEMailId.Text, txtEMailPassword.Text, txtToEMailId.Text, txtEMailSubject.Text, txtEMailBody.Text);
            if (status == true)
            {
                Response.Write("Message sent successfully");
            }
            else
            {
                Response.Write("Message not sent");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected bool SendEmail(string fromEMailId, string password, string toEMailId, string emailSubject, string EMailBody)
    {
        try
        {
            MailMessage email = new MailMessage();
            email.From =new MailAddress(fromEMailId);
            email.To.Add(toEMailId);
            email.Subject = emailSubject;
            email.Body = EMailBody;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential(fromEMailId, password);
            smtp.UseDefaultCredentials = true;
            smtp.EnableSsl = true;
            smtp.Send(email);
            
            return true;
        }
        catch (Exception ex)
        {
            return false;
            throw ex;
        }
    }
}


Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar