我有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