Mail Sending in asp.net c#

SnoServer NameSMTP AddressPortSSL
1GMailsmtp.gmail.com587Yes
2Hotmailsmtp.live.com587Yes
3Yahoosmtp.mail.yahoo.com587Yes
4Rediffmailsmtp.rediffmail.com587Yes
5Outlooksmtp.live.com587Yes
6AOLsmtp.aol.com587Yes
7AIMsmtp.aim.com587Yes
8Zoho Mailsmtp.zoho.com465Yes
9Mail.comsmtp.mail.com587Yes
10Sify.comsmtp.sify.com587Yes



Aspx page

<table>
       <tbody><tr>
           <td colspan="2">
               <asp:label forecolor="Green" id="lblConfirmation" runat="server">
           </asp:label></td>
       </tr>
       <tr>
           <td>
               <b>Email To</b>
           </td>
           <td>
               <asp:textbox runat="server" id="txtEmailTo">
           </asp:textbox></td>
       </tr>
       <tr>
           <td>
               <b>Subject</b>
           </td>
           <td>
               <asp:textbox runat="server" id="txtEmailSubject">
           </asp:textbox></td>
       </tr>
       <tr>
           <td>
               <b>Body</b>
           </td>
           <td>
               <asp:textbox textmode="MultiLine" runat="server" id="txtEmailBody" height="100px" width="400px">
           </asp:textbox></td>
       </tr>
       <tr>
           <td>
                 
           </td>
           <td>
               <asp:button text="Send Email" id="btnSendEmail" runat="server" onclick="btnSendEmail_Click">
           </asp:button></td>
       </tr>
   </tbody></table>





Aspx.cs


using System.Net.Mail;
using System.Net;



//--- Button Click to send emails.
    protected void btnSendEmail_Click(object sender, EventArgs e)
    {
        using (MailMessage mm = new MailMessage())
        {
            mm.From = new MailAddress("YourEmailAddress"); //--- Email address of the sender
            mm.To.Add(txtEmailTo.Text); //---- Email address of the recipient.
            mm.Subject = txtEmailSubject.Text; //---- Subject of email.
            mm.Body = txtEmailBody.Text; //---- Content of email.
            mm.IsBodyHtml = false; //---- To specify wether email body contains HTML tags or not.
 
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; //---- SMTP Host Details.
            smtp.EnableSsl = true; //---- Specify whether host accepts SSL Connections or not.
            NetworkCredential NetworkCred = new NetworkCredential("YourEmailAddress", "YourPassword");
            //---Your Email and password
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587; //---- SMTP Server port number. This varies from host to host.
            smtp.Send(mm);
 
            //==== Show confirmation message.
            lblConfirmation.Text = "Congratulations, Your email successfully sent";
 
            //---- Clear form fields.
            txtEmailBody.Text = string.Empty;
            txtEmailSubject.Text = string.Empty;
            txtEmailTo.Text = string.Empty;
        }
    }




To use these email service providers you have to just do changes in :
  1. smtp.Host
  2. smpt.EnableSSL
  3. NetworkCredentials
  4. smtp.Port


Example sample code



SmtpClient smtp = new SmtpClient();
           smtp.Host = "smtp.rediffmail.com"; //---- SMTP Host Details.
           smtp.EnableSsl = true; //---- Specify whether host accepts SSL Connections or not.
           NetworkCredential NetworkCred = new NetworkCredential("YourRediffMailId", "YourRediffMailPassword");
           //---Your Email and password
           smtp.UseDefaultCredentials = true;
           smtp.Credentials = NetworkCred;
           smtp.Port = 587; //---- SMTP Server port number. This varies from host to host.
           smtp.Send(mm);



Comments

Popular posts from this blog

Reverse Sentence using c#

How to write Pure java script Program?