MailBee. NET Objects Tutorials

SMTP authentication

Most SMTP relay servers require users to authenticate themselves in order to be allowed to send e-mails to external e-mail addresses (addresses belonging to other domains). The login/password is usually the same as for POP3 or IMAP account of this user on the same server.

C#

Smtp mailer = new Smtp();
SmtpServer server = new SmtpServer("smtp.company.com", "jdoe", "secret");
mailer.SmtpServers.Add(server);
...
mailer.Send();

VB.NET

Dim mailer As New Smtp() 
Dim server As New SmtpServer("smtp.company.com","jdoe","secret") 
mailer.SmtpServers.Add(server)
...
mailer.Send()

In the case if you want to establish connection manually using Connect method, the procedure is as follows:

C#

Smtp mailer = new Smtp();
SmtpServer server = new SmtpServer("mail.domain.com", "user@domain.com", "pass");
mailer.SmtpServers.Add(server);
mailer.Connect();
mailer.Hello();
mailer.Login();

VB.NET

Dim mailer As New Smtp()
Dim server As New SmtpServer("mail.domain.com", "user@domain.com", "pass")
mailer.SmtpServers.Add(server)
mailer.Connect()
mailer.Hello()
mailer.Login()

If SMTP authentication is enabled (since the login and the password are specified in SmtpServer constructor), Login method will attempt to authenticate on the SMTP server using the best (the most secure) authentication method supported by the server. You can, however, change this behaviour to force MailBee to use the specified authentication methods or try the simplest rather than the most secure methods first. This makes sense if the server incorrectly implements the secure authentication protocol (surprisingly, this happens quite often).

The code below forces MailBee to use SASL LOGIN (insecure), SASL PLAIN (insecure) or SASL NTLM (secure) methods. SASL NTLM will be tried only if neither SASL LOGIN or SASL PLAIN is supported:

C#

Smtp mailer = new Smtp();
SmtpServer server = new SmtpServer("smtp.company.com", "jdoe", "secret");
server.AuthMethods = AuthenticationMethods.SaslLogin | 
AuthenticationMethods.SaslPlain | AuthenticationMethods.SaslNtlm;
server.AuthOptions = AuthenticationOptions.PreferSimpleMethods;
mailer.SmtpServers.Add(server);
...
mailer.Send();

VB.NET

Dim mailer As New Smtp() 
Dim server As New SmtpServer("smtp.company.com","jdoe","secret") 
server.AuthMethods = AuthenticationMethods.SaslLogin _ 
Or AuthenticationMethods.SaslPlain Or AuthenticationMethods.SaslNtlm
server.AuthOptions = AuthenticationOptions.PreferSimpleMethods
mailer.SmtpServers.Add(server)
...
mailer.Send()

Some older servers require clients to authenticate themselves but do not support the SMTP authentication. This is because SMTP authentication is an optional extension to the SMTP protocol (and it's more correct to call it ESMTP authentication). In this case you can use results of POP3 authentication performed against the POP3 server which shares the user accounts database with the given SMTP server. This is called POP-before-SMTP authentication. With MailBee, you can use POP-before-SMTP in two ways.

If the POP3 and the SMTP server names are the same (e.g. both servers are mail.domain.com) and the POP3 service is running on the default port 110:

C#

Smtp mailer = new Smtp();
mailer.SmtpServers.Add("mail.domain.com", "jdoe@domain.com", "secret").AuthPopBeforeSmtp = true;
...
mailer.Send();

VB.NET

Dim mailer As New Smtp() 
mailer.SmtpServers.Add("mail.domain.com", "jdoe@domain.com", "secret").AuthPopBeforeSmtp = True
...
mailer.Send()

If the server names are not the same (e.g. pop.domain.com for POP3 and smtp.domain.com for SMTP) or the POP3 port is non-standard, use AuthPopBeforeSmtp method prior to connecting to the SMTP server.

C#

Smtp mailer = new Smtp();
mailer.SmtpServers.Add("smtp.domain.com");
mailer.AuthPopBeforeSmtp("pop.domain.com", 110, "jdoe@domain.com", "secret");
...
mailer.Send();

VB.NET

Dim mailer As New Smtp() 
mailer.SmtpServers.Add("smtp.domain.com")
mailer.AuthPopBeforeSmtp("pop.domain.com", 110, "jdoe@domain.com", "secret")
...
mailer.Send()

Note: Once the POP-before-SMTP authentication was completed, you do not need to call Login method. Login method performs ESMTP authentication only.

MailBee also supports Windows Integrated Authentication (login using the credentials of the currently logged Windows user). It can be used as follows:

C#

Smtp mailer = new Smtp();
mailer.SmtpServers.Add("smtp.domain.com", null, null, AuthenticationMethods.SaslNtlm);

VB.NET

Dim mailer As New Smtp() 
mailer.SmtpServers.Add("smtp.domain.com", Nothing, Nothing, AuthenticationMethods.SaslNtlm)

However, it cannot be used if you're developing ASP.NET application which runs under anonymous IIS or ASP.NET user context (because the currently logged user will be the IIS user rather than the human which is using the application).