繁体   English   中英

C#-无法发送通过多个dll调用的电子邮件

[英]C# - Cannot send email, called through multiple dlls

我有3个dll:

  • 我的asp.net应用程序[aspnet]
  • Steam api管理器[Steam.dll]
  • 通知管理器[Notifications.dll]

我需要计算Steam api请求(每天限制为10万个),最终,我想发送有关我每天提出的总请求的电子邮件。

    //My aspnet app global.asax
    protected void Application_Start(Object sender, EventArgs e)
    {
        Steam.RequestCounter.Run();        
    }

    //Steam.dll
    public static void Run()
    {
       // .. request count logic

        Notifications.SendEmail();
    }

    //Notifications.dll
    public static void SendEmail()
    {
       //..email sending logic
    }

如果调用堆栈如上所示,则不会发送电子邮件。 为什么? 如果我直接从我的asp .net应用程序(global.asax)调用Notifications.SendEmail()方法,它将起作用。 你能解释一下发生了什么吗?

编辑

通话过程中没有错误。 SmtpClient.Send()方法正常通过。 即使我尝试了catch块,也没有发生异常。

这是我的Notifications.SendEmail方法:

    public static void SendEmail(string subject, string body, params string[] recipients)
    {
        const string sender = "xxxx@outlook.com";
        var client = new SmtpClient("smtp-mail.outlook.com")
        {
            Port = 587,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false
        };

        var credentials = new System.Net.NetworkCredential(sender, "xxxx");
        client.EnableSsl = true;
        client.Credentials = credentials;

        var mail = new MailMessage {From = new MailAddress(sender)};
        foreach (var recipient in recipients)
        {
            mail.To.Add(recipient);
        }
        mail.Subject = subject;
        mail.Body = body;
        client.Send(mail);
    }

这是发送电子邮件(在global.asax内部)的情况:

    protected void Application_Start(Object sender, EventArgs e)
    {
        //call directly to Notifications.dll
        Email.SendEmail(
            "subject",
            "body",
            "xxx@outlook.com"
        );        
    }

这是不发送电子邮件的情况:

    protected void Application_Start(Object sender, EventArgs e)
    {
        //call to Steam.dll
        Steam.RequestCounter.Run();        
    }

    //Inside Steam.dll
    public static void Run()
    {
        SendEmail();
    }

    private static void SendEmail()
    {
        //call to Notifications.dll
        Email.SendEmail(
            "subject",
            "body",
            "xxx@outlook.com"
        ); 
    }

因此,问题不在请求计数逻辑中。 我认为参考存在问题。 也许宿主环境(asp.net应用)没有发送电子邮件所需的一切。 如果没有发生错误,我不知道如何检查。

启动应用程序时,global.asax中的函数将调用一次。 因此,如果未发生回收或部署,它将在星期一(即星期一)运行,并且永远不会再次运行。 您需要一些时间触发器,调度程序,服务或类似的东西

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM