MailBee. NET Objects Tutorials

How can I send e-mail through Gmail with C# or VB.NET

Gmail by default restricts external access via SMTP and IMAP. You need to enable it in Gmail account settings. You also need to enable "Less secure apps" to make it possible to authenticate with a login/password. The option name is misleading, they are secure enough. Google just assumes that any authentication which involves login/password exchange is insecure (despite that the transmission channel is TLS-secured).

If it's not an option to enable "Less secure apps", you'll need to use OAuth 2.0 authentication: OAuth 2.0 in Windows, .NET Core, and ASP.NET MVC apps

If it's OK to use login/password authentication, that's how:

C#

using MailBee;
using MailBee.SmtpMail;

...
Smtp mailer = new Smtp();
SmtpServer server = new SmtpServer("smtp.gmail.com", "gmail-login", "gmail-password");
mailer.SmtpServers.Add(server);
mailer.From.Email = "user@gmail.com";
mailer.To.Add("kathy@company.com");
mailer.Subject = "Report";
mailer.BodyPlainText = "The report contents";
mailer.Send();

VB.NET

Imports MailBee
Imports MailBee.SmtpMail

...
Dim mailer As New Smtp
Dim server As SmtpServer = New SmtpServer("smtp.gmail.com", "gmail-login", "gmail-password")
mailer.SmtpServers.Add(server)
mailer.From.Email = "user@gmail.com"
mailer.To.Add("kathy@company.com")
mailer.Subject = "Report"
mailer.BodyPlainText = "The report contents"
mailer.Send()

MailBee recognizes "smtp.gmail.com" host name and automatically adjusts port number to 465 (dedicated SSL port for SMTP). In case of exceptions, be sure to read the exception text carefully, Gmail often provides self-explaining error messages with clear instructions on what to do. For instance, WEB LOGIN REQUIRED with the link the user must open to confirm access to their account from a new location.