【问题标题】:View/Download content into Outlook on client machine C#在客户端计算机 C# 上查看/下载内容到 Outlook
【发布时间】:2020-10-12 23:00:32
【问题描述】:

我有一个需求,我需要从 SQL DB 获取数据并将该内容查看/下载到 Microsoft Outlook。 DB Table 包含各种列,例如 Id、Content、Subject、ToAddress 和其他常规信息。此表中的内容列是 HTML 格式。

我在我的应用程序中使用 Angular 6 和 Wep API。

我尝试了多种方法,但都没有解决我的问题:

方法一:

                Outlook.Application outlookApp = new Outlook.Application();
                Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
                mailItem.Subject = message.Subject;
                mailItem.To = message.ToEmailAddress;
                mailItem.HTMLBody = message.Content;
                mailItem.Importance = Outlook.OlImportance.olImportanceLow;
                mailItem.Display(true);

为此,电子邮件在服务器计算机上打开,但不在客户端计算机上。

方法二:

mailto:stuff@things.com&subject=something&body=<doctype html><span style="color:red">Hi how are you</span>

我不能使用这种方法,因为它不处理电子邮件正文中的 HTML。我正在为电子邮件正文使用内容数据。

内容列中的示例数据:

<style type="text/css">
    html {
        font-family: Arimo, sans-serif, Calibri (Body);
    }

    body {
        font-family: Arimo, sans-serif, Calibri (Body);
    }


    label {
        font-weight: normal !important;
    }

    p, div {
        font-family: Arimo, sans-serif, Calibri (Body);
    }

    h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 {
        font-family: Arimo, sans-serif, Calibri (Body);
    }

    footer {
        font-family: Arimo, sans-serif, Calibri (Body);
    }
</style>

<html>
<body>
    <p>Hello</p>
    <p>Thank you for using the application</p>
    <p>
        https://www.google.com
    </p>
    <p>
        This is another paragraph 
        <br /><br />
        Thank you,
        <br />
        Your DEV Team
    </p>
</body>
<footer>
    <span style="font-size:x-small;font-style: italic;color:#2b568f">
        Note: Please do not reply to this email.
    </span>
</footer>
</html>

提前致谢

【问题讨论】:

  • 您无法通过 Web API c# 代码在客户端计算机上打开 Outlook。您需要从在客户端浏览器上运行的 Angular 执行此操作。您首先从 Web API 获取数据到 Angular,然后从 Angular 打开邮件客户端。 stackoverflow.com/questions/36553390/…
  • @ChetanRanpariya 是的,我也尝试过这种方法。问题是我的数据库中的数据以 HTML 格式存储,当我将数据分配给正文时,它被视为纯文本并在电子邮件正文中显示 HTML 代码。
  • 无法为 mailto 设置 html 正文。你可以在这里阅读stackoverflow.com/questions/5620324/mailto-link-with-html-body。您希望用户使用电子邮件正文打开电子邮件客户端,以便用户只需单击发送按钮即可发送电子邮件?
  • 是的,如果用户愿意,他们应该能够发送电子邮件。
  • hmm...正如我之前所说,为mailto 设置html 正文是不可能的。您可能希望提供在 html 页面中显示内容并允许用户复制它的功能,以便用户可以将其复制并粘贴到他们的电子邮件客户端并手动发送电子邮件...

标签: c# angular mime


【解决方案1】:

控制器端代码

            string[] headerArray = new string[]
            {
                    "To: " + message.ToEmailAddress,
                    "Subject: "+ message.Subject,
                    "From: " + message.ToEmailAddress,
                    "Reply-To: " + message.ToEmailAddress,
                    "Sender: " + message.ToEmailAddress,
                    "MIME-Version: 1.0",
                    "Content-Type: text/html; charset=ISO-8859-1",
                    "Date: "+ message.Modification.ModifiedDateTime
            };

            string headers = String.Join("\r\n", headerArray);
            string content = message.Content;
            string file = $"{headers}{content}";

            return File(System.Text.Encoding.ASCII.GetBytes(file), "text/html", 
            "MyTestEmail.eml");

我在控制器中使用锚标记调用了上面的代码,它自动将文件下载到客户端的机器上,重要的是它还处理 HTML

类型的 Content

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2011-02-06
    • 2014-01-07
    • 2012-04-29
    相关资源
    最近更新 更多