Sending SMTP Mail via C# .NET

-------------------------------------------------------
Goto blog home
Visit my new blog dedicated to Internet of Things, Embedded Programming & Automation
-------------------------------------------------------
As a pre-requisite, SMTP service must be installed in IIS on the server from which the mail sending program will be running. For more details on SMTP service setup read guidance from Microsoft at http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/e4cf06f5-9a36-474b-ba78-3f287a2b88f2.mspx?mfr=true

The main class that you need to refer is:
using System.Web.Mail;

Then you may write the function to send an e-mail as shown below:
public bool SendEMail()
{
//Prepare the e-mail object
MailMessage objMailMessage = new MailMessage();
objMailMessage.BodyFormat = MailFormat.Html;
objMailMessage.Priority = MailPriority.High;
objMailMessage.To = "to@tocompany.com";
objMailMessage.From = "from@yourcompany.com";
objMailMessage.Subject = "Hi - Test Mail";
objMailMessage.Body = "Hello world.";

//Send the e-mail
SmtpMail.SmtpServer = "server.domain.com";

SmtpMail.Send(objMailMessage);
}

No comments:

Post a Comment