C#发送SMTP邮件超时求助:已转VB.Net方案仍遇问题
Hey there, let's break down why your SmtpClient.Send() call is hanging and timing out, even after whitelisting your app in the firewall. These are the most likely fixes to try:
1. Don’t Forget UseDefaultCredentials = false
This is one of the most common pitfalls with SmtpClient. If you skip setting this to false, the client will automatically use the credentials of the user running the app—completely overriding the credentials you specified. Add this line right before setting your network credentials:
smtpClient.UseDefaultCredentials = false;
2. Fix SSL Port Mismatch
You’re using port 465, which relies on Implicit SSL (the connection is encrypted from the start). But the .NET SmtpClient’s EnableSsl = true is designed for Explicit SSL (STARTTLS) (typically used on port 587). Virgin Media’s server might not handle this mismatch well.
Try either:
- Switch to port 587 (Virgin Media supports this for STARTTLS) while keeping
EnableSsl = true - If you need to stick with port 465, force modern TLS protocols to avoid negotiation issues:
smtpClient.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13;
3. Double-Check Account & Credential Settings
- Confirm your username (
x@y.com) and password are 100% correct. If your account has 2-factor authentication enabled, you’ll need to use an app-specific password instead of your regular login password. - Ensure Virgin Media allows SMTP access from your network (some ISPs restrict this, but since you’re using their official server, this is less likely—but worth verifying).
4. Add Timeout & Error Handling
A shorter timeout will give you faster feedback, and a try-catch block will reveal specific error messages instead of just a hang:
smtpClient.Timeout = 30000; // 30 seconds timeout try { smtpClient.Send(mailMsg); Console.WriteLine("Email sent successfully!"); } catch (Exception ex) { Console.WriteLine($"Send failed: {ex.Message}"); if (ex.InnerException != null) { Console.WriteLine($"Root cause: {ex.InnerException.Message}"); } }
5. Clean Up Resources Properly
While not the direct cause of the hang, wrapping your MailMessage and SmtpClient in using statements ensures resources are released correctly, preventing potential odd behavior down the line:
using (MailMessage mailMsg = new MailMessage()) using (SmtpClient smtpClient = new SmtpClient("smtp.virginmedia.com", 587)) { // Your email setup code here mailMsg.To.Add("a@b.com"); mailMsg.From = new MailAddress("x@y.com"); mailMsg.Subject = "subject"; mailMsg.Body = "body"; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new System.Net.NetworkCredential("x@y.com", "MyPassword"); smtpClient.EnableSsl = true; smtpClient.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13; smtpClient.Timeout = 30000; try { smtpClient.Send(mailMsg); Console.WriteLine("Email sent!"); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } }
The missing UseDefaultCredentials = false or SSL port mismatch is almost certainly the culprit here—give those fixes a shot first!
内容的提问来源于stack exchange,提问作者Rob Small

