Monday, December 24, 2012

Send E-Mail using C# Asp .Net

==>>First you need have  to design the aspx page like :---

  <div>
<table  align="center">
<tr>
<td colspan="2" align="center">
<b>Send Mail using C# Asp.Net</b>
</td>
</tr>
<tr>
<td>
Gmail ID:
</td>
<td>
<asp:TextBox ID="txtUserEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Gmail Password:
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="txtSubjectText" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
To:
</td>
<td>
<asp:TextBox ID="txtToEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
<asp:TextBox ID="txtBodyPart" runat="server" TextMode="MultiLine"  ></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSend" Text="Send" runat="server" onclick="btnSend_Click" />
</td>
</tr>
</table>
</div>

 

==>>Secound you need have  to write  the the code on  aspx.cs page  on BtnSend Click event  :---

   protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage Msg = new MailMessage();
            Msg.From = new MailAddress(txtUserEmail .Text);
            Msg.To.Add(txtToEmail.Text);
            Msg.Subject = txtSubjectText.Text;
            Msg.Body = txtBodyPart.Text;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential(txtUserEmail .Text, txtPassword .Text);
            smtp.EnableSsl = true;
            smtp.Send(Msg);
            Msg = null;
            Page.RegisterStartupScript("Message", "<script>alert('E-Mail Successfully Send...');if(alert){ window.location='Default.aspx';}</script>");
        }
        catch (Exception ex)
        {
         
        }
    }

Note:-You need to add this namespace in .cs file>>  using System.Net.Mail;

Hope this will help you