Creating MSG Files with RTF body and attachment

2017-08-11 03:27:10 Written by  support iceblue
Rate this item
(0 votes)

Spire.Email supports to create Outlook message (MSG) files with rich text bodies and attachments in C# and VB.NET. The RTF body supports to set the formatting for the font, such as in bold, underline and set the color and add lists to the message body. It makes the outlook message vivid and clear to view. Here comes to the steps of how to use Spire.Email to create and save the Outlook message with RTF body and attachment in C#.

Step 1: Create an instance of MailMessage class and specify sender and recipient address.

MailAddress addressFrom = new MailAddress("daisy.zhang@e-iceblue.com", "Daisy Zhang");
MailAddress addressTo = new MailAddress("susanwong32@outlook.com");
MailMessage mail = new MailMessage(addressFrom, addressTo);

Step 2: Add the subject, message body and attachment of the message.

mail.Subject = "This is a test message";

string htmlString = @"
   <p>Dear Ms. Susan,</p>
   <p>This is an example of creating an <b>outlook message</b> <u>(msg)</u>.</p>
   <ul>
   <li> Create a message with RTF file </li>
   <li> Create a message with attachment</li>
   </ul>
   <p style='color:red'>This text is in red </p>
   
   <p>Best regards, </p>
   <p>Daisy </p>";  
 mail.BodyHtml = htmlString;
 
 mail.Attachments.Add(new Attachment("logo.png"));

Step 3: Save the message as MSG format.

mail.Save("Sample.msg", MailMessageFormat.Msg);

Effective screenshot:

Creating MSG Files with RTF body and attachment

Full codes:

using Spire.Email;

namespace CreatingMSGFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            MailAddress addressFrom = new MailAddress("daisy.zhang@e-iceblue.com", "Daisy Zhang");
            MailAddress addressTo = new MailAddress("susanwong32@outlook.com");

            MailMessage mail = new MailMessage(addressFrom, addressTo);

            mail.Subject = "This is a test message";
            string htmlString = @"
           <p>Dear Ms. Susan,</p>
           <p>This is an example of creating an <b>outlook message</b> <u>(msg)</u>.</p>
           <ul>
           <li> Create a message with RTF file </li>
           <li> Create a message with attachment</li>
           </ul>
           <p style='color:red'>This text is in red </p>
           <p>Best regards, </p>
           <p>Daisy </p>";

            mail.BodyHtml = htmlString;

            mail.Attachments.Add(new Attachment("logo.png"));

            mail.Save("Sample.msg", MailMessageFormat.Msg);
        }
    }
}

Additional Info

  • tutorial_title:
Last modified on Wednesday, 15 September 2021 03:31