C# 从 MVC 4 ASP.NET 应用程序发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18978702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:46:41  来源:igfitidea点击:

send email from MVC 4 ASP.NET app

c#asp.net-mvcemail

提问by Toxic

I am trying to setup simple but complete ASP.NET MVC 4 web app, where I can send email to specific address, I configure the web.config file for SMPT settings and code in controller call, but I am getting error message "The SMTP host was not specified"

我正在尝试设置简单但完整的 ASP.NET MVC 4 Web 应用程序,我可以在其中向特定地址发送电子邮件,我为 SMPT 设置和控制器调用中的代码配置了 web.config 文件,但我收到错误消息“SMTP未指定主机”

<system.net>
<mailSettings>
  <smtp deliveryMethod="Network" from="[email protected]">
    <network host="smtp.live.com" port="25" userName="[email protected]" password="myPassword" defaultCredentials="true"/>
  </smtp>
</mailSettings>

in controller class

在控制器类中

var mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;

var smptClient = new SmtpClient { EnableSsl = false };

smptClient.Send(mailMessage);

many thanks

非常感谢

回答by Hyman Hughes

From a quick look you haven't set up the "From" property.

乍一看,您还没有设置“From”属性。

var mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.From = new MailAddress("[email protected]");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;

回答by ali

do some thing like this

做这样的事情

SmtpClient smtp = new  SmtpClient(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtp], Convert.ToInt32(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtpport]));

if (ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtpUseCredentials] == "true")
{
    smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtpusername], ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtppassword], ConfigurationManager.AppSettings[EFloOnline.Model.Constants.smtp]);
}
else
{
    smtp.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

    if (SendTo.Count == 0)
    {
        SendTo.Add(ConfigurationManager.AppSettings[EFloOnline.Model.Constants.ToMail]);
    }

    foreach (string recipientemail in SendTo)
    {
        oEmail.To.Add(recipientemail);
        try
        {
            smtp.Send(oEmail);
        }
        catch (Exception)
        {
        }
        oEmail.To.Clear();
    }
}

回答by Joe

Your code and configuration look correct.

您的代码和配置看起来正确。

Are you sure you have put the system.net/mailSettings element in the web.config in the root directory of your web site?

您确定已将 system.net/mailSettings 元素放在网站根目录下的 web.config 中吗?

A common mistake is to put such settings in the web.config in the Views folder.

一个常见的错误是将此类设置放在 Views 文件夹中的 web.config 中。

Incidentally, the MailMessageclass implements IDisposable, as does the SmtpClientclass from .NET 4. So you should be enclosing both in usingblocks.

顺便说一下,MailMessage该类实现了IDisposable,就像SmtpClient.NET 4中的类一样。因此您应该将两者都包含在using块中。

回答by pateketu

回答by BernardG

I wrote a blog post about doing this. http://www.bgsoftfactory.net/5-steps-to-send-email-with-mvcmailer-from-an-mvc-4-0-application/

我写了一篇关于这样做的博客文章。 http://www.bgsoftfactory.net/5-steps-to-send-email-with-mvcmailer-from-an-mvc-4-0-application/

I took the easier way, using MVCMailer. Even if sending email from MVC is quite easy, it's a little more complicated to make it nice, while MVCMailer allow to use razor templates to format the body of your email.

我采用了更简单的方法,使用 MVCMailer。即使从 MVC 发送电子邮件很容易,但让它变得更好有点复杂,而 MVCMailer 允许使用剃刀模板来格式化您的电子邮件正文。

You may save yourself some time by using MVCMailer.

您可以通过使用 MVCMailer 为自己节省一些时间。

回答by johngrinder

Best Idea to use SMTP mail functionality in .NET + MVC/ASP is this open source codeplex library:

在 .NET + MVC/ASP 中使用 SMTP 邮件功能的最佳想法是这个开源代码库:

http://higlabo.codeplex.com/

http://higlabo.codeplex.com/

Especially since the default-delivered components in .NET framework does fully support all types of SSL/TSL etc. (implicit/explicit mode as keyword here)

特别是因为.NET框架中默认交付的组件确实完全支持所有类型的SSL/TSL等(隐式/显式模式作为关键字在这里)

http://higlabo.codeplex.com/

http://higlabo.codeplex.com/

回答by user2962144

you missing smptClient.Send(mailMessage); at the end of your code

你错过了 smptClient.Send(mailMessage); 在你的代码末尾

var mailMessage = new MailMessage();
mailMessage.To.Add("[email protected]");
mailMessage.From = new MailAddress("[email protected]");
mailMessage.Subject = "testing 2 ";
mailMessage.Body = "Hello Mr. Aderson";
mailMessage.IsBodyHtml = false;
//this what you miss
    smptClient.Send(mailMessage);
//