Search code examples
c#sharepointsharepoint-2007

SendEmail method not working


I'm writing an application which sends a lot of emails throughout the app's lifecycle.

The users complained that the application was really unresponsive and generally slow. The only thing I could come up with as the reason, was the heavy email sending. So I thought I could solve the problem by sending the emails in a different thread!

I'm using the SendEmail method in the SPUtility helper class. Right now, I have a method that looks like this:

public static void SendEmail(SPWeb web, string to, string subject, string body)
{

    //Some logic goes here
    System.Threading.ThreadPool.QueueUserWorkItem(o =>
    {
        SPUtility.SendEmail(web, false, false, to, subject, body, false);
    });
}

This method is defined in a DLL, and it's being called from a LOT of different webparts.

But this is not working. If I remove the QueueUserWorkItem bit, and just let it send emails outside of a thread, it works like a charm, but then again, it's slow.

How can I solve this?


Solution

  • I suspect you're better off using MailMessage Class (System.Net.Mail)

    MailMessage mess = 
        new MailMessage(
            SPContext.Current.Site.WebApplication.OutboundMailReplyToAddress, 
            sendTo, 
            subject, 
            message);  
    mess.IsBodyHtml = true;  
    SmtpClient smtp = 
        new SmtpClient(
            SPContext.Current.Site.WebApplication.OutboundMailServiceInstance.Server.Address);  
    smtp.Send(mess);
    

    this MSDN post is similar to your situation, Sending Email using SPUtility.SendEmail slow in production