SmtpLogin Method |
Namespace: MailBee.SmtpMail
Exception | Condition |
---|---|
MailBeeException | An error occurred and ThrowExceptions is true. |
By default, this method does nothing because authentication is optional in SMTP/ESMTP.
If ESMTP auhtentication is enabled (AuthMethods is not None), this method will attempt to authenticate on the SMTP server using the best (most secure) authentication method specified in AuthMethods value and supported by the server.
If the login attempt fails, it might still be possible to send mail (with some restrictions, such as sending to local recipients only). Due to this, MailBee makes it possible to ignore authentication errors if IgnoreLoginFailure is set to true. ErrorOccurred warning event, however, will still be issued to let the application know about the problem.
![]() |
---|
Another alternative to Login method is using POP-before-SMTP authentication. It can be used if the mail server does not support ESMTP authentication, but still requires users to authnticate themselves in order to send mail. See AuthPopBeforeSmtp(String, Int32, String, String) method documentation for more information. |
For examples on using OAuth 2.0 authentication (XOAUTH2 in Gmail and Office 365), see OAuth2 topic.
using System; using MailBee; using MailBee.SmtpMail; class Sample { // ErrorOccurred event handler. private static void OnErrorOccurred(object sender, ErrorEventArgs e) { if (e.Reason is IMailBeeLoginException) { Console.WriteLine(e.Reason.Message); // Login error. } } // The actual code. static void Main(string[] args) { Smtp mailer = new Smtp(); // Specify SMTP server and authentication settings. // This constructor also sets server.AuthMethods = AuthenticationMethods.Auto SmtpServer server = new SmtpServer("smtp.somedomain.com", "jdoe", "badpassword"); server.IgnoreLoginFailure = true; mailer.SmtpServers.Add(server); // Subscribe to the ErrorOccurred event. mailer.ErrorOccurred += new ErrorEventHandler(OnErrorOccurred); mailer.Connect(); mailer.Hello(); // Authenticate the user "jdoe". mailer.Login(); mailer.Disconnect(); } }