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

ASP.NET Web API中Hangfire定时发送邮件失败问题求助

ASP.NET Web API + Hangfire 定时发邮件失败:DNS解析异常

我在ASP.NET Web API项目中使用System.Net.Mail.SmtpClient实现邮件发送功能,通过Hangfire执行定时自动发送任务。本地IIS服务器测试时邮件可正常发送,但部署到生产服务器后,Hangfire触发定时任务时出现「Failure sending mail」错误,错误堆栈显示无法解析远程名称mail.corbissoft.com。但直接调用API接口触发邮件发送时,生产环境也能正常运行。


邮件发送代码

public static void sendScheduledEmail(int clientId, string message)
{
    string smtpServer = "smtp.gmail.com";
    int smtpPort = 587;
    string smtpUsername = "abc@gmail.com";
    string smtpPassword = "abc1233454";

    try
    {
        using (var client = new SmtpClient(smtpServer, smtpPort))
        {
            client.EnableSsl = false;
            client.Credentials = new NetworkCredential(smtpUsername, smtpPassword);
            var mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(smtpUsername);
            string recipientEmail = new data_StockTransferInfoModel().GetEmail(clientId, message);
            if (!string.IsNullOrEmpty(recipientEmail))
            {
                mailMessage.To.Add(recipientEmail);
            }
            else
            {
                Log.Error("Recipient email not found for ClientID: {ClientId}", clientId);
                return;
            }
            mailMessage.Subject = "Test Email from ASP.NET Web API";
            mailMessage.Body = message;

            client.Send(mailMessage);

            Console.WriteLine($"Email sent successfully.");
        }
    }
    catch (Exception ex)
    {
        Log.Error(ex, "An error occurred while sending email: {ErrorMessage}", ex.Message);
    }
}

Hangfire定时任务配置(Startup.cs)

RecurringJob.AddOrUpdate<ClientsController>(controller => controller.SendAutoWhatsappMessage(), Cron.MinuteInterval(5));
RecurringJob.AddOrUpdate<ClientsController>(controller => controller.SendAutoEmail(),Cron.MinuteInterval(5));

错误堆栈信息

[ERR] An error occurred while sending email: Failure sending mail.
System.Net.Mail.SmtpException: Failure sending mail. ---&gt; System.Net.WebException: The remote name could not be resolved: 'mail.corbissoft.com'
   at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress&amp; address, Socket&amp; abortSocket, Socket&amp; abortSocket6)
   at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
   at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at LuandaryWebAPI.HelperClass.WhatsAppMessages.sendScheduledEmail(Int32 clientId, String message) in E:\WEBAPI_CHMUNIR 29-04-24 backup (2)\WEBAPI_CHMUNIR\LuandaryWebAPI\HelperClass\WhatsAppMessages.cs:line 139

排查方向

  • Hangfire运行账号权限差异:Hangfire默认使用应用池账号运行,而API接口调用依赖IIS网站账号。检查生产服务器上Hangfire所在应用池的账号是否具备DNS解析、外部网络访问权限,可临时将Hangfire应用池账号改为与网站账号一致测试。
  • 配置不一致问题:代码中SMTP服务器为smtp.gmail.com,但错误提示解析mail.corbissoft.com,说明生产环境存在配置覆盖。检查web.config、appsettings.json等配置文件是否重写了SMTP地址,或Hangfire任务是否调用了其他邮件发送逻辑。
  • DNS解析验证:在生产服务器上,用Hangfire应用池账号执行nslookup mail.corbissoft.com命令,确认该账号能否正常解析目标域名。
  • SmtpClient配置修正:Gmail的587端口需要开启SSL,代码中EnableSsl = false属于错误配置,可能触发异常跳转至其他SMTP服务器,建议改为client.EnableSsl = true。
  • Hangfire上下文差异:定时任务执行时的应用上下文与API请求上下文可能存在差异,检查sendScheduledEmail方法是否依赖特定环境变量或配置读取逻辑,确保定时任务能正确获取SMTP参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 19:47:35