Not logged in - Login

Authenticating to an SMTP server

Authenticating and connecting to an SMTP server from within C# code.

// build the email message
System.Net.Mail.MailMessage msg = 
   new System.Net.Mail.MailMessage("sender@nowhere.com", "recipient@somewhere.com");

// set the properties of the email message.
msg.Subject = "Test email message.";
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false;
msg.Body = "This is a test email message.";

// Create an instance of the SMTP object.
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
// Set the host for the SMTP client
smtp.Host = "smtp-mail.nowhere.com";

// Create an instance of the Network Credentials object.
System.Net.NetworkCredential cred = new System.Net.NetworkCredential();

// Set the Username & Password on the Network Credential object
cred.UserName = "sender@nowhere.com";
cred.Password = "Pa$$w0rd";

// Set the credentials for the SMTP object.
smtp.Credentials = cred;

// Send the email
smtp.Send(msg);