Saturday, 14 July 2012

Sending email in asp.net using smtp gmail server

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            color: #660066;
        }
        .style2
        {
            color: #0000CC;
        }
        .style3
        {
            color: #336699;
        }
        .style4
        {
            color: #666699;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div><center>
    <table style="height: 257px; width: 536px">
    <tr>
    <td class="style1"><strong>To</strong></td><td><asp:TextBox ID="txtto" runat="server" Height="25px" Width="360px"></asp:TextBox></td>
    </tr>
    <tr>
    <td class="style2"><strong>From</strong></td><td><asp:TextBox ID="txtfrom" runat="server" Height="25px" Width="360px"></asp:TextBox></td>
    </tr>
    <tr>
    <td class="style3">Subject</td><td><asp:TextBox ID="txtsubject" runat="server" Height="25px" Width="360px"></asp:TextBox></td>
    </tr>
    <tr>
    <td class="style4">Message</td><td><asp:TextBox ID="txtmessage" runat="server"
            TextMode="MultiLine" Height="90px" Width="367px"></asp:TextBox></td>
    </tr>
    <tr>
    <td></td><td><asp:Button ID="btnsave" runat="server" Text="submit"
            onclick="btnsave_Click" /></td>
    </tr>
    </table></center>
    </div>
    </form>
</body>
</html>
aspx.cs Code  
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

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

    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential("useremailid@gmail.com", "password");  
        smtp.EnableSsl = true;
        smtp.Send(txtfrom.Text, txtto.Text,txtsubject.Text,txtmessage.Text);

        txtto.Text = string.Empty;
        txtfrom.Text = string.Empty;
        txtsubject.Text = string.Empty;
        txtmessage.Text = string.Empty;
   
    }
}

No comments:

Post a Comment