Setting email expiry date


I came accross a task to set expiry date for outgoing emails. Although there is a Mail header to set the expiry date for an email, but it is one of the optional ones, so not many clients implement that.
RFC
http://www.faqs.org/rfcs/rfc4021.html
lists the header which is called “Expiry-Date” which has now been replaced in the new RFC by “Expires” , so setting this property in the System.Net.Mail MailMessage class I was able to expire the email as required.

Here is the little code

MailAddress from = new MailAddress(strEmailFrom, strEmailFrom);
MailAddress to = new MailAddress(strEmailto, strEmailto);
MailMessage message = new MailMessage(from, to);
message.Subject = strEmailSubject;
message.Body = strEmailBody;
message.IsBodyHtml = true;

DateTime dt = DateTime.Now;
dt = dt.AddDays(1);
//set the expires header for the email
//message.Headers.Add(“Expires”, String.Format(“{0:F}”, dt)); // this is according to the new RFC http://www.faqs.org/rfcs/rfc2156.html
message.Headers.Add(“Expiry-Date”, String.Format(“{0:F}”, dt)); // old RFC format http://www.faqs.org/rfcs/rfc1327.html
// Add a carbon copy recipient.
MailAddress copy = new MailAddress(strEmailFrom);
message.CC.Add(copy);
SmtpClient client = new SmtpClient(ConfigurationSettings.AppSettings[“MailServer”].ToString());
// Include credentials if the server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);

Advertisements

One thought on “Setting email expiry date

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s