System.Web.Mail FAQ
- 3 Advanced Programming Samples
- 3.1 How do I send non US-ASCII emails?
- 3.2 How do I send a web page?
- 3.3 How do I embed images in an email?
- 3.4 How do I email a website exception
- 3.5 How do I encrypt messages using s/mime or pgp?
- 3.6 How do I send multipart/related emails?
- 3.7 How do I send multipart/alternative emails?
- 3.8 How do I authenticate to send an email?* You Are Here * [ Scroll To Answer ] [ Discuss ]
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# ]
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 ); }