在 .Net 中发送格式化 (HTML) 电子邮件的方法

发布于 2024-10-09 11:30:05 字数 425 浏览 0 评论 0原文

网站向您发送各种电子邮件消息是很常见的:帐户激活、密码重置等。

过去,我构建了一个系统,在 XmlDocument 中生成 xml 标记,然后使用 < code>XslCompliedTransform 将 xml 转换为 html 电子邮件。这样做的缺点是 XSLT 既不有趣也不容易改变(至少对我来说)。

最近,我借鉴了一位过去同事的想法,在 SSRS 中使用数据驱动订阅以及数据库表作为消息队列。数据驱动器订阅每 10 分钟运行一次,并发送队列中的所有电子邮件,这些电子邮件是作为 WebArchive 发送的报告,以便它们呈现在电子邮件中,而不是作为附件发送。这很有效,但感觉有点矫枉过正。我看到的好处是,有人可以在 BIDS 中打开报表并更改布局,而无需拥有太多的编程经验。

你们通过哪些方式从应用程序向用户发送 html 格式的电子邮件?

It is common for websites to send you various e-mail messages: Account activation, password reset, etc.

In the past, I built a system where I generate xml markup in an XmlDocument and then use an XslCompliedTransform to convert the xml into an html e-mail. The downside to this is that XSLT's are neither fun nor easy to alter (at least for me).

Recently, I borrowed an idea from a past co-worker where I use data driven subscriptions in SSRS along with a database table as a message queue. The data drive subscription runs every 10 minutes and sends out all the e-mails in the queue, which are reports sent as WebArchive so that they are rendered in the e-mail message and not sent as an attachment. This works great, but feels a little like overkill. The benefit I see is that someone can open the report in BIDS and change the layout without having to have too much programming experience.

What are some ways you guys have sent html formatted e-mails out to users from your applications?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白色秋天 2024-10-16 11:30:05

这对于你正在做的事情来说可能有点过分了,但我通常解决这个问题的方法是为我想要在数据库中发送的每封电子邮件存储 html 和文本模板。模板具有动态数据的替换代码。前任:

<--Replacement.FirstName-->

然后,在负责发送电子邮件的 .net 服务中,我获取模板,并获取需要发送并进行必要替换的排队电子邮件的值。最后,我使用 RegEx.replace 删除任何剩余的替换代码(如果有)。

对于电子邮件的实际发送,我使用 System.Net.Mail 命名空间,因为它允许您发送多部分消息,因此如果最终用户的客户端仅支持纯文本,他们不会看到我的所有 html 标签,因为我发送了电子邮件的文本和 html 版本。

尽管它不是最优雅的解决方案,但我喜欢它,因为我通常会制作一个很好的前端来管理数据库中的 html/文本模板,以便电子邮件设计者可以管理这些模板。此外,我通常会列出可用的替换代码,以便他们只需单击按钮即可将相关替换代码添加到模板中。

This might be overkill for what you are doing but how I generally solve this issue is to store html and text templates for each of the emails I want to send out in the database. The templates have replacement codes for the dynamic data. ex:

<--Replacement.FirstName-->

Then in the .net service responsible for sending out my emails I get the templates and I get the values for the queued emails that need to go out and make the necessary replacements. At the end I use a RegEx.replace to strip out any remaining replacement codes (if any).

For the actual sending of the emails I use the System.Net.Mail namespace as it allows you to send multi-part messages so if the end user's client only supports plain text they don't see all my html tags since I send out a text and html version of the email.

Even though it isn't the most elegant solution I like it because I generally make a nice front end to manage the html/text templates in the database so that the email designers can manage these. Also I generally list the available replacement codes so they can just click a button to add the relevant replacement code to the template.

莫多说 2024-10-16 11:30:05

您可能也想研究使用此类:

http ://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.maildefinition.aspx

您可以传递带有键/值的字典。

它与 .NET 登录控件(如 ForgotPassword)使用的相同。

这里有一些代码示例:

http://www.go4answers.com/Example/systemnetmail-html-template-problem-88358.aspx

You might want to investigate using this class too:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.maildefinition.aspx

You can pass a dictionary with key / values.

It's the same used by the .NET Login Controls (like ForgotPassword)

There is some code examples here:

http://www.go4answers.com/Example/systemnetmail-html-template-problem-88358.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文