MailBee. NET Objects Tutorials

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:

C#

Smtp mailer = new Smtp();

VB.NET

Dim mailer As 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.

C#

mailer.SmtpServers.Add("mail.domain.com");

VB.NET

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:

C#

mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret");

VB.NET

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:

C#

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";

VB.NET

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.

C#

mailer.Send();

VB.NET

mailer.Send()