System.Net.Mail 和 MailMessage 不立即发送消息

发布于 2024-12-04 11:55:58 字数 1015 浏览 1 评论 0原文

当我使用 System.Net.Mail 发送邮件时,消息似乎没有立即发送。他们需要一两分钟才能到达我的收件箱。一旦我退出应用程序,所有消息都会在几秒钟内收到。是否有某种邮件消息缓冲区设置可以强制 SmtpClient 立即发送消息?

public static void SendMessage(string smtpServer, string mailFrom, string mailFromDisplayName, string[] mailTo, string[] mailCc, string subject, string body)
{
    try
    {
        string to = mailTo != null ? string.Join(",", mailTo) : null;
        string cc = mailCc != null ? string.Join(",", mailCc) : null;

        MailMessage mail = new MailMessage();
        SmtpClient client = new SmtpClient(smtpServer);

        mail.From = new MailAddress(mailFrom, mailFromDisplayName);
        mail.To.Add(to);

        if (cc != null)
        {
            mail.CC.Add(cc);
        }

        mail.Subject = subject;
        mail.Body = body.Replace(Environment.NewLine, "<BR>");
        mail.IsBodyHtml = true;

        client.Send(mail);
    }
    catch (Exception ex)
    {
        logger.Error("Failure sending email.", ex);
    }

谢谢,

马克

When I sent a mail using System.Net.Mail, it seems that the messages do not send immediately. They take a minute or two before reaching my inbox. Once I quit the application, all of the messages are received within seconds though. Is there some sort of mail message buffer setting that can force SmtpClient to send messages immediately?

public static void SendMessage(string smtpServer, string mailFrom, string mailFromDisplayName, string[] mailTo, string[] mailCc, string subject, string body)
{
    try
    {
        string to = mailTo != null ? string.Join(",", mailTo) : null;
        string cc = mailCc != null ? string.Join(",", mailCc) : null;

        MailMessage mail = new MailMessage();
        SmtpClient client = new SmtpClient(smtpServer);

        mail.From = new MailAddress(mailFrom, mailFromDisplayName);
        mail.To.Add(to);

        if (cc != null)
        {
            mail.CC.Add(cc);
        }

        mail.Subject = subject;
        mail.Body = body.Replace(Environment.NewLine, "<BR>");
        mail.IsBodyHtml = true;

        client.Send(mail);
    }
    catch (Exception ex)
    {
        logger.Error("Failure sending email.", ex);
    }

Thanks,

Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不念旧人 2024-12-11 11:55:58

尝试一下,如果您在dotnet 4.0上,

using (SmtpClient client = new SmtpClient(smtpServer))  
{
    MailMessage mail = new MailMessage();
    // your code here.

    client.Send(mail);
}

这将使您的client实例处置,从而使其使用戒烟协议元素结束其SMTP会话。

如果您陷入了较早的dotnet版本,请尝试安排为程序发送的每个消息重新使用相同的SMTPCLEINT实例。

当然,请记住,电子邮件本质上是一个存储和前向系统,并且没有关于SMTP发送到接收的延迟的同步(甚至是正式预测的)。

Try this, if you're on Dotnet 4.0

using (SmtpClient client = new SmtpClient(smtpServer))  
{
    MailMessage mail = new MailMessage();
    // your code here.

    client.Send(mail);
}

This will Dispose your client instance, causing it to wrap up its SMTP session with a QUIT protocol element.

If you're stuck on an earlier dotnet version, try arranging to re-use the same SmtpClient instance for each message your program sends.

Of course, keep in mind that e-mail is inherently a store-and-forward system, and there is nothing synchronous (or even formally predictable) about delays from smtp SEND to reception.

∞琼窗梦回ˉ 2024-12-11 11:55:58

我同意奥利的观点。
要回答您的问题,不,我不相信您可以通过表单设置任何缓冲区设置。

您的问题令人困惑的是,您说消息需要一两分钟才能到达您的收件箱,但接着又说,当发送给自己时,它们会立即通过......我相信您的意思是在内部,消息发送很好,该问题仅发生在外部地址上。在这种情况下,听起来您的电子邮件服务器可能会将这些消息排在绑定到外部地址的其他电子邮件后面(这是正常活动)。在外部网站上等待一两分钟收到电子邮件并不是那么漫长的等待。

然而,这不太可能,但是您的交换服务器是否设置为扫描传出消息?

I agree with Ollie.
To answer your question, No, I don't believe there is any buffer setting you can set via a form.

What is confusing about your question is, you say the message take a minute or two to reach your inbox, but then go on to say that when sending to yourself, they go through instantly... I believe you meant that internally, messages send fine and the issue only occurs for external address. In this case, it sounds like maybe your email server maybe queing these messages behind other emails bound for external addresses (which is normal activity). Waiting a minute or two for an email on an external site isn't that long of a wait.

However, this is unlikely, but is your exchange server set to scan outgoing messages?

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