asp.net mvc框架,自动发送电子邮件 [英] asp.net mvc framework, automatically send e-mail

查看:859
本文介绍了asp.net mvc框架,自动发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的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 - 这是2.0替换System.Web.Mail

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.

我有一个静态内部类以下code,以处理来自服务器的邮件简单的纯文本内容:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆