Asp.net Send Email using Gmail or Gmail SMTP Server | |
From | |
To | |
Subject | |
Body | |
Sendmail.aspx 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 id="Head1" runat="server"> <title>Send Mail</title> <link href="css/stylesheet.css" rel="stylesheet" type="text/css" /> <style type="text/css"> .style5 { color: #FFFFFF; font-size: small; font-weight: bold; background-color: #006699; } </style> </head> <body> <form id="form1" runat="server"> <div class="back-form" style="margin: 25%; font-family: verdana; padding: 5px 10px 5px 10px;"> <table class="style1"> <tr> <td class="style5" colspan="2"> Asp.net Send Email using Gmail or Gmail SMTP Server </td> </tr> <tr> <td class="style3"> <asp:Label ID="Label1" runat="server" Text="From"></asp:Label> </td> <td class="style3"> <asp:TextBox ID="Fromtxt" runat="server" CssClass="textbox-style" Width="239px"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="Label2" runat="server" Text="To"></asp:Label> </td> <td> <asp:TextBox ID="Totxt" runat="server" CssClass="textbox-style" Width="239px"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="Label3" runat="server" Text="Subject"></asp:Label> </td> <td> <asp:TextBox ID="Subjecttxt" runat="server" CssClass="textbox-style" Width="239px"></asp:TextBox> </td> </tr> <tr> <td valign="top"> <asp:Label ID="Label5" runat="server" Text="Body"></asp:Label> </td> <td valign="top"> <asp:TextBox ID="Bodytxt" runat="server" CssClass="textbox-style" Height="113px" TextMode="MultiLine" Width="337px"></asp:TextBox> </td> </tr> <tr> <td> </td> <td> <asp:Label ID="msg" runat="server" Style="font-weight: 700; font-size: small"></asp:Label> </td> </tr> <tr> <td> </td> <td> <asp:Button ID="SendMail" runat="server" Text="Send Mail" CssClass="black-button" OnClick="SendMail_Click" /> </td> </tr> </table> </div> </form> </body> </html> C# Code Behind
sendmail.cs using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Net.Mail; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void SendMail_Click(object sender, EventArgs e) { try { MailMessage mail = new MailMessage(); mail.To.Add(Totxt.Text); //Email ID where email is to be send // mail.To.Add("Another Email ID where you wanna send same email"); mail.From = new MailAddress(Fromtxt.Text); //"YourGmailID@gmail.com" mail.Subject = Subjecttxt.Text; //"Email using Gmail"; string Body = Bodytxt.Text; // body of mail mail.Body = Body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address smtp.Credentials = new System.Net.NetworkCredential ("YourUserName@gmail.com", "YourGmailPassword"); //Or your Smtp Email ID and Password smtp.EnableSsl = true; smtp.Send(mail); msg.ForeColor = System.Drawing.Color.Green; msg.Text = "Mail Send Successfully !!"; } catch (Exception ex) { msg.ForeColor = System.Drawing.Color.Red; msg.Text = ex.ToString(); } } } |
Thursday, 4 April 2013
Working Asp.net Send Email using Gmail or Gmail SMTP Server
Working Asp.net Send Email using Gmail or Gmail SMTP Server
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment