我一直在尝试创建一个小程序来通过smtp.gmail.com发送电子邮件,但它总是提示我“操作已经超时”.
我知道网上有很多解决方案,但没有一个可行.
我知道网上有很多解决方案,但没有一个可行.
try { MailMessage message = new MailMessage(); SmtpClient smtp = new SmtpClient(); message.From = new MailAddress("from@gmail.com"); message.To.Add(new MailAddress("to@gmail.com")); message.Subject = "Test"; message.Body = "Content"; smtp.Port = 465; smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential("from@gmail.com","pwd"); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(message); } catch (Exception ex) { MessageBox.Show("err: " + ex.Message); }
解决方法
将端口更改为587:
try { MailMessage message = new MailMessage(); SmtpClient smtp = new SmtpClient(); message.From = new MailAddress("from@gmail.com"); message.To.Add(new MailAddress("to@gmail.com")); message.Subject = "Test"; message.Body = "Content"; smtp.Port = 587; smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential("from@gmail.com","pwd"); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(message); } catch (Exception ex) { MessageBox.Show("err: " + ex.Message); }