简体   繁体   中英

C# - Cannot send email, called through multiple dlls

I have 3 dlls:

  • my asp.net application [aspnet]
  • steam api manager [Steam.dll]
  • notification manager [Notifications.dll]

I need to count steam api requests (they are limited to 100K per day) and in the end of the day, I want to send email about total requests per day I made.

    //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
    }

If the call stack looks like above, the email is not send. Why? If I call Notifications.SendEmail() method directly from my asp .net app (global.asax) it works. Can you please explain me what is going on?

EDIT :

There is no error during the call. SmtpClient.Send() method pass normally. Even if I put it into try, catch block, no exception occured.

This is my Notifications.SendEmail method:

    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);
    }

This is scenario when email is sent (inside global.asax):

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

This is scenario when email is not sent:

    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"
        ); 
    }

So problem is not in request counting logic. I think problem is with references. Maybe host environment(asp.net app) don't have everything what it needs to send the email. I don't know how to check it, if no error occurred.

The function in your global.asax will called once - when you Start your application. So if no recycle or deploy occurred it will run on ie Monday and never again. You need some time trigger, scheduler, service or something like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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