logo

SMTP Authentication With The MailMessage Class

Printer Friendly Version



Many people think that you cannot authenticate a SMTP server using the System.Web.Mail.MailMessage class and opt paying for an alternative.
Well heres a solution that may save you some money.

Dim Msg As New System.Web.Mail.MailMessage
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpserver"
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
Msg.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
Msg.To = "dst@addr.net"
Msg.From = "src@addr.net"
Msg.Subject = "Subject"
Msg.Body = "Message"
System.Web.Mail.SmtpMail.SmtpServer = "smtpserver"
System.Web.Mail.SmtpMail.Send(Msg)

This example is for a remote server, to authenticate the localhost you do not need to set the smtpserver field or the smtpserverport field.
Just change the sendusing field to 1 & SmtpMail.SmtpServer to localhost

For more things you can do with the MailMessage Class check out MSDN at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_schema_configuration.asp

Submission Date:  3/31/2004 9:08:45 AM
Submitted By:  JR Garner
My Home Page:  http://www.visionarydesignz.co.nz

My Biography