ASP.NET, Coding

How to send mails using Microsoft Schemas

The http://schemas.microsoft.com/cdo/configuration/ namespace defines most of the fields that are used to set configurations for various Collaboration Data Objects (CDO) objects. These configuration fields are set by using an implementation of the IConfiguration.Fields collection.

There are 3 basic fields used in this article from the CDOEX namespace. The 3 fields are smtpauthenticate, sendusername, sendpassword. All the values are read from the appSettings section under the respective keys. Please pass (mailTo, From, subject, body) as paramters to the below function to use it.

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.HtmlControls;
using System.Web.Mail;public class EmailSend
{
public EmailSend()
{
//
// TODO: Add constructor logic here //
}
public static string SendMail(String mailTo, String From, String subject, string body)
{
try
{
SmtpMail.SmtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
MailMessage oMessage = new MailMessage();
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1; //cdoBasic(Use the basic (clear text) authentication mechanism)
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = System.Configuration.ConfigurationManager.AppSettings"SmtpUser"];
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = System.Configuration.ConfigurationManager.AppSettings["SmtpPass"];
oMessage.BodyFormat = MailFormat.Html;
oMessage.Priority = MailPriority.High;
oMessage.From = From;
oMessage.Subject = subject;
oMessage.To = mailTo;
oMessage.Headers.Add("content-type", "text/html;");
oMessage.Body = body;
SmtpMail.Send(oMessage);

return "Success";
}
catch (Exception ex)
{
return "Error Sending Mail";
}
}
}