SautinSoft blog

Articles about converting HTML, PDF, Word, RTF, Excel, images using C# and VB.Net

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

without comments

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.
RTF to HTML DLL .Net provides API to convert RTF to HTML email in C#

RTF to HTML DLL .Net provides API to convert RTF to HTML email in C#

To get the ball rolling, download the RTF-to-HTML DLL .Net component from here: http://www.sautinsoft.com/components/rtftohtml_dll_net.zip.
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

Reference to Microsoft.Outlook library

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));
        }
 
    }
}
Dowload: RtfToHtmlEmailOutlook.cs.
I hope this article can be helpful to you. It is worth pointing out that, you may send HTML email without MS Oulook using standard .Net class SmtpClient.
 
Max Sautin (C# developer).
If you have any questions regarding this article email me, info@sautinsoft.com.
Share on Facebook

Written by Max Sautin

March 7th, 2014 at 8:45 am

Leave a Reply