Allows to operate with images in memory during HTML to RTF and vice versa conversion

Namespace:  SautinSoft
Assembly:  SautinSoft.RtfToHtml (in SautinSoft.RtfToHtml.dll) Version: 6.2.2.1

Syntax

      
 C#  Visual Basic 
public class SautinImage : IDisposable
Public Class SautinImage _
	Implements IDisposable

Members

         
 All Members  Properties   Methods  
 Public

 Protected
 Instance

 Static 
 Declared

 Inherited
 XNA Framework Only 

 .NET Compact Framework Only 

 MemberDescription
Cid
Image Cid
Dispose()()()
Releases the property Img from memory.
Finalize()()() (Overrides Object..::.Finalize()()().)
Img
Object of the Image standard class

Remarks

Examples

CopyHow to create HTML email with images from RTF and send using Outlook in C#
using System;
using System.IO;
using System.Collections.Generic;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Sample
{
    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 = Path.GetFullPath(@"..\..\..\..\..\test.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;
            List<SautinSoft.RtfToHtml.SautinImage> imageList = new List<SautinSoft.RtfToHtml.SautinImage>();

            // 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 pathToImage = Path.Combine(pathToStoreTempAttachments, si.Cid);
                si.Img.Save(pathToImage);
                Outlook.Attachment attachment = newMail.Attachments.Add(pathToImage, Outlook.OlAttachmentType.olEmbeddeditem, null, si.Cid);
                attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", si.Cid);
                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));
        }

    }
}
CopyHow to create HTML email with images from RTF and send using Outlook in VB.Net
Imports System.IO
Imports System.Text
Imports Outlook = Microsoft.Office.Interop.Outlook

Namespace SampleConvert
    Friend Class Class1
        Shared Sub Main(ByVal args() As String)
            ' This application converts RTF file to HTML email with embedded images
            ' and send the email using Outlook


            ' Here we'll specify settings variables
            Dim pathToRtf As String = Path.GetFullPath("..\..\..\..\..\test.rtf")

            ' We need this folder to store image attachments temporary.
            ' Images will be deleted after sending the email.
            Dim pathToStoreTempAttachments As String = "c:\temp"
            Dim [from] As String = "bob@bobsite.com"
            Dim [to] As String = "john@johnsite.com"
            Dim subject As String = "Testing message from Bob to John using Outlook"

            ' 1. Convert RTF to HTML and place all images to list
            Dim rtf As String = File.ReadAllText(pathToRtf)
            Dim r As New SautinSoft.RtfToHtml()
            r.ImageStyle.IncludeImageInHtml = False
            Dim imageList As New List(Of SautinSoft.RtfToHtml.SautinImage)()

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

            ' 3. Create an instance of MS Outlook            
            'Type outlookApp = Type.GetTypeFromProgID("Outlook.Application");
            'object objOutlookApp = Activator.CreateInstance(outlookApp);
            Dim application As New Outlook.Application()

            ' 4. Create a new MailItem and set the To, Subject, and Body properties.
            Dim newMail As Outlook.MailItem = CType(application.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
            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.            
            Dim account As Outlook.Account = GetAccountForEmailAddress(application, [from])


            ' 6. Attach images to the email using cid
            ' And send the email
            For Each si As SautinSoft.RtfToHtml.SautinImage In imageList
                Dim pathToImage As String = Path.Combine(pathToStoreTempAttachments, si.Cid)
                si.Img.Save(pathToImage)
                Dim attachment As Outlook.Attachment = newMail.Attachments.Add(pathToImage, Outlook.OlAttachmentType.olEmbeddeditem, Nothing, si.Cid)
                attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", si.Cid)
                File.Delete(pathToImage)
            Next si

            newMail.SendUsingAccount = account
            newMail.Send()
        End Sub
        Public Shared Function GetAccountForEmailAddress(ByVal application As Outlook.Application, ByVal smtpAddress As String) As Outlook.Account

            ' Loop over the Accounts collection of the current Outlook session.
            Dim accounts As Outlook.Accounts = application.Session.Accounts
            For Each account As Outlook.Account In accounts
                ' When the e-mail address matches, return the account.
                If account.SmtpAddress = smtpAddress Then
                    Return account
                End If
            Next account
            Throw New System.Exception(String.Format("No Account with SmtpAddress: {0} exists!", smtpAddress))
        End Function
    End Class
End Namespace

Inheritance Hierarchy

System..::.Object
  SautinSoft..::.RtfToHtml..::.SautinImage

See Also