3

I need to create a new Outlook Mail Item using a pre existing oft template. This works fine, but my problem is when I need to then add text to the body of the template. when i attempt this, it writes over the template body.

The current template body consists if a table with three rows and I need to add user input to the middle row?

I followed the MSDN example to create the item but I'm unsure of how to do this next part.

Is it possible to add text to an oft template without writing over the top of it?

private void button1_Click(object sender, EventArgs e)
{
    Outlook.Application application = new Outlook.Application();


    Outlook.Folder f = 
        application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts) 
            as Outlook.Folder;

    Outlook.MailItem mail =
        application.CreateItemFromTemplate(@"C:\Documents and Settings\riversd\Desktop\DataBase\Notification.oft", f)
        as Outlook.MailItem;

    mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
    mail.Body = "<p>TEST</p>";
    mail.Save();
4

1 回答 1

5

MailItem.Body is a string data type. If you want to append text to the Body, you should use += instead of just =. Since you are using an HTML-formatted email (BodyFormat = olFormatHTML), you should be working with MailItem.HTMLBody which is an HTML-syntax string.

See MSDN section 17.3.1 for reference on how to add text to the Body property. Section 17.3.2 shows how to add text to HTMLBody property.

Text Email Example (BodyFormat = olFormatPlain)

mail.Body += "<p>TEST</p>";

HTML Email Example (BodyFormat = olFormatHTML)

mail.HTMLBody = mail.HTMLBody.Replace("</body>", "<p>TEST</p></body>");
于 2012-07-26T19:30:10.173 回答