You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何向各类邮箱后缀发送邮件?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账号作为发件人发送:
    1. 开启账号的2步验证,创建应用专用密码(替代原账号密码)
    2. 将代码中fromAddress改为你的Gmail邮箱,password替换为应用专用密码
    3. 保留Gmail对应的SMTP配置即可正常发送

内容的提问来源于stack exchange,提问作者Dj Sucuk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 05:25:26