Send Email using C# (Asp.net)

  1. Create a HTML page for Email body.
      • Set some parameters inside html page to send dynamic emails.
  2. Call SendMail Method.

Example HTML,

<html>
<body>

<h1>Test Mail #Some_Unic_ID1# </h1>

<p>My test  #Some_Unic_ID2# </p>

</body>
</html>

save this html as tsetMail.html

user this method to send email.Pass tsetMail.html path

public void SendMail(string tsetMailPath)
   {
           try
           {
               //Fetching Settings from WEB.CONFIG file.
               string NameOfEmailSender =    ConfigurationManager.AppSettings[“NameOfEmailSender”].ToString();
               string NameOfSenderPassword = ConfigurationManager.AppSettings[“NameOfSenderPassword”].ToString();
               string emailSenderHost = ConfigurationManager.AppSettings[“NameOfSmtpServer”].ToString();
               int emailSenderPort = Convert.ToInt16(ConfigurationManager.AppSettings[“NameOFPortNumber”]);
               Boolean SSLTrueOrFalse = Convert.ToBoolean(ConfigurationManager.AppSettings[“SSLTrueOrFalse”]);               //Fetching Email Body Text from EmailTemplate File.
               string FilePath = tsetMailPath;
               StreamReader str = new StreamReader(FilePath);
               string MailText = str.ReadToEnd();
               str.Close();
               MailText = MailText.Replace(“#Some_Unic_ID1#”, “Pass Some Vale”);
               MailText = MailText.Replace(“#Some_Unic_ID2#”, “Another value”);
stringsubject=”Mail subject test”;               //Base class for sending email
               MailMessage _mailmsg = new MailMessage();               //Make TRUE because our body text is html
               _mailmsg.IsBodyHtml = true;               //Set From Email ID
               _mailmsg.From = new MailAddress(NameOfEmailSender);               //Set To Email ID
               _mailmsg.To.Add(“Reciver_mail@gmail.com”);               //set CC
               _mailmsg.CC.Add(“CC_Mail@gmail.com”);                    //Set Subject
               _mailmsg.Subject = subject;               //Set Body Text of Email
               _mailmsg.Body = MailText;               //Now set your SMTP
               SmtpClient _smtp = new SmtpClient();
             _smtp.UseDefaultCredentials = false;
            _smtp.Credentials = new System.Net.NetworkCredential(NameOfEmailSender, NameOfSenderPassword);               //Set HOST server SMTP detail
               _smtp.Host = emailSenderHost;               //Set PORT number of SMTP
               _smtp.Port = emailSenderPort;
           _smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                         //Set Sender UserEmailID, Password
               NetworkCredential _network = new NetworkCredential(NameOfEmailSender, NameOfSenderPassword);
               _smtp.Credentials = _network;               //Send Method will send your MailMessage create above.
               _smtp.Send(_mailmsg);
           }
           catch (Exception ex)
           {
           }
}

 

 

Add this to your web.config
<appSettings>
<add key=”NameOfSmtpServer” value=”yor server ex: 10.10.10.10″/>
<add key=”NameOFPortNumber” value=”your port ex:12″/>
<add key=”NameOfEmailSender” value=”Sender Email”/>
<add key=”NameOfSenderPassword” value=”Sender Email Password”/>
<add key=”SSLTrueOrFalse” value=”True or False”/>
</appSettings>

Leave a comment