C# Winforms Application Failure in sending Email:

2019-08-12 15:57发布

问题:

My WinForms C# program sends email. I've used MailMessage and SmtpClient. In the constructor of SmtpClient I passed smtp.gmail.com and port 587.

This is working on my friend's computer, however when I try it I get this error:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'smtp.gmail.com'

System.Net.Mail.SmtpException: Operation Timed out

How do I fix this error? Why is it specific to my computer?

If i use port 25 i get exception : Operation Timed out

If i use port 587 i get exception : The remote name could not be resolved: 'smtp.gmail.com'

if i use port 465 i get exception : Operation Timed out

回答1:

Try some basic network troubleshooting. First, ensure you can ping something by IP address, this does not need DNS to work, so try ping 173.194.41.86 and see if you get some replies.

Then ping www.gmail.com from your machine with ping www.gmail.com. This should tell you if your network can resolve any names to IP addresses. You should get four IP address pings in the response the same as above.

If it does not, then your DNS configuration is bad. If you have a static IP address then you will need to check with a network admin or set the DNS servers the same as the working machine. If its dynamic then you might try ipconfig /renew to get the latest settings from the DHCP server.

If pinging www.gmail.com worked then try ping smtp.gmail.com to see if the smtp host is resolving. If it does not, then there's still a DNS issue but one that is effecting only the smtp hostname.

Try ipconfig /flushdns to clear the local DNS cache and try again. Try rebooting, if not on a prod server.

You can also use nslookup to check DNS name resolution and also Resolve-DnsName in PowerShell, possibly only in 3.0 though.

Also, check any firewall settings.



回答2:

Mark EnableSsl in your SmtpClient to true.

smtpClient.EnableSsl = true;