IrishChieftain
IrishChieftain

Reputation: 15253

ASP.NET Email Subscription

I have a database of email subscribers; approx. 1800 in the list. We have our own exchange server. I'm using the code below to grab each email address from the DB and send email, one at a time. I'm using an ASP.NET 4.0 Web Form to do this.

I notice the page hanging when the emails are being sent. What would be the best approach for implementing this - use a console app? How would I go about stress testing something like this?

I'm also getting an unhandled error message in the server log:

Event code: 3001 Event message: The request has been aborted.

// Call data access method via business class
TaxSalesBusiness bizClass = new TaxSalesBusiness();
DataSet ds = bizClass.GetSubscribers();

foreach (DataRow row in ds.Tables[0].Rows)
{
    // Check well-formedness of each email adddress
    if (!IsWellformedEmailAddr(row["Email"].ToString()))
    {
        // Ignore and log mal-formed email address
        LogError(row["Email"].ToString() 
           + " is a malformed email address. Message was not sent to this subscriber " 
           + row["Name"].ToString() + ".", "");
        continue;
    }
    else
    {
        string toAddress = row["Email"].ToString();
        smtpClient.Send(fromAddress, toAddress, message.Subject, message.Body); 
    }
} 

Upvotes: 0

Views: 1525

Answers (2)

JefClaes
JefClaes

Reputation: 3373

Another (more stable) solution could be to forward the mails to a (database?) queue, and have a Windows Service poll that queue once in a while. If there are new things on the queue the Windows Service is responsible for sending the e-mails.

This approach will help you steer clear from performance issues in your website, and it could make the delivery of the mails to be more reliable.

Upvotes: 1

Icarus
Icarus

Reputation: 63956

I notice the page hanging when the emails are being sent. What would be the best approach for implementing this - use a console app?

Yes.

And you can SendAsync instead of waiting for one email to be sent before you send the next one. I don't know how much your Exchange server will like that, though.

The problem with your current approach is that sending 1800 emails one by one is going to take a lot of time and using a web page to perform such a long operation will probably timeout the request. You can perhaps do it on an async page and launch everything on its own thread, but that's just complicating things more than it needs to when you can perfectly do this in a Console App with far less lines of code and complications. Remember the KISS principle.

Upvotes: 1

Related Questions