gpt4 book ai didi

c# - 从控制台应用程序发送电子邮件时导致套接字异常的原因是什么?

转载 作者:可可西里 更新时间:2023-11-01 09:01:17 25 4
gpt4 key购买 nike

我正在尝试编写一个可以发送电子邮件的基本控制台应用程序。问题是我不断收到 Socket 异常:

An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.x.xxx:25

我关闭了 Windows 防火墙,但它没有任何改变。我尝试在指定和不指定凭据的情况下运行它。我还尝试使用不同的 smtp 服务器,包括带有端口 25 的 smtp.mail.yahoo.com 和我的凭据,但没有成功。是什么问题导致了这个异常?

我在 Windows 7 上运行 VS2008。下面是我的代码示例和我得到的异常。

static void Main(string[] args)
{
String recipient = "recipient@email.com";
String sender = "sender@email.com";

MailMessage message = new MailMessage();
message.From = new MailAddress(sender);
message.To.Add(new MailAddress(recipient));
message.Subject = "Subject";
message.Body = "Body";

SmtpClient smtp = new SmtpClient
{
Host = "smtp.server",
Port = 25,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
//UseDefaultCredentials = false,
//Credentials = new NetworkCredential("validemail", "validpassword"),
Timeout = 3000
};

try
{
smtp.Send(message);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

我得到的异常是:

System.Net.Mail.SmtpException: Failure sending mail. ---> 
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.x.xxx:25
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, Int32 timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket
6, Int32 timeout)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at ...Program.Main(String[] args) in ...\Program.cs:line xxx

最佳答案

我曾经遇到过非常相似的情况,结果发现防病毒软件只是阻止了那个端口。此处列出了您可能的选择: http://social.msdn.microsoft.com/Forums/br/transactsql/thread/c083c2c6-f74e-42cd-a811-4329ff7aa5f1

关于c# - 从控制台应用程序发送电子邮件时导致套接字异常的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12692064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com