Conversion from RTF to HTML when sending an eMail

I’m creating a windows application and want to send an email using the SmtpClient composed in a Rich Text Box. However my two options in the MailMessage class are HtmlBody and TextBody, Is it capable of converting the Rich Text Format to HTML or sending the email in RTF?

Hi Steven,

Thank you for considering Aspose.Email and we are sorry for a delayed response.

The MailMessage class doesn’t have the capability of converting the Rich Text Format to HTML automatically. However, you can load the contents of the RTF in MapiMessage and interpret it to MailMessage object, which will automatically convert the RTF body to HTML as shown in the following code sample and email can be sent to preserve the formatting.

Sample Code:

string dir = "EMAIL_513747\\";
string path = @"test.rtf";

// Get the contents of the RTF file.
string rtfText = System.IO.File.ReadAllText(dir + path);

// Display the RTF text. This should look like the contents of your file.
MapiMessage mapi = new MapiMessage();
mapi.BodyRtf = rtfText;

MailMessageInterpretor mi = MailMessageInterpretorFactory.Instance.GetIntepretor(mapi.MessageClass);
MailMessage mailMsg = mi.Interpret(mapi);

// Save or send the MailMessage
mailMsg.Save(dir + "newHtml.msg", MailMessageSaveType.OutlookMessageFormat);
Hi, the code you gave me works well but with one problem. When the RTF has an embedded image, the image is lost during the process.

My Code:---------------------------------------------------------------------------------------
RTFMessage = MapiMessage.FromMailMessage(msg);
RTFMessage.BodyRtf = rtbMessage.Rtf;
MailMessageInterpretor mi = MailMessageInterpretorFactory.Instance.GetIntepretor(RTFMessage.MessageClass);
MailMessage mailMsg = mi.Interpret(RTFMessage);

//Create an instance of SmtpClient class
SmtpClient client = GetSmtpClient();

try
{
// Send the email
client.Send(mailMsg);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
--------------------------------------------------------------------------------------------------

Do you know why that is? And if there is a way to fix the problem so the image remains intact when converted to HTML and sent?

Hi Steven,


Aspose.Email can be used to send the RTF contents including images as email body. You may please give a try to the following sample code which demonstrates this feature in conjunction with Aspose.Words. RTF file is loaded into Document object which helps in converting the TRF to Mhtml. This mhtml stream is then used as mail message body as shown below:

Document wordDocument = new Document(SampleFile.rtf");
MemoryStream mhtmlStream = new MemoryStream();
wordDocument.Save(mhtmlStream, Aspose.Words.SaveFormat.Mhtml);

// Load the MHTML in MailMessage
mhtmlStream.Position = 0;
MailMessage message = MailMessage.Load(mhtmlStream, MessageFormat.Mht);
message.Subject = “Sending Invoice in Email”;
message.From = “sender@gmail.com”;
message.To = “recipient@gmail.com”;

// Save the message in MSG format to disk
message.Save(“test.msg”, MailMessageSaveType.OutlookMessageFormat);

For further details please refer to the online article here and feel free to write us back if you have any other query in this regard. We will be more than happy to provide you assistance.