Save Email in C# and VB.NET

The following example shows how to create an email message with two body versions, text and HTML, and save it to selected file format.

Screenshot

Save Screenshot

See the full code below, use RUN EXAMPLE to execute.


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        // Create new message
20        MailMessage message = new MailMessage(new MailAddress("sender@example.com", "Sender"),
21                              new MailAddress("first.receiver@example.com", "First receiver"));
22
23        // Add second receiver to CC and set subject
24        message.Cc.Add(new MailAddress("carbon.copy@example.com", "Carbon Copy receiver"));
25        message.Subject = "GemBox.Email .NET component save example";
26
27        // Add HTML and text body
28        message.BodyHtml = "<html>" +
29                              "<body>" +
30                                 "<p>Hi!<br/><br/>This message was created and saved with " +
31                                    "<b>GemBox.Email .NET component</b>.<br/>" +
32                                    "More info can be found at <a href=\"http://www.gemboxsoftware.com/\">" +
33                                    "GemBox Software website</a>.<br/><br/>" +
34                                    "Regards,<br/>" + 
35                                    "GemBox" +
36                                 "</p>" +
37                              "</body>" +
38                           "</html>";
39
40        message.BodyText = "Hi!\r\n" +
41                           "\r\n" +
42                           "This message was created and saved with GemBox.Email .NET component.\r\n" +
43                           "More info can be found at http://www.gemboxsoftware.com/.\r\n\r\n" + 
44                           "Regards,\r\n" +
45                           "GemBox";
46
47        message.Save("Save.msg");
48    }
49}
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        ' Create new message
17        MailMessage message = new MailMessage(new MailAddress("sender@example.com", "Sender"),
18                              new MailAddress("first.receiver@example.com", "First receiver"))
19
20        ' Add second receiver to CC And set subject
21        message.Cc.Add(New MailAddress("carbon.copy@example.com", "Carbon Copy receiver"))
22        message.Subject = "GemBox.Email .NET component save example"
23
24        ' Add HTML And text body
25        message.BodyHtml = "<html>" &
26                              "<body>" &
27                                "<p>Hi!<br/><br/>This message was created and saved with " &
28                                  "<b>GemBox.Email .NET component</b>.<br/>" &
29                                  "More info can be found at <a href=""http://www.gemboxsoftware.com/"">" &
30                                  "GemBox Software website</a>.<br/><br/>" &
31                                  "Regards,<br/>" &
32                                  "GemBox" &
33                                "</p>" &
34                              "</body>" &
35                            "</html>"
36
37        message.BodyText = "Hi!" & vbCrLf &
38                           vbCrLf &
39                           "This message was created and saved with GemBox.Email .NET component." & vbCrLf &
40                           "More info can be found at http://www.gemboxsoftware.com/." & vbCrLf & vbCrLf &
41                           "Regards," & vbCrLf &
42                           "GemBox"
43
44        message.Save("Save.msg")
45
46    End Sub
47
48End Module

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