Send Email From C# / VB.NET / ASP.NET Using SMTP - GemBox

The following example shows how you can use the GemBox.Email library to send an email with a plain text body from your console application using C# or VB.NET code.

  • C#
  • VB.NET
Copy View on GitHub using GemBox.Email; using GemBox.Email.Smtp; class Program { static void Main() { // If using the Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY"); // Create new email message. MailMessage message = new MailMessage( new MailAddress("sender@example.com", "Sender"), new MailAddress("first.receiver@example.com", "First receiver"), new MailAddress("second.receiver@example.com", "Second receiver")); // Add additional receivers. message.Cc.Add( new MailAddress("third.receiver@example.com", "Third receiver"), new MailAddress("fourth.receiver@example.com", "Fourth receiver")); // Add subject and body. message.Subject = "Send Email in C# / VB.NET / ASP.NET"; message.BodyText = "Hi 👋,\n" + "This message was created and sent with GemBox.Email.\n" + "Read more about it on https://www.gemboxsoftware.com/email"; // Create new SMTP client and send an email message. using (SmtpClient smtp = new SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)")) { smtp.Connect(); smtp.Authenticate("<USERNAME>", "<PASSWORD>"); smtp.SendMessage(message); } } } Imports GemBox.Email Imports GemBox.Email.Smtp Module Program Sub Main() ' If using the Professional version, put your serial key below. ComponentInfo.SetLicense("FREE-LIMITED-KEY") ' Create new email message. Dim message As New MailMessage( New MailAddress("sender@example.com", "Sender"), New MailAddress("first.receiver@example.com", "First receiver"), New MailAddress("second.receiver@example.com", "Second receiver")) ' Add additional receivers. message.Cc.Add( New MailAddress("third.receiver@example.com", "Third receiver"), New MailAddress("fourth.receiver@example.com", "Fourth receiver")) ' Add subject and body. message.Subject = "Send Email in C# / VB.NET / ASP.NET" message.BodyText = "Hi 👋," & vbLf & "This message was created and sent with GemBox.Email." & vbLf & "Read more about it on https://www.gemboxsoftware.com/email" ' Create new SMTP client and send an email message. Using smtp As New SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)") smtp.Connect() smtp.Authenticate("<USERNAME>", "<PASSWORD>") smtp.SendMessage(message) End Using End Sub End Module

The above code creates a simple email that is represented with a MailMessage object.It has a plain text body, and it's sent using the SmtpClient to multiple recipients, which can be specified in MailMessage.To, MailMessage.Cc and MailMessage.Bcc address collections.

Sending a basic email in C# and VB.NET
Screenshot of generated plain text email

The SmtpClient.SendMessage method sends the MailMessage to the SMTP server for delivery, and the SMTP server will immediately send that email to its recipients.

If you'd like to know how to create advanced emails, including HTML body, attachment, and images, check the HTML with Attachment example.

You can reuse the same instance of SmtpClient to send multiple emails.

However, in the case of an ASP.NET application, we encourage you to recreate the client for each request, as shown in the following examples.

The SmtpClient.SendMessage method sends the MailMessage to the SMTP server for delivery, and the SMTP server will immediately send that email to its recipients.

If you'd like to know how you can create advance emails, which include HTML body, attachment and images, then check the HTML with Attachment example.

Sending email in ASP.NET MVC

With this example, you can incorporate email-sending capabilities into ASP.NET MVC applications.

  • C#
  • VB.NET
Copy public class ExampleController : Controller { public ActionResult SendEmail() { var host = "<SMTP SERVER DOMAIN>"; var port = 0; var security = GemBox.Email.Security.ConnectionSecurity.Auto; System.Net.Security.RemoteCertificateValidationCallback ignoreCertificate = (sender, certificate, chain, errors) => true; var username = "<USERNAME>"; var password = "<PASSWORD>"; // Set the SMTP host, port and connection security. using (var smtp = new SmtpClient(host, port, security, ignoreCertificate)) { // Connect and sign to the SMTP server. smtp.Connect(); smtp.Authenticate(username, password); // Create and send a new email. var message = new MailMessage(); // ... smtp.SendMessage(message); } return new EmptyResult(); } }Public Class ExampleController Inherits Controller Function SendEmail() As ActionResult Dim host = "<SMTP SERVER DOMAIN>" Dim port = 0 Dim security = GemBox.Email.Security.ConnectionSecurity.Auto Dim ignoreCertificate As System.Net.Security.RemoteCertificateValidationCallback = Function(sender, certificate, chain, errors) True Dim username = "<USERNAME>" Dim password = "<PASSWORD>" ' Set the SMTP host, port and connection security. Using smtp As New SmtpClient(host, port, security, ignoreCertificate) ' Connect and sign to the SMTP server. smtp.Connect() smtp.Authenticate(username, password) ' Create and send a new email. Dim message As New MailMessage() ' ... smtp.SendMessage(message) End Using Return New EmptyResult() End Function End Class

Embedded within the ExampleController class, the SendEmail action method demonstrates connecting to an SMTP server, setting up security parameters, and sending an email message.

Sending email in ASP.NET Web Forms

This example demonstrates a method for integrating email functionality into ASP.NET Web Forms applications using the GemBox.Email library.

  • C#
  • VB.NET
Copy public partial class ExamplePage : Page { protected void SendEmailButton_Click(object sender, EventArgs e) { // Create SMTP client. using (var smtp = new SmtpClient("<HOST>")) { // Connect and sign to the SMTP server. smtp.Connect(); smtp.Authenticate("<USERNAME>", "<PASSWORD>"); // Create and send a new email. var message = new MailMessage(); // ... smtp.SendMessage(message); } } }Public Class ExamplePage Inherits Page Protected Sub SendEmailButton_Click(sender As Object, e As EventArgs) Handles SendEmailButton.Click ' Create SMTP client. Using smtp As New SmtpClient("<HOST>") ' Connect and sign to the SMTP server. smtp.Connect() smtp.Authenticate("<USERNAME>", "<PASSWORD>") ' Create and send a new email. Dim message As New MailMessage() ' ... smtp.SendMessage(message) End Using End Sub End Class

The code, embedded within the ExamplePage class, handles a secure connection to an SMTP server, authenticating credentials and sending an email message upon button click (SendEmailButton_Click). You can easily adapt and customize this code to suit your specific needs, making it a practical resource for incorporating essential email-sending capabilities in ASP.NET Web Forms projects.

Using an SMTP Server Like Gmail, Outlook and Yahoo

Every SMTP server has a different server address, and you'll need to find out what your email server's address is before you can use it from your C# and VB.NET code.

To connect an SMTP server with your .NET application, you need to create the SmtpClient object and provide it with the address of your outgoing mail server.

There are many free email providers that you can use to send an email. Here is the summary of SMTP server settings for some commonly used free email services:

ProviderHostPortConnectionSecurity
Gmailsmtp.gmail.com465SSL
Outlooksmtp-mail.outlook.com587STARTTLS
Office 365smtp.office365.com587STARTTLS
Yahoosmtp.mail.yahoo.com465SSL
AOLsmtp.aol.com465SSL

Từ khóa » Visual Basic Send Email