在web api vs asmx服务中使用SMTP发送电子邮件

时间:2016-11-17 23:10:43

标签: asp.net asp.net-mvc asp.net-web-api asp.net-mvc-5 asmx

我正在尝试在ASP.NET Web API中发送通知电子邮件。以下是代码

    [HttpPost]
    [Route("api/UpdateUser")]
    public Foo UpdateUser(Foo vm)
    { 
       /* some data base operations here....
       ......
       ......
             */
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("<SMTP Server>", 587);

        mail.From = new MailAddress("test@test.com");
        mail.To.Add("toemail@test.com");
        mail.Subject = "You are subject";
        mail.IsBodyHtml = true;
        mail.Body = "Hello My Dear User";


        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);
        Foo newVM = new Foo();

    }

但它会引发以下错误:

System.Net.Mail.SmtpException: Failure sending mail. 
---> System.Net.WebException: Unable to connect to the remote server 
---> System.Net.Sockets.SocketException: A connection attempt failed because the connected 
party did not properly respond after a period of time, or established connection failed 
because connected host has failed to respond <server IP>:587 
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) 
--- End of inner exception stack trace 
--- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) 
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) 
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) 
at System.Net.Mail.SmtpClient.GetConnection() 
at System.Net.Mail.SmtpClient.Send(MailMessage message) 
--- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) 

现在,我创建了一个全新的asp.net Web项目并将相同的代码放在asmx服务(SOAP)中,并使用soap客户端工具调用它,它工作得很好..

我在同一个(测试环境)服务器上的两个独立的IIS应用程序中部署,唯一的区别是web.api是 ASP.NET MVC 应用程序的一部分,其中asmx服务部分是 asp.net web 应用程序。

结果: asmx服务有效,但WEB API错误输出上面的错误消息。

问题是为什么?我错过了什么? 我四处搜索,发现以下配置可以提供帮助

  <system.net>
<defaultProxy>
  <proxy usesystemdefault="False"/>
</defaultProxy>

:(但不幸的是,在我的情况下,它没有帮助......

有人遇到过这样的问题吗?

1 个答案:

答案 0 :(得分:0)

试试这个

var mailMessage = new MailMessage();
mailMessage.From = new MailAddress("yourmail");
mailMessage.To.Add("akash073@waltonbd.com");
mailMessage.CC.Add("akash073@gmail.com");
mailMessage.Subject = "Test";
mailMessage.Body = "Test";
var smtp = new SmtpClient();
smtp.Host = "your host";
smtp.Port = 25;//your port
smtp.Credentials = new System.Net.NetworkCredential("userName", "password");
smtp.Send(mailMessage);