c# - 在客户端计算机 C# 上查看/下载内容到 Outlook
问题描述
我有一个要求,我需要从 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>
提前致谢
解决方案
控制器端代码
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类型的内容
推荐阅读
- return - 函数输出一个列表但返回 nil
- android - 如何通过wifi wampserver访问android上的localhost?
- api - Hyperwallet API,移除一个用户/一个转账方式
- reactjs - 对象数组无法从 material-ui 在 React 芯片上添加项目
- angular - 将组件匹配到动态组件的字符串
- json - 从 json 文档的文本文件恢复 MongoDB 集合
- r - R 在包本身中调用数据集
- node.js - MongoDB 是否有办法在不删除更新数据中不包含的现有元素的情况下更新文档
- javascript - 造型角垫选择条件
- python - 加载模板时,内置服务器的 Django 不断断开连接