如何获取通过 SmtpClient C# 发送邮件的事件

发布于 2024-12-26 05:36:38 字数 1090 浏览 2 评论 0原文

我正在尝试建立一个从我的网站发送新闻通讯的系统。

我将其构建为一个控制台应用程序,然后每天从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清君侧 2025-01-02 05:36:38

如果您执行非异步行 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.

寂寞花火° 2025-01-02 05:36:38

SmtpClient sc = new SmtpClient();

sc.SendCompleted += new SendCompletedEventHandler(SendAsyncCallback);

SmtpClient sc = new SmtpClient();

sc.SendCompleted += new SendCompletedEventHandler(SendAsyncCallback);

软糯酥胸 2025-01-02 05:36:38

您的代码本身现在正在这样做。如果 WriteLog() 确实写入了 Sent Email to...,则表示邮件已发送,否则未发送。

Your code is itself doing so now. If WriteLog() does write Sent Email to..., it means mail has been sent else not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文