使用 C# 编辑 Outlook.MailItem 的 RTFBody
我正在尝试将字符串附加到传出的 Outlook.MailItem。在发送事件处理程序中,我有:
switch (mailItem.BodyFormat) {
case Outlook.OlBodyFormat.olFormatRichText:
byte[] mailItemBytes = mailItem.RTFBody as byte[];
System.Text.Encoding encoding = new System.Text.ASCIIEncoding();
string RTF = encoding.GetString(mailItemBytes);
RTF += "my string";
byte[] moreMailItemBytes = encoding.GetBytes(RTF);
mailItem.RTFBody = moreMailItemBytes;
break;
// ...
}
但收到的电子邮件不包含我的字符串。
I'm trying to append a string to an outgoing Outlook.MailItem. In the send event handler I have:
switch (mailItem.BodyFormat) {
case Outlook.OlBodyFormat.olFormatRichText:
byte[] mailItemBytes = mailItem.RTFBody as byte[];
System.Text.Encoding encoding = new System.Text.ASCIIEncoding();
string RTF = encoding.GetString(mailItemBytes);
RTF += "my string";
byte[] moreMailItemBytes = encoding.GetBytes(RTF);
mailItem.RTFBody = moreMailItemBytes;
break;
// ...
}
but the received email does not contain my string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这是旧的并且已经有绿色复选标记,但在搜索类似问题后,我发现一个页面对于如何使用 Word.Document 对象模型修改 Outlook 项目中的 RTF 正文提供了很好的答案。 https://www.add-in- express.com/forum/read.php?FID=5&TID=12738
基本上,您将文本视为 Word 文档,而忘记了使用 RTF。您需要首先将 Microsoft.Office.Interop.Word 的引用添加到您的项目中。
然后将 using 添加到您的项目中
,然后添加您的代码
I know this is old and has green check already but after searching for similar issues I found a page that gives a good answer for how to modify the RTF body in an outlook project using the Word.Document object model. https://www.add-in-express.com/forum/read.php?FID=5&TID=12738
Basically you treat the text a word doc and forget about working with RTF all together. You will need to add reference of Microsoft.Office.Interop.Word to your project first.
then add using to your project
then add your code
RTF 是一种非常复杂的格式文件,并且不会像连接字符串那么容易。您可以尝试使用 RichTextBox 控件并导入首先获取那里的数据,向其中添加文本,然后重新获取格式化的值。有点笨重,但比解析 RTF 文件容易得多。
作为替代方案,您也许能够找到一个解析并使用 RTF 的库,但这意味着您的应用程序的依赖项,并且很可能会在发行版中包含另一个 DLL。
RTF is a pretty elaborate format for files and isn't going to be as easy as concatenating a string. You may try using the RichTextBox control and importing the data there first, adding text to it, then re-grabbing the formatted value back. A bit clunky, but a lot easier than parsing an RTF file.
As an alternative, you may be able to find a library that parses and works with RTF, but that means a dependency for your application and most likely another DLL to include in the release.