views:

592

answers:

4

We are writing a feature to send a reminder email to customers in x number of days and just wondered if it was possible to delay the sending of the emails similar to how you can in Outlook (New Mail > Options button > Do not deliver before) in C#.

Does anyone know of a way of doing this?

Thanks for your help.

A: 

The SmtpClient is a pretty dumb client and doesn't support this -- sends are immediate whether they are synchronous or asynchronous. You may be able to do it using an Exchange integration. I'm not sure what APIs are exposed for Exchange, though. The simplest way would be to keep the message in a database table along with the time-to-send and have an off-line process that regularly scans this table and looks for messages whose time-to-send value has passed and are not marked as sent.

tvanfosson
Hehehe great minds think alike :)
Dave Swersky
+8  A: 

If you want to submit the email to an SMTP server to hold for a number of days, the server would have to support this feature. I would recommend against doing it that way, however. If you had a problem with missing emails it could be hard to track down.

Here's what I would do:

  1. Schedule the emails in your system by making a table entry.
  2. Include a "SendDate" column in the table where you can set a date in the future
  3. Write a Windows service that wakes up once every 15 minutes (for example) and sends the emails in that table where SendDate < currentDate
Dave Swersky
This is exactly what we do for our support website, to notify customers when an incident they have reported is modified. The only difference is, we use a SQL Server Job to send the mail instead of a service.
Carl
I too do this. It's much quicker to insert an email into a database table and you can also keep a record of emails sent.
GateKiller
+2  A: 

One possibility is to create a service that runs on a scheduled task processing 'pending' mail. You might store the pending mail in a SQL Database. (Dave details this)

This article looks promissing if you would like to avoid a 'real' service

http://www.codeproject.com/KB/aspnet/ASPNETService.aspx

ccook
I decided to go with the method detailed in the link above and this seems to be working fine. Thanks
Chris Knight
You're welcome Chris
ccook
+1  A: 

No.

The way to accomplish this would be for your application to write the email to a queue (could be database, file, MSMQ, etc), and then write a separate process that, at a later date, reads mail from that queue and sends it.

As far as I know, the SMTP protocol doesn't have any kind of "delayed send" feature. Also, the feature you reference in Outlook actually just keeps it in your client's outbox until the designated date, so it's a client-side feature, not a server-side feature.

Portman