Showing posts with label Sending Mail From Asp.net Page To Gmail Using SMTP. Show all posts
Showing posts with label Sending Mail From Asp.net Page To Gmail Using SMTP. Show all posts

Monday, June 30, 2014

Sending Mail From Asp.net Page To Gmail Using SMTP. Gmail Port Number 587



<body>
    <form id="form1" runat="server">
    <div align="center">
    <h3>Send Mail To Your's Gmail Using ASp.net</h3>
    <table>
   
    <tr>
    <td>Gmail User</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtfrom" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>Gmail Password</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtpassword" TextMode="Password" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>Subject</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtsub" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>To</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtto" runat="server" ></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
    <td>Body</td>
    <td>:</td>
    <td>
    <asp:TextBox ID="txtbody" runat="server"  TextMode="MultiLine" Columns="30" Rows="10"></asp:TextBox>
    </td>
   
   
    </tr>
    <tr>
   
    <td></td><td></td>
    <td>
   
        <br />
   
    <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" />
    </td>
    </tr>
    </table>
   
    </div>
    </form>
</body>


ASPX.CS 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;


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

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
       MailMessage newmsg = new MailMessage();

       newmsg.From = new MailAddress(txtfrom.Text);
      
        newmsg.Subject = txtsub.Text;
        newmsg.To.Add(txtto.Text);
        newmsg.Body = txtbody.Text;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential(txtfrom.Text, txtpassword.Text);
         
        smtp.EnableSsl = true;
        smtp.Send(newmsg);
        Response.Write("msg is send");

    }
}