System.Web.Mail FAQ

Complete FAQ Listing


3.8 How do I authenticate to send an email? Printer Friendly   Email This FAQ   Discuss

If you are using the .NET Framework 1.0, this cannot be done. However, in the 1.1 version, the MailMessage.Fields property was added. This allowed access to the underlying CDO.Message fields.

The following example demonstrates sending your username and password to the SMTP server to provide authentication.
 
[ C# ]
private void Page_Load(object sender, System.EventArgs e)
{
	MailMessage mail = new MailMessage();
	mail.To = "me@mycompany.com";
	mail.From = "you@yourcompany.com";
	mail.Subject = "this is a test email.";
	mail.Body = "Some text goes here";
	mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");	//basic authentication
	mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "my_username_here"); //set your username here
	mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "super_secret");	//set your password here

	SmtpMail.SmtpServer = "mail.mycompany.com";  //your real server goes here
	SmtpMail.Send( mail );
}



[ VB.NET ]
Private Sub Page_Load(sender As Object, e As System.EventArgs)
   Dim mail As New MailMessage()
   mail.To = "me@mycompany.com"
   mail.From = "you@yourcompany.com"
   mail.Subject = "this is a test email."
   mail.Body = "Some text goes here"
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") 'basic authentication
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "my_username_here") 'set your username here
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "super_secret") 'set your password here
   SmtpMail.SmtpServer = "mail.mycompany.com" 'your real server goes here
   SmtpMail.Send(mail)
End Sub 'Page_Load

 

Discuss This FAQ Item
Got a question? Another Suggestion? Want to post your solution? Discuss it below.
New Post
  • System.Web.Mail & ESMTP    [ Reply ]
    • RE: System.Web.Mail & ESMTP    [ Reply ]
    • RE: System.Web.Mail & ESMTP    [ Reply ]
    • RE: System.Web.Mail & ESMTP    [ Reply ]
    • RE: System.Web.Mail & ESMTP    [ Reply ]
    • RE: System.Web.Mail & ESMTP    [ Reply ]
  • proxy information    [ Reply ]
    • RE: proxy information    [ Reply ]
      • RE RE: proxy information    [ Reply ]
        • RE: RE RE: proxy information    [ Reply ]
  • smtpauthenticate    [ Reply ]
    • RE: smtpauthenticate    [ Reply ]
      • RE RE: smtpauthenticate    [ Reply ]
        • RE: RE RE: smtpauthenticate    [ Reply ]
          • RE RE: RE RE: smtpauthenticate    [ Reply ]
            • RE: RE RE: RE RE: smtpauthenticate    [ Reply ]
          • RE RE: RE RE: smtpauthenticate    [ Reply ]
      • RE RE: smtpauthenticate    [ Reply ]
  • webmail authentification    [ Reply ]
    • RE: webmail authentification    [ Reply ]
    • RE: webmail authentification    [ Reply ]
    • RE: webmail authentification    [ Reply ]
      • RE RE: webmail authentification    [ Reply ]
  • CDO question    [ Reply ]
    • RE: CDO question    [ Reply ]
  • test    [ Reply ]
  • CDO troubles?    [ Reply ]
  • Authentication    [ Reply ]
  • About the authentication    [ Reply ]
    • RE: About the authentication    [ Reply ]
    • RE: About the authentication    [ Reply ]
  • Still can't connect    [ Reply ]
    • RE: Still can't connect    [ Reply ]
      • RE RE: Still can't connect    [ Reply ]
    • RE: Still can't connect    [ Reply ]
  • Framework1.0-send mail authentication    [ Reply ]
    • RE: Framework1.0-send mail authentication    [ Reply ]
      • RE RE: Framework1.0-send mail authentication    [ Reply ]
      • RE RE: Framework1.0-send mail authentication    [ Reply ]
  • Looks Great! But I are carfused...    [ Reply ]
    • RE: Looks Great! But I are carfused...    [ Reply ]
  • about the Mail Authentication Schemas...    [ Reply ]
  • SMTP Servers    [ Reply ]
    • RE: SMTP Servers    [ Reply ]
    • RE: SMTP Servers    [ Reply ]
    • RE: SMTP Servers    [ Reply ]
  • Attachment problem    [ Reply ]
  • 'Fields' is not a member of 'System.Web.Mail.MailMessage'    [ Reply ]
  • smtp server authentication    [ Reply ]
    • RE: smtp server authentication    [ Reply ]
      • RE RE: smtp server authentication    [ Reply ]
        • RE: RE RE: smtp server authentication    [ Reply ]
          • RE RE: RE RE: smtp server authentication    [ Reply ]
  • help!    [ Reply ]
    • RE: help!    [ Reply ]
    • RE: help!    [ Reply ]
  • is authentication necessary??    [ Reply ]
    • RE: is authentication necessary??    [ Reply ]
  • Send Email using GroupWise    [ Reply ]
  • who tell me why i still can't send a mail    [ Reply ]
  • SMTP    [ Reply ]
  • Authentication Problems    [ Reply ]
    • RE: Authentication Problems    [ Reply ]
      • RE RE: Authentication Problems    [ Reply ]
  • Smtp authentication    [ Reply ]
  • authentication SMTP    [ Reply ]
  • overwrite the default return-path field    [ Reply ]
  • The system indicates the error: can't access CDO.Message    [ Reply ]
  • How do I validate email address?    [ Reply ]
  • Can't open .aspx or User Controls after pointing project path to 1.1    [ Reply ]
  • Fields.Add Not Working in 1.1    [ Reply ]
    • RE: Fields.Add Not Working in 1.1    [ Reply ]
  • help needed    [ Reply ]