我经常使用免费的gmail.com邮箱,因为它容量较大,但我们在使用.net编程实现邮件发送时,常会出现我们意想不到的错误。最常见的就是:
(1)The operation has timed out.
(2)出现类似提示:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first ...."
出现此类错误的主要原因是:
(1)POP的设置不对;
(2)SmtpClient的UseDefaultCredentials,Credentials,EnableSsl设置不正确所引起。
首先,需要进入gmail.com邮箱,进行"转发和 POP/IMAP"的选项设置。如下图:
然后,在你的Smtp程序中设置以下几项即可:
// 创建 SmtpClient 以发送 Email
SmtpClient client = new SmtpClient();
MailMessage message = new MailMessage();
// 设置发信人的EMAIL地址
message.From = new MailAddress(fromAddress);
// 设置收信人的EMAIL地址