Configuring SMTP in .Net

less than 1 minute read

One of the easiest way to configure a mail service on .NET site is to create a SMTP class and call its function SendMail.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
public static class SMTP
public static void SendMail(string sFrom, string sTo, string sCC, string sSubject,
string sBODY, bool IsBodyHTML, string sHostName, string sAttachment)
MailMessage message = new MailMessage();
MailAddress address = new MailAddress(sFrom);

message.From = address;
if (sCC != string.Empty)
message.IsBodyHtml = IsBodyHTML;
message.Body = sBODY;
message.Subject = sSubject;
if (sAttachment != string.Empty)
message.Attachments.Add(new Attachment(sAttachment));

SmtpClient client = new SmtpClient(sHostName, 25);
catch (Exception Ex)