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:
- Acceptable for any .Net application: ASP.NET, Windows Forms, WPF, Console, Web Service, SilverLight etc.
- Works at 32-bit and 64-bit Windows machines
- Works in Medium Trust level and shared hosting
- 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.
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);
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);
’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:”, “”)
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)
Max Sautin
If you have any questions regarding this article email me, info@sautinsoft.com
Share on Facebook