Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outlook Mail Item with OFT Template

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();
like image 944
Derek Avatar asked Dec 11 '25 12:12

Derek


1 Answers

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>");
like image 66
SliverNinja - MSFT Avatar answered Dec 14 '25 01:12

SliverNinja - MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!