【问题标题】:ASP MVC: Sending an E-mailASP MVC:发送电子邮件
【发布时间】:2011-02-14 05:01:40
【问题描述】:

我是 .NET 平台的新手。目前我正在学习 ASP.NET MVC。

我想从我的程序中发送一封电子邮件,我有以下代码:

public void sendVerrificationEmail()
    {
        //create the mail message
        MailMessage mail = new MailMessage();

        //set the addresses
        mail.From = new MailAddress("");
        mail.To.Add("");

        //set the content
        mail.Subject = "This is an email";
        mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>";
        mail.IsBodyHtml = true;

        //send the message
        SmtpClient smtp = new SmtpClient("127.0.0.1");
        smtp.Send(mail);
    }

现在当我执行这段代码时,我会得到以下异常:

System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:25

现在我对 IIS 管理器和其他东西非常陌生。所以那里可能有问题。

我是否需要安装虚拟 SMTP 服务器或其他东西?目前我有以下设置:

http://img153.imageshack.us/img153/695/capture2p.png

我已经找了几个小时了,但似乎找不到可行的解决方案。

我们将不胜感激!

【问题讨论】:

    标签: c# asp.net asp.net-mvc email iis-7


    【解决方案1】:

    如你所说

    SmtpClient smtp = new SmtpClient("127.0.0.1"); 
    

    本地主机上应该有一个 SMTP 服务器。如果它不存在,那么您可以使用网络的 MailServer。

    出于测试目的,您可以使用

    <system.net>
    <mailSettings>
          <smtp from="Test@test.com" deliveryMethod="SpecifiedPickupDirectory">
            <network host="127.0.0.1" port="25" userName="userID" password="*****" defaultCredentials="true" />
            <specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\mail\"/>
          </smtp>
        </mailSettings>
      </system.net>
    

    这会将您的电子邮件保存在 C:\temp\mail 中而不发送。

    【讨论】:

    • 好的,谢谢,Visual Studio 2010 中是否包含 SMTP 服务器?
    • 哦,还有一个问题。发送电子邮件的代码位于类库中。我认为它无法访问我的 web.config 中的设置?
    • 如果是这样,您可以在代码隐藏中设置所有这些属性。
    【解决方案2】:

    好吧,当您尝试将电子邮件发送到在 127.0.0.1 上运行的 SMTP 服务时 - 实际上显然应该在那里运行一个来接受电子邮件,或者 ;)?

    与 IIS 管理器无关 - 简单的常识就足够了。

    基本上:

    • 不要在那里设置中继 smtp 服务。只是不要这样做。

    • 在 IIS 配置中配置 smtp 服务 - 这样它会进入您的 web.config,并且不会在您的应用程序的很多地方进行硬编码。

    【讨论】:

      【解决方案3】:

      我通常通过 GMail 的 SMTP 服务从 localhost 发送邮件。当项目上传到我的网络主机时,我有另一个配置。

      这是一个例子: http://www.shabdar.org/send-email-using-gmail-account-asp-net-csharp.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-11
        • 1970-01-01
        • 2017-11-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-17
        • 1970-01-01
        相关资源
        最近更新 更多