C# · 12月 29, 2021

C#Windows窗体应用程序 – 使用gmail smtp发送电子邮件

我一直在尝试创建一个小程序来通过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){ Message@R_180_2419@.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){ Message@R_180_2419@.Show(“err: ” + ex.Message);}