SmtpConnect Method
Connects to an SMTP server.

Namespace: MailBee.SmtpMail
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 12.5.0 build 687 for .NET 4.5
Syntax
public bool Connect()

Return Value

Type: Boolean
true if the method succeeds; otherwise, false.
Exceptions
ExceptionCondition
MailBeeExceptionAn error occurred and ThrowExceptions is true.
Remarks

If SmtpServers collection contains only one SmtpServer instance, the component will attempt to connect to the server denoted by this SmtpServer instance only.

If SmtpServers collection contains multiple SmtpServer instances, the component will first attempt to connect to the first instance (SmtpServers[0]), then, if it fails, to the second, etc. Since SmtpServer objects are sorted accordingly their priority values (Priority, high-priority servers will be tried first. This gives the developer ability to implement reliable mail sending mechanism with support of backup servers which should be used when primary servers fail.

If the connection has already been made in the past, and that connection failed for the first server in the collection (SmtpServers[0]) but then succeeded for the second server (SmtpServers[1]) so that the current position in SmtpServers collection advanced by 1, the component will now try to connect to the second server first. Thus, the component remembers which server in SmtpServers collection is up and running, so that subsequent connection attempts could try this server first. If now this server fails, the next server in SmtpServers collection is tried, etc. If the last server in the collection fails too, the component starts back from the beginning of the collection and tries the servers which have not yet been tried during the last connection attempt. Then, if they all failed too and the component reaches the same server it already tried during the current connection attempt (in our example, it's the second server in the collection), the component will report an error.

The developer can use GetCurrentSmtpServerIndex method to learn the position (in SmtpServers collection) of the server to which the component tried to connect last time (regardless whether that connection attempt succeeded or not).

Note Note
By default, MailBee can autodetect if the mail server you're connecting to requires SSL connection (this works for well-known mail services like gmail.com or live.com). To disable SSL mode and port autodetection, set AutodetectPortAndSslMode to false.
Examples
This sample attempts to connect to the primary SMTP server, but will automatically switch to the backup server if the primary server is down. If the connection is established, the name of the server the component connected to is printed into console.
// To use the code below, import these namespaces at the top of your code.
using System;
using MailBee;
using MailBee.SmtpMail;

// The actual code (put it into a method of your class)

Smtp mailer = new Smtp();

// Add high-piority (0) main server and lower-priority (1) backup server.
mailer.SmtpServers.Add("main.server.com", 25, 0);
mailer.SmtpServers.Add("backup.server.com", 25, 1);

// Attempt to connect.
mailer.Connect();

// Display the host name of the server the connected was established with.
Console.WriteLine(mailer.SmtpServers[mailer.GetCurrentSmtpServerIndex()].Name);

mailer.Disconnect();
See Also