在 web api 与 asmx 服务中使用 SMTP 发送电子邮件
Posted
技术标签:
【中文标题】在 web api 与 asmx 服务中使用 SMTP 发送电子邮件【英文标题】:Send Email using SMTP in web api vs asmx service 【发布时间】:2017-04-01 16:37:24 【问题描述】:我正在尝试在 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) 中,并使用肥皂客户端工具调用它,它工作得很好..
我将两者部署在同一(测试环境)服务器上的两个单独的 IIS 应用程序中,唯一的区别是 web.api 是 ASP.NET MVC 应用程序的一部分,而 asmx 服务是 的一部分>asp.net 网络 应用程序。
结果: asmx 服务有效,但 WEB API 错误并显示上述错误消息。
问题是为什么?我错过了什么? 我四处搜索,发现以下配置可能会有所帮助
<system.net>
<defaultProxy>
<proxy usesystemdefault="False"/>
</defaultProxy>
:(但不幸的是,在我的情况下它没有帮助......
有人遇到过这样的问题吗?
【问题讨论】:
【参考方案1】:试试这个
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);
【讨论】:
有什么区别??,无论如何我试过但没有成功我得到同样的错误...... 如果它在 asmx Web 服务中实现,我的工作正常。但是如果在 ASP.NET WEB API 中实现同样的功能,就不行了。以上是关于在 web api 与 asmx 服务中使用 SMTP 发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章