Sending email in ASP.NET

Sending mail in ASP.NET is simple! You just create a message and then call the SmtpMail’s static send method:

MailMessage message = new MailMessage();message.To = "me@email.com";message.From = "me@email.com";message.Subject = "An email from helen";message.Body = "Hello Helen. From Helen.";SmtpMail.Send(message);

If you need to talk to a non-local server, that’s not really much more difficult. You just specify a SmtpServer address in the SmtpMail object before you send:

SmtpMail.SmtpServer = "mail.myserver.com"; SmtpMail.Send(message);

If your SMTP server requires authentication, just specify your username and password with the following fields in your message object before you send it:

message.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;message.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "myusername";message.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "mypassword";

References:

4 guys email tutorial

Microsoft mail object documentation

Usenet post explaining how to set username and password

Posted on 08 Dec 04 by Helen Emerson (last updated on 08 Dec 04).
Filed under ASP.NET