Send Email in C#, VB.NET and ASP.NET

The following example shows how to create an email message with two body versions, text and HTML, and one image as an attachment using GemBox.Email library.

The example also shows how to send email message in C# and VB.NET using SmtpClient.

SmtpClient can also be used to send email messages from ASP.NET applications.

Screenshot

Send Screenshot

See the full code below.

1using GemBox.Email;
2using GemBox.Email.Imap;
3using GemBox.Email.Mime;
4using GemBox.Email.Pop;
5using GemBox.Email.Smtp;
6using System;
7using System.Collections.Generic;
8using System.Globalization;
9using System.Text;
10
11class Sample
12{
13    [STAThread]
14    static void Main(string[] args)
15    {
16        // If using Professional version, put your serial key below.
17        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
18
19        Console.WriteLine("Creating message...");
20
21        // Create new email message
22        MailMessage message = new MailMessage(new MailAddress("sender@example.com", "Sender"),
23                                              new MailAddress("first.receiver@example.com", "First receiver"));
24
25        // Add second receiver to CC and set subject
26        message.Cc.Add(new MailAddress("second.receiver@example.com", "Second receiver"));
27        message.Subject = "GemBox.Email .NET component";
28
29        // Add HTML and text body
30        message.BodyHtml = "<html>" +
31                              "<body>" +
32                                 "<p>Hi!<br/><br/>This message was created and sent with " +
33                                    "<b>GemBox.Email .NET component</b>.<br/>" +
34                                    "More info can be found at <a href=\"http://www.gemboxsoftware.com/\">" +
35                                    "GemBox Software website</a>." +
36                                 "</p>" +
37                              "</body>" +
38                           "</html>";
39
40        message.BodyText = "Hi!\r\n" +
41                           "\r\n" +
42                           "This message was created and sent with GemBox.Email .NET component.\r\n" +
43                           "More info can be found at http://www.gemboxsoftware.com/.";
44
45        // Add attachment
46        message.Attachments.Add(new Attachment("Picture.jpg"));
47
48        Console.WriteLine("Sending message...");
49
50        // Initialize new SMTP client and send an email message
51        using (SmtpClient smtp = new SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)"))
52        {
53            smtp.Connect();
54            Console.WriteLine("Connected.");
55
56            smtp.Authenticate("<USERNAME>", "<PASSWORD>");
57            Console.WriteLine("Authenticated.");
58
59            smtp.SendMessage(message);
60        }
61
62        Console.WriteLine("Message sent successfully.");
63    }
64}
1Imports System.Globalization
2Imports System.Text
3Imports GemBox.Email
4Imports GemBox.Email.Imap
5Imports GemBox.Email.Mime
6Imports GemBox.Email.Pop
7Imports GemBox.Email.Smtp
8
9Module Samples
10
11    Sub Main()
12
13        ' If using Professional version, put your serial key below.
14        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
15
16        Console.WriteLine("Creating message...")
17
18        ' Create new email message
19        Dim message As New MailMessage(New MailAddress("sender@example.com", "Sender"),
20                                       New MailAddress("first.receiver@example.com", "First receiver"))
21
22        ' Add second receiver to CC And set subject
23        message.Cc.Add(New MailAddress("second.receiver@example.com", "Second receiver"))
24        message.Subject = "GemBox.Email .NET component"
25
26        ' Add HTML And text body
27        message.BodyHtml = "<html>" &
28                              "<body>" &
29                                "<p>Hi!<br/><br/>This message was created and sent with " &
30                                  "<b>GemBox.Email .NET component</b>.<br/>" &
31                                  "More info can be found at <a href=""http://www.gemboxsoftware.com/"">" &
32                                  "GemBox Software website</a>." &
33                                "</p>" &
34                              "</body>" &
35                            "</html>"
36
37        message.BodyText = "Hi!" & vbCrLf &
38                           vbCrLf &
39                           "This message was created and sent with GemBox.Email .NET component." & vbCrLf &
40                           "More info can be found at http://www.gemboxsoftware.com/."
41
42        ' Add attachment
43        message.Attachments.Add(New Attachment("Picture.jpg"))
44
45        Console.WriteLine("Sending message...")
46
47        ' Initialize new SMTP client and send an email message
48        Using smtp As New SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)")
49            smtp.Connect()
50            Console.WriteLine("Connected.")
51
52            smtp.Authenticate("<USERNAME>", "<PASSWORD>")
53            Console.WriteLine("Authenticated.")
54
55            smtp.SendMessage(message)
56        End Using
57
58        Console.WriteLine("Message sent successfully.")
59
60    End Sub
61
62End Module

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