SautinSoft blog

Articles about converting between HTML, PDF, Word, RTF, Excel, PPT, Images with c# and vb.Net examples!

How to create HTML email with embedded images from RTF and send using Outlook.

 Text, RTF to HTML  Add comments
Feb 022011
 

How to create HTML email with embedded images from RTF in .Net

In this article I’ll show how to create a simple .Net application which creates and sends HTML email with embedded images from any RTF document.

Let’s make that code will satisfy 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. Works in Medium Trust level and shared hosting
  4. Compatible with .NET 1.1, 2.0, 3.5 , 4.0 frameworks and even higher if such will appear
To make converting of RTF to HTML email with images at server-side we’ll use the RTF-to-HTML DLL .Net, it’s completely satisfies all requirements placed above.
Download the RTF-to-HTML DLL .Net

Download the evaluation version:
http://www.sautinsoft.com/components/rtftohtml_dll_net.zip


Let’s create C# sample:

We’ll think that we already have RTF document with images stored in the variable
‘rtf’ as string.
//1. Start converting rtf to html
string rtf = ByteToString(FileUpload1.FileBytes);
SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();
r.ImageStyle.IncludeImageInHtml = false;
System.Collections.ArrayList arListWithImages = new System.Collections.ArrayList();

//2. After launching this method we’ll get our RTF document in HTML format and list of all images in the Arraylist ‘arListWithImages’
string html = r.ConvertString(rtf, arListWithImages);
//3. Create HTML email
string from = “from@from.com”;
string to = “to@to.com”;
string subject = “Simple message”;

MailMessage
emailMessage = new MailMessage();
emailMessage.From = newMailAddress (from);
emailMessage.To.Add(to);
emailMessage.Subject = subject.Replace(“\r\n”, “”);

//4. Attach images to email
System.Net.Mail.AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, “text/html”);

foreach
(SautinSoft.SautinImage simg in arListWithImages)
{
if (simg.Img != null)
{
LinkedResource lr = null;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
simg.Img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
if (ms != null && ms.Position > 0)
ms.Position = 0;
lr = newLinkedResource(ms);
lr.ContentId = simg.Cid.Replace(“cid:”, “”);
altView.LinkedResources.Add(lr);
}
}
emailMessage.AlternateViews.Add(altView);
//5. Send the message using email account
SmtpClient client = newSmtpClient(“127.0.0.1″);

System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
client.Credentials = credentials;
client.UseDefaultCredentials = true;
client.Send(emailMessage);

Download .cs file (1 Kb)


Let’s create VB.Net sample:
’1. Start converting rtf to html
Dim rtf AsString = ByteToString(FileUpload1.FileBytes)
Dim r AsNew SautinSoft.RtfToHtml()
r.ImageStyle.IncludeImageInHtml = False
Dim arListWithImages AsNew System.Collections.ArrayList()

’2. After launching this method we’ll get our RTF document in HTML format and list of all images in the Arraylist ‘arListWithImages’
Dim html AsString = r.ConvertString(rtf, arListWithImages)
’3. Create HTML email
Dim [from] AsString = “from@from.com”
Dim [to] AsString = “to@to.com”
Dim subject AsString = “Simple message”
Dim emailMessage AsNew MailMessage()
emailMessage.From = New MailAddress([from])
emailMessage.To.Add([to])
emailMessage.Subject = subject.Replace(vbCrLf, “”)

’4. Attach images to email
Dim altView As System.Net.Mail.AlternateView = AlternateView.CreateAlternateViewFromString(html, Nothing, “text/html”)

ForEach simg As SautinSoft.SautinImage In arListWithImages
If simg.Img IsNotNothingThen
Dim lr As LinkedResource = Nothing
Dim ms AsNew System.IO.MemoryStream()
simg.Img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
If ms IsNot NothingAndAlso ms.Position > 0 Then
ms.Position = 0
EndIf
lr = New LinkedResource(ms)
lr.ContentId = simg.Cid.Replace(
“cid:”, “”)
altView.LinkedResources.Add(lr)
EndIf
Next simg
emailMessage.AlternateViews.Add(altView)
’5. Send the message using email account
Dim client AsNew SmtpClient(“127.0.0.1″)
Dim credentials AsNew System.Net.NetworkCredential()
client.Credentials = credentials
client.UseDefaultCredentials = True
client.Send(emailMessage)

Download .vb file (1 Kb)
Max Sautin
If you have any questions regarding this article email me, info@sautinsoft.com
Share on Facebook
 Posted by Max Sautin at 7:01 am  Tagged with: emded images in outlook mail, html email from rtf, rtf to email with images, rtf to html

 Leave a Reply Cancel reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

  How to convert RTF to HTML and vice versa in C#!?

Follow Me

RSS Twitter Facebook

Categories

  • Articles about document conversion (9)
  • Life and work (4)
  • Using and adjusting the SautinSoft components (21)
    • ASPX, Images to PDF (2)
    • Excel to PDF (1)
    • HTML to Images (1)
    • HTML to Word (2)
    • HTML, RTF to PDF (2)
    • PDF to Word, Images (5)
    • Text, RTF to HTML (3)
    • Word, Excel, Ppoint, Access to PDF (5)

Twitter

  • Our company glad to inform about the release new version 2.2.1.23 of "PDF Focus .Net" ! http://t.co/Jr3xkFFZ 1 year ago
  • How to convert HTML to Image using http://t.co/bB2VdKmU http://t.co/CfwdxH06 1 year ago
  • "Converting of MS Word reports and Excel spreadsheets to PDF format in C#" Read the article from our customer: http://t.co/tgrSgzYn 1 year ago
  • "How to convert ASPX page with graphic objects to PDF" Read the article from our customer: http://t.co/Q3VNQ8DN 1 year ago
  • "Converting RTF to HTML under Windows Azure" Read the article from our customer: http://t.co/1zc0kDE8 1 year ago

Articles about document conversion

  • We are very happy to offer the new update PDF Focus .Net 2.2.1.23!
  • «UseOffice. Net 3.5.7» – a modern component to accurately convert popular office formats.
  • Wow… Our components for converting “HTML to RTF” are FREE now!
  • Wow… Our components for converting “RTF to HTML” are FREE now!
  • How to convert Excel to PDF in .Net?

Recent Comments

    © 2011 HTML, RTF, Excel, PDF and Word: articles about different ways to convert, export, merge and filter documents for Developers! Suffusion theme by Sayontan Sinha