ASP MVC:发送电子邮件

4

我对.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.pnghttp://img153.imageshack.us/img153/695/capture2p.png。我已经找了几个小时,但似乎找不到解决方案。帮助将不胜感激!
3个回答

6

由于您正在调用

SmtpClient smtp = new SmtpClient("127.0.0.1"); 

本地主机应该有一个SMTP服务器。如果没有,则可以使用您的网络的邮件服务器。

为了测试目的,您可以使用以下内容:

<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服务器或类似的东西? - user323395
哦,还有一个问题。发送电子邮件的代码在类库中。我不认为它可以访问我的 web.config 中的设置? - user323395
如果是这样的话,你可以在代码后台中设置所有这些属性。 - Amsakanna

0

嗯,当您尝试将电子邮件发送到运行在127.0.0.1的SMTP服务时,那里实际上应该明显地运行一个来接受电子邮件,或者呢?

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

基本上:

  • 不要在那里设置中继SMTP服务。就是这样,别这么干。

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


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接