简体   繁体   中英

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();

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>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM