MailBee. NET Objects Tutorials

Sending e-mails using multiple SMTP servers

The multiple SMTP servers are useful when the developer is unsure of reliability of the SMTP server he is going to use for sending e-mails (e.g. the server crashes sometimes). For instance, when using a single SMTP server to send a message, if this SMTP server crashes, MailBee is unable to send the message.

Otherwise, when using multiple SMTP servers, if one SMTP server crashes, MailBee attempts to use another SMTP server according to the priority of these servers. This means that a server having the highest priority is used instead of the crashed server.

Thus, the use of multiple SMTP servers increases the reliability of the message sending process. MailBee.NET Objects allows the developer to manage the collection of SmtpServer objects, stored in SMTP.SmtpServers property of the SMTP object. The developer should call the SMTP.SmtpServers.Add method to add a new instance of SmtpServer object to collection. There are five overloads of this method. The first overload allows adding the direct SmtpServer object as follows:

C#

SmtpServer smtp_srv = new SmtpServer();

smtp_srv.Name = "mail.domain.com";
smtp_srv.AccountName = "john_doe";
smtp_srv.Password = "secret";

oMailer.SmtpServers.Add(smtp_srv);

VB.NET

Dim smtp_srv As SmtpServer =  New SmtpServer() 
 
smtp_srv.Name = "mail.domain.com"
smtp_srv.AccountName = "john_doe"
smtp_srv.Password = "secret"
 
oMailer.SmtpServers.Add(smtp_srv)

The next overload of the SMTP.SmtpServers.Add method allows adding new SMTP servers by the specified server name or corresponding IP-address as shown below:

C#

oMailer.SmtpServers.Add("127.0.0.1");
oMailer.SmtpServers.Add("smtp.company.com");

VB.NET

oMailer.SmtpServers.Add("127.0.0.1")
oMailer.SmtpServers.Add("smtp.company.com")

Besides, the developer can specify the port number and the priority of SMTP server. The default priority is 0 (i.e. the highest) and the default port number is 25. The following code adds two SMTP servers with different priority levels to collection:

C#

oMailer.SmtpServers.Add("127.0.0.1", 33, 1);
oMailer.SmtpServers.Add("smtp.company.com", 37, 2);

VB.NET

oMailer.SmtpServers.Add("127.0.0.1", 33, 1)
oMailer.SmtpServers.Add("smtp.company.com", 37, 2)

In addition, the following overload of the SMTP.SmtpServers.Add method allows adding SMTP servers to collection by the specified server name (or corresponding IP-address), login and password as shown below:

C#

oMailer.SmtpServers.Add("127.0.0.1", "dan_brown", "password");
oMailer.SmtpServers.Add("smtp.company.com ", "john_doe", "secret");

VB.NET

oMailer.SmtpServers.Add("127.0.0.1", "dan_brown", "password")
oMailer.SmtpServers.Add("smtp.company.com ", "john_doe", "secret")