C# System.Net.Mail.SmtpClient.Send()超时SmtpException
我想通过GMail发送电子邮件C# System.Net.Mail.SmtpClient.Send()超时SmtpException,c#,email,C#,Email,我想通过GMail发送电子邮件 static void Main(string[] args) { var client = new SmtpClient("smtp.gmail.com", 587) { Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"), Enabl
static void Main(string[] args)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
Console.WriteLine("Sent");
Console.ReadLine();
}
我可以在家里通过Gmail发送电子邮件
static void Main(string[] args)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
Console.WriteLine("Sent");
Console.ReadLine();
}
但System.Net.Mail.SmtpClient.Send返回超时SmtpException在公司的封闭网络中使用相同的代码
static void Main(string[] args)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
Console.WriteLine("Sent");
Console.ReadLine();
}
我要向网络管理员询问这件事
static void Main(string[] args)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
Console.WriteLine("Sent");
Console.ReadLine();
}
网络管理员不知道gmail使用的地址/端口信息,所以在要求我尝试将[address:smtp.gmail.com,port:587]与telnet连接并成功连接之前,我必须知道需要检查的地址/端口
static void Main(string[] args)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
Console.WriteLine("Sent");
Console.ReadLine();
}
是否还有更多内容需要检查/打开网络地址/端口?您需要使用EnableSsl=true
static void Main(string[] args)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
Console.WriteLine("Sent");
Console.ReadLine();
}
仅供参考,Gmail需要身份验证:
static void Main(string[] args)
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
Console.WriteLine("Sent");
Console.ReadLine();
}
Outgoing Mail (SMTP) Server
requires TLS or SSL: smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465
向我们展示导致问题的代码。这不是代码问题。它在家里工作得很好为什么是587端口?这就是你在代码中使用的端口吗?或者在app/web.config中?587是Gmail SMTP端口TLS。OP明确表示,代码在他家中有效,但在工作中无效。您建议如何改变这一点?@SteenT:它使用TLS。因此,请使用enableSsl=true来启用TLS。SSL&TSL实际上是等效的——TLS比SSL更广泛。根据,这将迫使SMTPClient使用3207,RFC使用这两个术语TLS/SSL。因此,在他的家中,它可以工作,没有TLS和SSL,但需要从他的工作网络?感谢您的回答,但@SteenTøttrup是正确的。我确实设置了EnableSsl。这不是代码问题。完全相同的代码在家里工作得很好。