将数据保存在 SQL Server 数据库中,然后发送电子邮件

发布于 2024-09-25 19:43:43 字数 238 浏览 1 评论 0原文

我有一个数据输入和编辑表单,在每个数据输入或更新事件中,我都必须向动态收件人列表发送电子邮件。我一直在用户单击保存或编辑按钮后立即发送电子邮件,但我正在考虑首先将数据保存到数据库,然后稍后发送电子邮件。我想这样做的部分原因是为了提高应用程序的响应时间,因为电子邮件发送往往需要比预期更长的时间。

有没有人做过一些与此相关的事情,是否有更好的方法来实现类似的事情,或者是否有人知道这方面的好教程。

电子邮件正文采用 html 格式。

I have a data entry and editing form and in every data entry or update event, I have to send an email to a dynamic list of recipients. I have been sending the email as soon as the user clicks the save or edit buttons but am thinking of first saving the data to the database, and then sending the email later. I want to do this partly to improve the response time of the application as the email sending tends to take a long time than desired.

Has any one done some thing some how related to this, is there a better way of implementing something similar or does one know a good tutorial on such.

The email body is html formatted.

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

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

发布评论

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

评论(3

鼻尖触碰 2024-10-02 19:43:44

您可以编写一个 Windows 服务来处理发送电子邮件,然后使用消息队列作为将数据从应用程序传递到服务的方法。即您的应用程序保存数据,然后将消息添加到队列中。该服务不断轮询队列中的消息,并将每条消息作为电子邮件发送。

You could write a Windows Service that handles sending your emails, then use a Message Queue as the method of passing data from your application to the service. I.e. your applicaiton saves the data, then adds a message to the Queue. The service continually polls the queue for messages, sending each one as an email.

我们只是彼此的过ke 2024-10-02 19:43:44

我同意 ck 关于使用服务和消息队列的观点,但还有一些替代方案。

一种是使用定期轮询数据库的服务。这可以让您避免以更高的 CPU 负载为代价的消息队列(该服务将执行许多不必要的数据库调用)。

您还可以使用数据库触发器或数据库中的计划作业直接在数据库中执行此操作。最新版本的 SQL Server 支持运行用 C# 或 Vb.Net 编写的存储过程,因此您可以在此处重用大部分现有代码。

最后,您可以寻求一个简单的解决方案,在 ASP.NET 应用程序中的单独线程上发送电子邮件。这样,您就不需要服务应用程序,并且可以或多或少地像现在一样重用您的代码。

I agree with ck about using a service and a message queue, but there are some alternatives.

One is to use a service that polls the database at a regular interval. This lets you avoid the message queue at the cost of a higher cpu load (the service will do many unnecessary database calls).

You could also do this directly in the database using either a database trigger or a scheduled job in the database. The latest versions of SQL Server supports running stored procedures written in C# or Vb.Net so you could probably reuse much of your existing code here.

Finally you could go for a simple solution where you do the email sending on a separate thread in your asp.net application. This way you avoid the need of a service application and you can reuse your code more or less as it is today.

孤蝉 2024-10-02 19:43:44

执行此操作的一种方法是写入数据库,然后将一条消息放入队列中,告诉电子邮件服务(编写为 Windows 服务)有电子邮件要发送。然后,电子邮件服务与数据库对话以查找它实际需要执行的操作。这将电子邮件服务与 Web 应用程序解耦,并且还避免了轮询。

这与 ck 的解决方案略有不同,因为队列消息用作触发器而不是包含电子邮件信息。这在某种程度上将 Web 应用程序和电子邮件服务解耦,意味着电子邮件服务可以由多个客户端重复使用,而无需每个客户端都遵守(并保持同步)相同的电子邮件消息格式。

One way to do this is write to the database, and then put a message on a queue that tells an email service (written as a Windows service) that there are emails to send. The email service then talks to the database to find what it actually needs to do. This decouples the email service from the web application and also avoids polling.

This is slightly different to ck's solution in that the queue message is used as a trigger rather than containing the email information. This decouples the web app and the email service to some extent, and means the email service can be reused by multiple clients without each client having to observe (and keep in step with) the same email message format.

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