2012-01-06 212 views
0

我的MVC Web应用程序时,新用户获取与下面的代码创建发送电子邮件:asp.net的MVC发送电子邮件错误:无法发送电子邮件

private static void SendMail(User user) 
     { 
      string ActivationLink = "http://localhost/Account/Activate/" + 
            user.UserName + "/" + user.NewEmailKey; 

      var message = new MailMessage("[email protected]", user.Email) 
      { 
       Subject = "Activate your account", 
       Body = ActivationLink 
      }; 

      var client = new SmtpClient("localhost"); 
      client.UseDefaultCredentials = false; 
      client.Send(message); 
     } 

这有什么错我的代码,请告诉我。

ERROR : Failure sending mail. {"Unable to connect to the remote server"}

SMTP配置: enter image description here

+0

检查您的邮件服务器。 – SLaks 2012-01-06 14:18:01

+0

你会得到什么错误?你的stmp服务器如何配置? – 2012-01-06 14:18:09

+0

垃圾邮件收件箱中的任何东西? – 2012-01-06 14:18:29

回答

1

下面是此错误的可能原因:

1您没有提供正确的认证细节

2所述的端口被阻塞,例如由防火墙

在你的例子中,我注意到你没有指定端口,当y ou创建你的SmtpClient - 它可能有助于指定它。

1

Gmail在端口587打开,您需要启用SSL。

请尝试下面的代码。

 var smtp = new SmtpClient 
     { 
      Host = "smtp.gmail.com", 
      Port = 587, 
      EnableSsl = true, 
      DeliveryMethod = SmtpDeliveryMethod.Network, 
      UseDefaultCredentials = false, 
      Credentials = new NetworkCredential(<fromAddress>, <fromPassword>) 
     }; 
     using (var message = new MailMessage(<fromAddress>, <toAddress>) 
     { 
      Subject = <subject>, 
      Body = <body> 
     }) 
     { 
      smtp.Send(message); 
     }