Wednesday 5 February 2014

How we can send mail in C#??

How we can send mail through C#??

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(); 
}

No comments:

Post a Comment