在Outlook邮件正文中将文本更改为粗体

时间:2018-04-30 14:27:52

标签: c# .net outlook

请帮我跟进。 我正致力于从模板创建和修改Outlook消息。我需要将一些文本更改为Bold。

Array
(
    [0] => Array
        (
            [group_id] => 1
            [group_name] => Home
            [module_id] => 1
            [mod_name] => Home
            [mod_link] => /home
            [var_name] => home
        )

    [1] => Array
        (
            [group_id] => 2
            [0] => Array
                (
                    [module_id] => 2
                    [mod_name] => Numbers
                    [mod_link] => /number_mapping
                    [var_name] => numbermap
                )

            [1] => Array
                (
                    [module_id] => 70
                    [mod_name] => DR Plan
                    [mod_link] => /dr_plan
                    [var_name] => dr_plan
                )

        )

    [2] => Array
        (
            [group_id] => 3
            [0] => Array
                (
                    [module_id] => 8
                    [mod_name] => Reporting
                    [mod_link] => /reporting
                    [var_name] => reporting
                )

            [1] => Array
                (
                    [module_id] => 80
                    [mod_name] => Scheduler
                    [mod_link] => /scheduler
                    [var_name] => scheduler
                )

        )

)

Screenshot with breakpoint
Mail Display Result

2 个答案:

答案 0 :(得分:3)

如果您想在电子邮件中使用HTML,则需要设置HTMLBody属性而不是Body

foreach (XmlNode node in nodeList)
{
    string CustomerName = node.SelectSingleNode("CustomerName").InnerText;

    string ReportName = node.SelectSingleNode("ReportName").InnerText + ".pdf";

    Outlook.Application mailApplication = new Outlook.Application();

    Outlook.MailItem mail = mailApplication.CreateItemFromTemplate(@"d:\Friday Report\#TEMPLATES\template.oft") as Outlook.MailItem;
    mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
    mail.Attachments.Add(@"d:\Friday Report\" + ReportName);
    mail.Subject = "Application Packaging – Weekly Summary";
    CustomerName = "<b>" + CustomerName + "</b> ";
    string body = mail.Body;
    string new_body = body.Replace("CustomerName", CustomerName );
    mail.HTMLBody = new_body;
    mail.Display(true);
    mail.Close(Outlook.OlInspectorClose.olDiscard);
}

但是,您应该使用有效的HTML,将邮件用<html><body>{your message}</body></html>

包围

答案 1 :(得分:0)

这似乎有效(见代码下面的屏幕截图)

using Microsoft.Office.Interop.Outlook;
using outlookApp = Microsoft.Office.Interop.Outlook;

namespace z_Console_Scratch
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();

            Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)outlookApp.CreateItem(OlItemType.olMailItem);

            mailItem.Subject = "test subject";
            mailItem.HTMLBody = "<html><body>This is the <strong>funky</strong> message body</body></html>";
            mailItem.Display(false);

        }


    }

}

注意:这也有效:mailItem.HTMLBody = "<html><body>This is the <b>funky</b> message body</body></html>";

屏幕截图

image of email body showing bolded text