How to create HTML email with embedded images from RTF and send it using Outlook in .NET C#


In this article you will find how to create a simple .NET/C# application which converts RTF document to HTML email with embedded images and sends it using MS Outlook programmatically.

Let’s make a C# code satisfying of these conditions:

  1. Acceptable for any .Net application: ASP.NET, Windows Forms, WPF, Console, Web Service, SilverLight etc.
  2. Works at 32-bit and 64-bit Windows machines.
  3. Compatible with .NET 2.0, 3.5 , 4.0, 4.5 frameworks and even higher if such will appear.

To make converting of RTF to HTML email with images at server-side we’ll use RTF-to-HTML DLL .Net library. It’s .Net component which will provides API to convert RTF to HTML with all images which we’ll get in a list after converting. We need these images extracted from RTF to further adding them into email as attachments.

To get the ball rolling, download the RTF-to-HTML DLL .Net component from here:

Download.

Further, please unpack the archive. We’ll need the file “SautinSoft.RtfToHtml.dll” from the package. Create a new C# Console Application named RtfToHtmlEmail. Next add references to the “SautinSoft.RtfToHtml.dll”, “Microsoft Outlook Library” and “Microsoft.CSharp” as shown on pictures below:


Reference to Microsoft.Outlook library

Microsoft.Outlook library

This is C# code of our application:


           using System;

using System.IO;
using System.Collections;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace RtfToHtmlEmail
{
    class Program
    {
        static void Main(string[] args)
        {
            // This application converts RTF file to HTML email with embedded images
            // and send the email using Outlook

            // Here we'll specify settings variables
            string pathToRtf = @"c:\Book.rtf";
            // We need this folder to store image attachments temporary.
            // Images will be deleted after sending the email.
            string pathToStoreTempAttachments = @"c:\temp";
            string from = "bob@bobsite.com";
            string to = "john@johnsite.com";
            string subject = "Testing message from Bob to John using Outlook";

            // 1. Convert RTF to HTML and place all images to list
            string rtf = File.ReadAllText(pathToRtf);
            SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();
            r.ImageStyle.IncludeImageInHtml = false;
            ArrayList imageList = new ArrayList();

            // 2. After launching this method we'll get our RTF document in HTML format
            // and list of all images.
            string html = r.ConvertString(rtf, imageList);

            // 3. Create an instance of MS Outlook
            //Type outlookApp = Type.GetTypeFromProgID("Outlook.Application");
            //object objOutlookApp = Activator.CreateInstance(outlookApp);
            Outlook.Application application = new Outlook.Application();

            // 4. Create a new MailItem and set the To, Subject, and Body properties.
            Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
            newMail.To = to;
            newMail.Subject = subject;
            newMail.HTMLBody = html;
            newMail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

            // 5. Retrieve the account that has the specific SMTP address.
            // Use this account to send the e-mail.
            Outlook.Account account = GetAccountForEmailAddress(application, from);

            // 6. Attach images to the email using cid
            // And send the email
            foreach (SautinSoft.RtfToHtml.SautinImage si in imageList)
            {
                string imageCid = si.Cid.Replace("cid:", "");
                string pathToImage = Path.Combine(pathToStoreTempAttachments, imageCid);
                si.Img.Save(Path.Combine(pathToImage));
                Outlook.Attachment attachment = newMail.Attachments.Add(pathToImage, Outlook.OlAttachmentType.olEmbeddeditem, null, imageCid);
                attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid);
                File.Delete(pathToImage);
            }

            newMail.SendUsingAccount = account;
            newMail.Send();
        }
        public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress)
        {

            // Loop over the Accounts collection of the current Outlook session.
            Outlook.Accounts accounts = application.Session.Accounts;
            foreach (Outlook.Account account in accounts)
            {
                // When the e-mail address matches, return the account.
                if (account.SmtpAddress == smtpAddress)
                {
                    return account;
                }
            }
            throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
        }

    }
}
                    

If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.