2016-11-17 216 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

相關問題