通过编码编辑MailItem.RTFBody

问题描述:

我使用UTF8Encoding从MailItem编码RTFbody已经取得了一些成功。我可以编写一封新邮件,完成所有新邮件,然后点击发送。点击发送后,我会在电子邮件中添加一个也添加到类别中的标签。这一切都可以通过RTFBbody工作。通过编码编辑MailItem.RTFBody

问题出现在我回复RTF电子邮件时,出于测试目的,这些电子邮件只是我发给我孤独自己的电子邮件。当我发送回复邮件并添加新标签时,我先删除旧标签,然后添加新标签。当我使用包含新标签的编辑字符串在回复电子邮件中设置RTFBody时,出现“内存不足或磁盘空间不足”错误。这不会发生,当我只是删除具有相同功能的标签。

贝娄是我使用的代码:

private void ChangeRTFBody(string replaceThis, string replaceWith) 
{ 
    byte[] rtfBytes = Globals.ThisAddIn.email.RTFBody as byte[]; 
    System.Text.Encoding encoding = new System.Text.UTF8Encoding(); 
    string rtfString = encoding.GetString(rtfBytes); 

    rtfString = rtfString.Replace(replaceThis, replaceWith); 

    rtfBytes = encoding.GetBytes(rtfString); 
    Globals.ThisAddIn.email.RTFBody = rtfBytes; < // The error is here only on 
                // reply and only when I replace 
                // with new tags 
} 

这些都是我打的电话:

删除旧标签:ChangeRTFBody(lastTag, "");

添加新的标签:ChangeRTFBody("}}\0", newTag + "}}\0");

像我说过,当我创建一封新电子邮件并发送时,它会起作用,但当我尝试回复同一封电子邮件时,则不起作用。似乎byte[]的大小在删除后几乎翻倍。当我在删除过程中检查它时,它的大小约为15k字节,当我在“添加”过程中检查时,它跳到了超过30k字节。当我尝试将新增充值的byte[]添加到rtfBody时出现错误。

感谢您的任何帮助和提示,并对所有阅读感到抱歉。

我有同样的问题,并且遇到了我认为通过使用Word.Document对象模型来替换Outlook rtf正文中的文本的更简单的方法。您需要首先将Microsoft.Office.Interop.Word的引用添加到您的项目中。

然后添加使用

using Word = Microsoft.Office.Interop.Word; 

那么你的代码看起来像

Word.Document doc = Inspector.WordEditor as Word.Document; 

//body text 
string text = doc.Content.Text; 

//find text location 
int textLocation = text.IndexOf(replaceThis); 

if(textLocation > -1){ 
    //get range 
    int textLocationEnd = textLocation + replaceThis.Length; 

    //init range 
    Word.Range myRange = doc.Range(textLocation , textLocationEnd); 

    //replace text 
    myRange.Text = replaceWith 
} 
+0

是的,我结束了使用WordEditor为好。编辑RTF让人头疼。 – Natzely 2017-05-02 15:05:45