ASP.NET fails to send email through Gmail SMTP, bu

2019-08-29 06:00发布

问题:

Hi all a couple weeks ago, my ASP.NET website stopped sending user comments which I implemented by emailing those comments to my email account through Gmail SMTP server (smtp.gmail.com). I opened the project on my development machine and again it fails to send the email after a couple minutes with the following exception message:

Failure sending mail

Unable to read data from the transport connection: net_io_connectionclosed.

I'm using Gmail's SMTP with port 465, and MailClient.EnableSSL = True. The weird thing is my Office Outlook 2007 is using the same settings and it doesn't have any problems sending mail using the same Gmail account.

Any thoughts?

回答1:

This code works for me. Note the port number is different to the 465 you are trying. I'm almost certain I also tried 465 first with no luck.

var msg = new MailMessage(new MailAddress(recipient, "SCS Web Site"), new MailAddress(toAddress))
                {
                    Body = BuildMailBody(),
                    Subject = "SCS Web Site Message"
                };
var smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(myGmailUser, myGmailPassword)
                };
smtp.Send(msg);


回答2:

You wouldn't happen to have a firewall disallowing outbound connections here then, say, specifically allowed outlook to punch through . . .



回答3:

One oddity I found with formmail going through gmail (on Windows server) was that the port number was the problem. My code was similar to yours, but it only worked when I changed the port to 25 rather than the 587 or 465. That might be what you need here.