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.
Smtp mailer = new Smtp();
SmtpServer server = new SmtpServer("smtp.company.com", "jdoe", "secret");
mailer.SmtpServers.Add(server);
...
mailer.Send();
Dim mailer As New Smtp()
Dim server As New SmtpServer("smtp.company.com","jdoe","secret")
mailer.SmtpServers.Add(server)
...
mailer.Send()
Some mail services (e.g. Gmail) may not allow you to authenticate even with a valid login/password in case if the connection comes from an unknown IP address. For instance, you can send with your mail client which runs on your PC but cannot send from a web server where MailBee is installed because the server has another IP address assigned. In this case you may need to adjust security settings for your mail account. In case of Gmail, you may also need to enable "Less secure apps" in their settings (by default, Gmail does now allow login/password authentication at all). Alternatively, you can use OAuth 2.0 authentication.
In the case if you want to establish connection manually using Connect method, the procedure is as follows:
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();
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:
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();
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:
Smtp mailer = new Smtp();
mailer.SmtpServers.Add("mail.domain.com", "jdoe@domain.com", "secret").AuthPopBeforeSmtp = true;
...
mailer.Send();
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.
Smtp mailer = new Smtp();
mailer.SmtpServers.Add("smtp.domain.com");
mailer.AuthPopBeforeSmtp("pop.domain.com", 110, "jdoe@domain.com", "secret");
...
mailer.Send();
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:
Smtp mailer = new Smtp();
mailer.SmtpServers.Add("smtp.domain.com", null, null, AuthenticationMethods.SaslNtlm);
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).