为什么 System.Net.Mail 在我的 c#.net Web 应用程序的一部分中工作,但在另一部分中不起作用?

发布于 2024-09-05 04:09:50 字数 1678 浏览 2 评论 0原文

我有一个 Web 应用程序在我公司域内的 IIS 上运行,并且可以通过 Intranet 访问。我有这个应用程序根据一些用户操作发送电子邮件。例如,它部分是一个调度应用程序,因此如果任务完成,则会发送一封电子邮件通知其他用户。

问题是,电子邮件在某些情况下可以完美地工作,而在其他情况下则完全不行。我有一个 login.aspx 页面,该页面在加载页面时会发送报告电子邮件(通过 Windows 任务调度程序每天加载一次) - 这似乎总是工作得很好。我有一个更新页面,应该在输入文本并单击“更新”按钮时发送电子邮件 - 此操作大多数时候都会失败。这两个任务都使用我编写的使用 System.Net.Mail 发送电子邮件的相同静态重载方法。

我尝试使用 gmail 作为我的 smtp 服务器(而不是我们的内部服务器),并得到相同的结果。

我调查了运行本地 SMTP 服务是否有任何区别,结果似乎没有。此外,由于 C# 是服务器端代码,难道不应该只关心服务器上运行的内容,而不关心客户端上运行的内容吗?

请帮我找出问题所在!我应该去哪里看?我可以尝试什么?

下面是我发送电子邮件的代码。

public static void sendEmail(String[] recipients, String sender, String subject, String body, String[] attachments)
{
        MailMessage message;
        try
        {
            message = new MailMessage(sender, recipients[0]);
        }
        catch (Exception)
        {
            return;
        }
        foreach (String s in recipients)
        {
            if (!message.To.Contains(new MailAddress(s)))
                message.To.Add(s);
        }
        message.From = new MailAddress(sender);
        message.Subject = subject;
        message.Body = body;
        message.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("PRIVATE.PRIVATE.PRIVATE", 25);
        smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
        smtp.UseDefaultCredentials = true;
        if (attachments.Count() > 0)
        {
            foreach (String a in attachments)
            {
                message.Attachments.Add(new Attachment(a));
            }
        }
        try
        {
            smtp.Send(message);
        }
        catch (Exception e)
        {

        }
    }
}

I have a web application that is running on IIS within my company's domain, and is being accessed via intranet. I have this application sending out email based on some user actions. For example, its a scheduling application in part, so if a task is completed, an email is sent out notifying other users of that.

The problem is, the email works flawlessly in some cases, and not at all in others. I have a login.aspx page which sends out report emails when the page is loaded (its loaded once a day via windows task scheduler) - this always seems to work perfectly. I have an update page which is supposed to send email when text is entered and the "Update" button is clicked - this operation will fail most of the time. Both of these tasks use the same static overloaded method I wrote to send email using System.Net.Mail.

I have tried using gmail as my smtp server (instead of our internal one), and get the same results.

I investigated whether having the local SMTP Service running makes any difference, and it doesn't seem to. Besides, since C# is server-side code, shouldn't it only matter whats running on the server, and not the client?

Please help me figure out whats wrong! Where should I look? What can I try?

Below is my code to send email.

public static void sendEmail(String[] recipients, String sender, String subject, String body, String[] attachments)
{
        MailMessage message;
        try
        {
            message = new MailMessage(sender, recipients[0]);
        }
        catch (Exception)
        {
            return;
        }
        foreach (String s in recipients)
        {
            if (!message.To.Contains(new MailAddress(s)))
                message.To.Add(s);
        }
        message.From = new MailAddress(sender);
        message.Subject = subject;
        message.Body = body;
        message.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("PRIVATE.PRIVATE.PRIVATE", 25);
        smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
        smtp.UseDefaultCredentials = true;
        if (attachments.Count() > 0)
        {
            foreach (String a in attachments)
            {
                message.Attachments.Add(new Attachment(a));
            }
        }
        try
        {
            smtp.Send(message);
        }
        catch (Exception e)
        {

        }
    }
}

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

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

发布评论

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

评论(1

爱要勇敢去追 2024-09-12 04:09:50

在代码到达 sendmail 方法之前,您的代码中可能会出现异常,因此您的 sendmail 方法永远不会被调用,如果您有日志,则应该检查发生了什么。

另外,它可能适用于您的login.aspx,因为您没有遇到异常并且您的代码完成执行。

You are probably getting an exception in your code before your code reaches the sendmail method, so your sendmail method never gets called, if you have a log you should check there what is happening.

Also, it probably works on your login.aspx because you dont get exceptions and your code finishes executing.

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