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

2022年C#使用System.Net.Mail发送邮件失效,求可用代码

解决System.Net.Mail无法发送Gmail/Hotmail邮件的问题

目前有大量使用System.Net.Mail发送邮件的示例,但因邮件服务器近期升级,原有代码对Gmail和Hotmail均不再生效,原有代码如下:

void sendMail(string ip) {
    SmtpClient SmtpMail = new SmtpClient("smtp.gmail.com");

    MailMessage msg = new MailMessage();
    msg.Subject = "MailTest";
    msg.Body = "MailTest";
    msg.From = new MailAddress("email@gmail.com");
    msg.To.Add("email@gmail.com");
    msg.Priority = MailPriority.High;
    SmtpMail.EnableSsl = true;
    SmtpMail.UseDefaultCredentials = true;
    SmtpMail.Credentials = new System.Net.NetworkCredential("email@gmail.com", "password"); 

    try
    {
        SmtpMail.Send(msg);
        oldIP = newIP;
    }
    catch (Exception ec) 
    {
        int xxx = 1;
    }
    
}

此前问题因重复被关闭,指向需检查Gmail账户的‘Less secure apps’设置,但该设置已不再提供。此外未收到认证错误,代码仅抛出‘Failure sending mail’异常,求可用的有效代码?


Gmail 解决方案

Google已经彻底停用了"低安全性应用"设置,现在只能通过应用专用密码或OAuth2.0认证来发送邮件,这里先给出最简单的应用专用密码方案:

前置要求

  • 你的Gmail账户必须开启两步验证
  • 在Google账户安全设置中生成一个「应用专用密码」(选择对应应用类型,比如"邮件",生成后复制保存)

有效代码

void sendMail(string ip)
{
    // 使用using自动释放资源,避免内存泄漏
    using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
    using (var msg = new MailMessage())
    {
        msg.Subject = "MailTest";
        msg.Body = "MailTest";
        msg.From = new MailAddress("你的gmail邮箱@gmail.com");
        msg.To.Add("收件人邮箱@gmail.com");
        msg.Priority = MailPriority.High;

        smtpClient.EnableSsl = true;
        // 关键:必须先设为false,再设置Credentials,否则默认凭据会覆盖自定义配置
        smtpClient.UseDefaultCredentials = false;
        // 这里填生成的应用专用密码,不是原账户密码
        smtpClient.Credentials = new System.Net.NetworkCredential("你的gmail邮箱@gmail.com", "你的应用专用密码");

        try
        {
            smtpClient.Send(msg);
            oldIP = newIP;
        }
        catch (Exception ex)
        {
            // 建议捕获具体异常信息,方便排查问题
            Console.WriteLine($"发送失败:{ex.Message}");
            if (ex.InnerException != null)
            {
                Console.WriteLine($"内部异常:{ex.InnerException.Message}");
            }
        }
    }
}

注意事项

  • 端口用587(TLS协议),比465(SSL)兼容性更好
  • 如果是企业级应用,建议使用OAuth2.0认证,但配置步骤更复杂

Hotmail/Outlook 解决方案

Hotmail现在统一使用Outlook的SMTP服务,配置如下:

有效代码

void sendMail(string ip)
{
    using (var smtpClient = new SmtpClient("smtp.office365.com", 587))
    using (var msg = new MailMessage())
    {
        msg.Subject = "MailTest";
        msg.Body = "MailTest";
        msg.From = new MailAddress("你的hotmail邮箱@hotmail.com");
        msg.To.Add("收件人邮箱@hotmail.com");
        msg.Priority = MailPriority.High;

        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = false;
        // 如果开启了两步验证,同样需要使用微软账户的应用专用密码
        smtpClient.Credentials = new System.Net.NetworkCredential("你的hotmail邮箱@hotmail.com", "你的账户密码/应用专用密码");
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

        try
        {
            smtpClient.Send(msg);
            oldIP = newIP;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发送失败:{ex.Message}");
            if (ex.InnerException != null)
            {
                Console.WriteLine($"内部异常:{ex.InnerException.Message}");
            }
        }
    }
}

注意事项

  • 不要使用旧的smtp.live.com服务器,已被逐步弃用
  • 若开启两步验证,需在微软账户安全设置中生成应用专用密码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 07:25:22