2016-11-17 161 views
0

我试图发送电子邮件通知中的ASP.NET Web API。以下是发送使用SMTP电子邮件的Web API和ASMX服务

[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("[email protected]"); 
     mail.To.Add("[email protected]"); 
     mail.Subject = "You are subject"; 
     mail.IsBodyHtml = true; 
     mail.Body = "Hello My Dear User"; 


     SmtpServer.EnableSsl = true; 

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

    } 

但它会引发以下错误的代码:在ASMX服务(SOAP)和呼叫

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项目,放在相同的代码它使用SOAP客户端工具,它工作得很好..

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

结果: ASMX服务工作,但与上述错误信息WEB API错误的。

问题是为什么呢?我错过了什么? 我搜索四周,发现下面的配置可以帮助

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

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

任何一个面临这样的问题?

回答

0

试试这个

var mailMessage = new MailMessage(); 
mailMessage.From = new MailAddress("yourmail"); 
mailMessage.To.Add("[email protected]"); 
mailMessage.CC.Add("[email protected]"); 
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); 
+0

有什么区别??无论如何,我试过但不成功我得到了相同的错误... – venu

+0

我的工作正常....如果您使用GMAIL,请打开不太安全的应用程序 –

+0

我的工作正常,如果它在asmx Web服务中实施。但是如果在ASP.NET WEB API中实现相同,则不起作用。 – venu

相关问题