开发者

Why does System.Net.Mail work in one part of my c#.net web app, but not in another?

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 (i开发者_Python百科ts 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)
        {

        }
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜