Send Email – C#, ASP.NET


This articles provides you a piece of code for sending email using C#. Sending email is necessary for any project whether its windows application, webservice or a web application.

First include a tag in web.config for defining host and specifying emal & password.

<system.net>
  <mailSettings>
    <smtp from="info@yourwebserverdomain.com">
      <network host="mail.yourwebserverdomain.com" port="25" defaultCredentials="false" username="info@yourwebserverdomain.com" password="emailpassword"/>
    </smtp>
  </mailSettings>
</system.net>

Host: For specifyind mail server host.

Port: For specifying port of your mail server. You need to get port number from your hosting provider if your port is other than 25. Default Port is 25.

Username: Mail id from which you want to send emails.

Paswword: Password of mail id.

Below is a method which is used to send email. Add method in your class file. Eg. is given below:

using System;
using System.Collections.Generic;
using System.Web;
using System.Net.Mail;

  public class Emailcl
  {
      public Emailcl()
      {

      }

public static void SendEmailWithReplyAddress(string sFromEmail, string sFromName, string sToEmail, string sToName , string sReplyToEmail, string sSubject, string sMessage, bool bIsHTML)
    {
      MailMessage oMessage = new MailMessage();
      oMessage.From = new MailAddress(sFromEmail, sFromName);
      oMessage.To.Add(new MailAddress(sToEmail, sToName));
      oMessage.Subject = sSubject;
      oMessage.Body = sMessage;
      oMessage.IsBodyHtml = bIsHTML;
      oMessage.ReplyTo = new MailAddress(sReplyToEmail, sFromName); ;
      SmtpClient oSmtpClient = new SmtpClient();
      oSmtpClient.Send(oMessage);
    }
 }

Tags: , , ,

Comments & Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

*


− 4 = 5

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>