C# Sending Email from IIS 6 OR IIS 7

Quick and dirty how to send email from a web site using C#.  This code can be added using Visual Studios Web Express of the full version of Visual Studios.  I am currently developing in .NET 3.5 and 4.0

You should not any problems if your server is configured correctly. *If running IIS 7 you need to ensure that IIS 6 Compatibility Mode is checked for the role of you IIS Server Instance.

Setting up your server


This is tricky. First you need to make sure SMTP is installed as a “Role” for your Windows Server,
Then you need to configure your “Sites” SMTP to point at the “localhost”. After you do this you will able to follow the same directions on the link above.

You have to go to the Start-> Administrative tools -> Internet Iformation Services (IIS) 6.0 Manager.

Adding the Code:

Step 1: Add References at the top of your codebehind

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

Step 2: Add this class

public class EmailClient
public string SimpleMail(string mailto, string mailfrom, string mailsubject, string mailbody, string mailcc, string mailbcc, string smtpserver, string username, string password, string attachment)

string _results = “success!”;

//Create new SMTP Message
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
MailAddress senders = new MailAddress(mailfrom);
mm.From = senders;

mm.Priority = System.Net.Mail.MailPriority.Normal;
mm.IsBodyHtml = true;
string[] emailTo = mailto.Split(‘;’);

//Parse through “To Emails”
foreach (string e in emailTo)
if (!string.IsNullOrEmpty(e))
mm.Subject = mailsubject;
mm.Body = mailbody;

//Parse through “To CC”
string[] emailcc = mailcc.Split(‘;’);

if (mailcc.Length > 0)
foreach (string cc in emailcc)
if (!string.IsNullOrEmpty(cc))

//Parse through “To BCC”
string[] bccs = mailbcc.Split(‘;’);

if (bccs.Length > 0)
foreach (string bcc in bccs)
if (!string.IsNullOrEmpty(bcc))
//Send Emmail to local SMTP
SendEmail(mm, smtpserver);

catch (Exception e)
_results = e.Message.ToString();
//Enable one of these for debugging
//throw new Exception(e.Message);
// writeerrorlog(e.Message);

return _results;

public static void SendEmail(System.Net.Mail.MailMessage m, string smtpserver)
SmtpClient smtp = new SmtpClient();
smtp.Port= 25;
smtp.UseDefaultCredentials= true;

Step 3: Send a email with the custom class above

string results = “”;
EmailClient eclient = new EmailClient();
results= eclient.SimpleMail(“noreply@email.com”, “info@email.com;info2.email.com”, “infocc@email.com;info2cc.email.com”, “infobcc@email.com;info2bcc.email.com”, “New Email Subject”, “Some info that you want to send someone”, “localhost”);



If you are experiencing issues sending check to make sure the smtp service is running.  If it shows as a red X.  Just restarted and continue testing.

Good Luck!!


If you have any problems post it, so that I can resolve your problem.




About Art Hicks

I am Business Owner/Applications Engineer that specializes in providing rich solutions to my clients and colleagues.
Aside | This entry was posted in C#. Bookmark the permalink.

0 Responses to C# Sending Email from IIS 6 OR IIS 7

  1. Cory Dusel says:

    Thanks for this wonderful post. I actually like the content you put up on your website. Added to my bookmarks for future visits.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s