Send a message using SMTP server
Smtp object provides a wide range of methods and properties for tuning the process of a message sending. A new instance of Smtp object can be created as follows:
Smtp mailer = new Smtp();
If the SMTP server does not require any authentication, the specified host name or its IP address is enough to connect to this SMTP server.
mailer.SmtpServers.Add("mail.domain.com");
mailer.SmtpServers.Add("mail.domain.com")
However, most servers nowdays require users to authenticate themselves (i.e. you must have a valid account if you're going to send e-mail through this server to external recipients). To specify your account credentials, use this code:
mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret");
mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret")
Depending on the server settings, you may be required to specify the entire e-mail address instead of just an the account name (e.g. jdoe@domain.com or domain.com\jdoe in some cases).
Some SMTP servers require the clients to authenticate themselves but do not support any SMTP commands for that. These servers rely on the authentication result of the recent POP3 connection from the same client (i.e. the same IP address). This is possible because POP3 and SMTP servers usually share the same user accounts database. See SMTP Authentication for details.
Prior to sending the message, you should specify its contents. A couple of examples:
mailer.Message.From.AsString = "jdoe@domain.com";
mailer.Message.To.AsString = "bill@domain.com";
mailer.Message.Subject = "Hi";
mailer.Message.BodyPlainText = "Hello Bill";
mailer.Message.From.AsString = "John Doe <jdoe@domain.com>";
mailer.Message.To.AsString = "Bill <bill@domain.com> (Company Inc), Kathy <kathy@server.com>";
mailer.Message.Subject = "The document";
mailer.Message.BodyHtmlText = "<html>The document body</html>";
mailer.Message.From.Email = "jdoe@domain.com";
mailer.Message.From.DisplayName = "John Doe";
mailer.Message.To.AsString = "Kathy <kathy@server.com>";
mailer.Message.Cc.Add("bill@domain.com", "Bill Smith");
mailer.Message.Subject = "News";
mailer.Message.BodyPlainText = "News body";
mailer.Message.From.AsString = "jdoe@domain.com"
mailer.Message.To.AsString = "bill@domain.com"
mailer.Message.Subject = "Hi"
mailer.Message.BodyPlainText = "Hello Bill"
mailer.Message.From.AsString = "John Doe <jdoe@domain.com>"
mailer.Message.To.AsString = "Bill <bill@domain.com> (Company Inc), Kathy <kathy@server.com>"
mailer.Message.Subject = "The document"
mailer.Message.BodyHtmlText = "<html>The document body</html>"
mailer.Message.From.Email = "jdoe@domain.com"
mailer.Message.From.DisplayName = "John Doe"
mailer.Message.To.AsString = "Kathy <kathy@server.com>"
mailer.Message.Cc.Add("bill@domain.com", "Bill Smith")
mailer.Message.Subject = "News"
mailer.Message.BodyPlainText = "News body"
When the message is ready, you can now send it.