Thursday, June 23, 2005

SMTP Authentication in ASP.NET

Do you want to send email in ASP.NET and your SMTP server requires SMTP authentication?

Here is the code to do that:
=======================
public static void SendMail(
string mailTo,
string mailFrom,
string mailSubject,
string mailBody,
string mailCc,
string mailBcc,
string smtpServer,
string userName,
string password)
{
System.Web.Mail.MailMessage mailMessage = new System.Web.Mail.MailMessage();
mailMessage.From = mailFrom;
mailMessage.To = mailTo;
mailMessage.Subject = mailSubject;
mailMessage.Body = mailBody;
mailMessage.Cc = mailCc;
mailMessage.Bcc = mailBcc;
mailMessage.BodyFormat = System.Web.Mail.MailFormat.Text;

const int cdoBasic = 1;
const int cdoSendUsingPort = 2;

mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", 10);
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", false);

System.Web.Mail.SmtpMail.Send(mailMessage);
}
=======================

Original snippet is from here:
SMTP Authentication in ASP.NET

No comments:

Followers

About Me

My photo
Email me: blog@postjobfree.com