Exception when setting rtf body

Hi,

This code results in a System.ArgumentOutOfRangeException.

var temp = new MapiMessage();
temp.SetBodyRtf("asdf", true);

image.png (22.6 KB)

@weswes

The System.ArgumentOutOfRangeException you are encountering when setting the RTF body in the MapiMessage class may be due to the RTF string being improperly formatted or not meeting the expected requirements.

To resolve this issue, ensure that the RTF string you are passing to the SetBodyRtf method is a valid RTF format. Here’s a minimal example of how to set a simple RTF body correctly:

var temp = new MapiMessage();
string rtfBody = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Riched20 10.0.18362;}viewkind4\uc1 
\pard\fs22 This is a sample RTF body.\par
}";
temp.SetBodyRtf(rtfBody, true);

Ensure that your RTF string starts with the correct RTF header and is properly formatted. If the issue persists, check the content of the RTF string for any errors or unsupported features. If you are working with a plain text file instead of RTF, consider using a different approach to handle the text content without relying on RTF formatting.

Okay. I should have seen that in the docs.

However, a nicer exception message would be nice – “Invalid RTF” or something like that.

@weswes,

You’re right, a clearer exception message, such as “Invalid RTF” would make the behavior much more intuitive.

Thank you.