我有3個DLL文件:C# - 無法發送電子郵件,通過多個DLL稱爲
- 我的asp.net應用[ASPNET]
- 蒸汽API管理[Steam.dll]
- 通知管理器[通知.dll]
我需要計算蒸汽API請求(它們每天限制爲100K),並且在一天結束時,我想發送有關每天總請求的電子郵件。
//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 = "[email protected]";
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",
"[email protected]"
);
}
這是當不發送電子郵件方案:
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",
"[email protected]"
);
}
所以問題不在請求計數邏輯。我認爲問題與參考。也許主機環境(asp.net應用程序)沒有發送電子郵件所需的一切。如果沒有發生錯誤,我不知道如何檢查它。
你可以發佈你的整個代碼?也許Run()方法會引發一些錯誤,並且不會調用SendEMail。嘗試重構Run()以返回計數,並在調用sendemail之後傳遞一個參數,所有在asp.net上調用以嘗試成功 – Max 2014-09-03 14:21:13
在示例中,您發佈的「直接來自我的asp.net app(global.asax)有效「 - SendEMail'無條件地從'Application_Strart'調用,通常在」global.asax「中調用。您需要提供更好(但仍然很小)的樣本以及「不發送電子郵件」的含義(即「SendEMail」已被調用但失敗,而「SendEMail」未被調用)。 – 2014-09-03 14:33:05
請檢查我的編輯 – exeq 2014-09-03 15:16:34