如何获取通过 SmtpClient C# 发送邮件的事件
我正在尝试建立一个从我的网站发送新闻通讯的系统。
我将其构建为一个控制台应用程序,然后每天从 Windows 调度程序执行该应用程序。
我想在系统中进行一些日志记录,所以我添加了最简单的日志“功能”。
邮件发送得很好,但我想在发送邮件时在日志中写入。问题是我不知道如何在邮件发送或邮件未发送时从 SmtpClient 获取事件。
public void SendMails(List<Agent> agents, string logfilename)
{
foreach (Agent agent in agents)
{
MailMessage mail = new MailMessage();
mail.....
SmtpClient client = new SmtpClient();
client.....
try
{
client.Send(mail);
WriteLog(logfilename, "Sent mail to: " + mail.To[0].Address);
}
catch (SmtpException smtpexception)
{
WriteLog(logfilename, "SMTP EXCEPTION: " + smtpexception.Message);
}
catch (Exception e)
{
WriteLog(logfilename, "EXCEPTION: " + e.Message);
}
}
}
public void WriteLog(string filename, string message)
{
TextWriter tw = new StreamWriter(@"c:\MailLogs\" + filename + ".txt");
tw.WriteLine(message);
tw.Close();
}
那么,我该如何更改代码,以便在程序实际知道邮件是否发送后我可以写入日志......
I am trying to build a system for sending newsletters from my website.
I am building it as a console app which I will then execute daily from windows scheduler.
I want to have some logging in the system, so I am adding the most simple log "feature".
The mails are sent fine, but I would like to write in the log when a mail is sent. The problem is that I don't know how to get an event from the SmtpClient when the mail was sent or the mail was not sent.
public void SendMails(List<Agent> agents, string logfilename)
{
foreach (Agent agent in agents)
{
MailMessage mail = new MailMessage();
mail.....
SmtpClient client = new SmtpClient();
client.....
try
{
client.Send(mail);
WriteLog(logfilename, "Sent mail to: " + mail.To[0].Address);
}
catch (SmtpException smtpexception)
{
WriteLog(logfilename, "SMTP EXCEPTION: " + smtpexception.Message);
}
catch (Exception e)
{
WriteLog(logfilename, "EXCEPTION: " + e.Message);
}
}
}
public void WriteLog(string filename, string message)
{
TextWriter tw = new StreamWriter(@"c:\MailLogs\" + filename + ".txt");
tw.WriteLine(message);
tw.Close();
}
So, how do I change the code, so I can write the log after the program actually knows whether the mail was sent or not...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您执行非异步行
client.Send(mail);
,并且它不会抛出异常,则意味着邮件已发送。无论它是否已交付,您都将无法看到,因为这是由您的 smtp 服务器处理的,并且据我所知,SmtpClient 不支持该事件。
if you execute the line
client.Send(mail);
which is not async and it does not throw an Exception it means that the mail has been sent.Whether it is delivered or not you will not be able to see as that is handled by your smtp server and as far as I know the SmtpClient does not support an event for that.
SmtpClient sc = new SmtpClient();
sc.SendCompleted += new SendCompletedEventHandler(SendAsyncCallback);
SmtpClient sc = new SmtpClient();
sc.SendCompleted += new SendCompletedEventHandler(SendAsyncCallback);
您的代码本身现在正在这样做。如果
WriteLog()
确实写入了Sent Email to...
,则表示邮件已发送,否则未发送。Your code is itself doing so now. If
WriteLog()
does writeSent Email to...
, it means mail has been sent else not.