views:

51

answers:

2

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?

+1  A: 

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.

ryudice
I'm not sure I understand your point. It works because it works?
Marc
+2  A: 

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)
        {

        }
    }
}
Marc
You are ignoring exceptions, you should log them in some place and let the user know an error happened at least.
ryudice
Since the email is not something the user triggers directly, I'm not sure I want them to even know if it failed. But your point on logging is well-taken. I should have that anyway.
Marc