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
IIS 6
http://msdn.microsoft.com/en-us/library/8b83ac7t.aspx
IIS 7
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.To.Add(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))
{
mm.CC.Add(cc);
}
}
}
//Parse through “To BCC”
string[] bccs = mailbcc.Split(‘;’);
if (bccs.Length > 0)
{
foreach (string bcc in bccs)
{
if (!string.IsNullOrEmpty(bcc))
{
mm.Bcc.Add(bcc);
}
}
}
//Send Emmail to local SMTP
try
{
SendEmail(mm, smtpserver);
}
catch (Exception e)
{
_results = e.Message.ToString();
//Enable one of these for debugging
//throw new Exception(e.Message);
//OR
// writeerrorlog(e.Message);
}
return _results;
}
public static void SendEmail(System.Net.Mail.MailMessage m, string smtpserver)
{
SmtpClient smtp = new SmtpClient();
smtp.Host=smtpserver;
smtp.Port= 25;
smtp.UseDefaultCredentials= true;
smtp.Send(m);}
}
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”);
DONE!!!
Troubleshooting
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!!
Art
If you have any problems post it, so that I can resolve your problem.
Thanks for this wonderful post. I actually like the content you put up on your website. Added to my bookmarks for future visits.