Send Email with SMTP in C# and VB.NET

The example shows how to send a mail message using the SmtpClient class.

Screenshot

Send Message Screenshot

See the full code below.

1using GemBox.Email;
2using GemBox.Email.Security;
3using GemBox.Email.Smtp;
4using System;
5using System.Net.Security;
6using System.Security.Cryptography.X509Certificates;
7
8class Sample
9{
10    [STAThread]
11    static void Main(string[] args)
12    {
13        // If using Professional version, put your serial key below.
14        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
15
16        using (SmtpClient smtp = new SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)"))
17        {
18            smtp.Connect();
19            Console.WriteLine("Connected.");
20
21            smtp.Authenticate("<USERNAME>", "<PASSWORD>");
22            Console.WriteLine("Authenticated.");
23
24            // Create new message
25            MailMessage message = new MailMessage(new MailAddress("sender@example.com"),
26                                                  new MailAddress("receiver@example.com"));
27            message.Subject = "SMTP test message";
28            message.BodyText = "This is test message body.";
29
30            Console.WriteLine("Sending message...");
31
32            // Send message
33            smtp.SendMessage(message);
34
35            Console.WriteLine("Message sent.");
36        }
37    }
38}
1Imports System.Net.Security
2Imports System.Security.Cryptography.X509Certificates
3Imports GemBox.Email
4Imports GemBox.Email.Smtp
5
6Module Samples
7
8    Sub Main()
9
10        ' If using Professional version, put your serial key below.
11        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
12
13        Using smtp As New SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)")
14
15            smtp.Connect()
16            Console.WriteLine("Connected.")
17
18            smtp.Authenticate("<USERNAME>", "<PASSWORD>")
19            Console.WriteLine("Authenticated.")
20
21            ' Create New message
22            Dim message As New MailMessage(New MailAddress("sender@example.com"),
23                                           New MailAddress("receiver@example.com"))
24            message.Subject = "SMTP test message"
25            message.BodyText = "This is test message body."
26
27            Console.WriteLine("Sending message...")
28
29            ' Send message
30            smtp.SendMessage(message)
31
32            Console.WriteLine("Message sent.")
33
34        End Using
35
36    End Sub
37
38End Module

Check next sample or find out more about GemBox.Email and GemBox Software.