Send mail from asp to another email id

Sandeepraturi
Posted by Sandeepraturi under C# category on | Points: 40 | Views : 1282
-----------put this code into head tag-------------

<head runat="server">
<title></title>
<script type="text/javascript">
function ok() {
alert('Your mail has been sent succuessfully');
}
</script>
</head>
-----------------put this code into body tag..................
<div>
<table align="center">
<tr><td>To</td><td><asp:TextBox ID="to" runat="server"></asp:TextBox></td></tr></br>
<tr> <td> subject</td><td><asp:TextBox ID="sub" runat="server"></asp:TextBox></td></tr></br>

<tr><td>message</td><td><asp:TextBox ID="message" runat="server"
TextMode="MultiLine" Height="120px" Width="278px"></asp:TextBox></td></tr></br>

<tr> <td></td><td><asp:Button ID="btn" runat="server" Text="send"
onclick="btn_Click" Height="26px" Width="101px" /></td></tr>
</table>
</div>
-----------------------------c# code----------------------------

using System.Net.Mail;

protected void clear()
{
to.Text = "";
sub.Text = "";
message.Text = "";

}
protected void btn_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add(to.Text);
mail.From = new MailAddress("sandeepraturi@gmail.com");
mail.Body = message.Text;
mail.Subject = sub.Text;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
("sandeepraturi@gmail.com", "*********");
smtp.EnableSsl = true;
smtp.Send(mail);
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "ok();", true);
}
catch
{
Response.Write("write correct email id");
}

clear();
}

Comments or Responses

Login to post response