如何向各类邮箱后缀发送邮件?Gmail发送SMTP认证错误求助
问题描述
向输入的邮箱地址发送邮件时,发送至Hotmail邮箱正常,但发送至Gmail邮箱时出现认证错误。
发送邮件代码
string to = Textbox1.Text; string subject = "Mail subject"; string body = "Hello"; string fromAddress = "test@hotmail.com"; string password = "xyz"; string smtpServer; int smtpPort; bool enableSSL; string domain = to.Substring(to.IndexOf("@") + 1); switch (domain.ToLower()) { case "gmail.com": smtpServer = "smtp.gmail.com"; smtpPort = 587; enableSSL = true; break; case "hotmail.com": smtpServer = "smtp.office365.com"; smtpPort = 587; enableSSL = true; break; default: MessageBox.Show("The specified email provider is not supported."); return; } SmtpClient smtpClient = new SmtpClient(smtpServer); smtpClient.Port = smtpPort; smtpClient.Credentials = new NetworkCredential(fromAddress, password); smtpClient.EnableSsl = enableSSL; MailMessage mailMessage = new MailMessage(fromAddress, to, subject, body); smtpClient.Send(mailMessage);
错误信息
SMTP服务器要求安全连接或客户端未通过认证。服务器响应:5.7.0 需要认证。了解更多请访问
解决方案
核心问题是:用Hotmail账号密码连接Gmail的SMTP服务器,不符合Gmail的认证规则。Gmail的SMTP仅允许自身账号(或关联谷歌工作区账号)认证,且需特定安全设置。
具体解决方式:
- 方案一:统一使用发件人对应的SMTP服务器。不管收件人是哪个邮箱,都用Hotmail的
smtp.office365.com发送,无需根据收件人域名切换SMTP,避免跨服务商认证问题。 - 方案二:改用Gmail账号作为发件人发送:
- 开启账号的2步验证,创建应用专用密码(替代原账号密码)
- 将代码中
fromAddress改为你的Gmail邮箱,password替换为应用专用密码 - 保留Gmail对应的SMTP配置即可正常发送
内容的提问来源于stack exchange,提问作者Dj Sucuk
相关产品推荐
相关产品推荐

