asp.net mvc框架,自动发送电子邮件 [英] asp.net mvc framework, automatically send e-mail
问题描述
我希望我的asp.net mvc框架系统每当某个动作(某个控制器内部)被触发时发送电子邮件。有没有任何第三方图书馆或.net标准的方法来实现这一点?
I want my asp.net mvc framework system to send an e-mail everytime a certain action (inside a certain controller) is fired off. Are there any third party libraries or .net standard ways to accomplish this?
推荐答案
更新的方法是使用 System.Net.Mail - 这是System.Web.Mail的2.0替换。
A more up to date method would be to use System.Net.Mail - this is the 2.0 replacement for System.Web.Mail.
这样的东西,从BaseController(如果有其他控制器需要这个)调用的实际控制器。
Something like this, called from either a BaseController (if there are other controllers that need this) the actual controller in question.
我在静态类中有以下代码来处理来自服务器的简单纯文本邮件:
I have the following code inside a static class to handle mailing simple plain text items from the server:
internal static void SendEmail(MailAddress fromAddress, MailAddress toAddress, string subject, string body)
{
var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
};
var client = new SmtpClient("smtpServerName");
client.Send(message);
}
显然,你可能想要一些错误处理等等 - 发送可以抛出异常,例如,如果服务器拒绝连接。
Obviously, you'd probably want some error handling etc in there - Send can throw an exception for example if the server is refusing connections.
这篇关于asp.net mvc框架,自动发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!