Click here to Skip to main content
15,903,854 members
Articles / Programming Languages / Visual Basic
Article

Sending E-mails from ASP.NET with expiry dates

Rate me:
Please Sign up or sign in to vote.
3.50/5 (9 votes)
18 Aug 2003 52K   15   3
How to apply an expiry date to an E-mail so that it will work correctly with the expires-after field in Microsoft Exchange (i.e. Outlook).

Introduction

Have you ever wanted to set an expiry time on an E-mail you send using the System.Web.Mail class library? The solution is pretty simple but I puzzled for a while over this and couldn't find any samples, and so I thought it might be helpful to submit some code.

The Code

VB
Dim sTime As String = _
   Now().AddDays(1).ToString("dd MMM yyyy") & " " & _
   Now().ToShortTimeString & " +0100"
Dim objMsg As New 
MailMessage
objMsg.BodyFormat = MailFormat.Html
objMsg.To = "you@home.com"
objMsg.From = "me@home.com"
objMsg.Subject = "Expiry Test"
objMsg.Body = "<p>test</p>"
objMsg.Headers.Add("expiry-date", sTime)
SmtpMail.SmtpServer = "myServer"
SmtpMail.Send(objMsg)

Explanation

Note the special format required for the Date/Time - the "+0100" at the end indicates your time zone, in this case one hour ahead of GMT. In this case of course I have set the expiry date as one day from Now()

This requires a reference to System.Web.Mail which suggests Web Forms but can be used in any .NET application.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWinform? Pin
| Muhammad Waqas Butt |25-Aug-05 23:58
professional| Muhammad Waqas Butt |25-Aug-05 23:58 
AnswerRe: Winform? Pin
rohancragg26-Aug-05 3:51
rohancragg26-Aug-05 3:51 
Yes.

You would need to reference and import System.Web.Mail.

However, your windows clients will need permission to relay SMTP email via the server and the SMTP server will need to be configured to allow these clients to send SMTP email via that server. You may also encounter problems with client firewalls blocking SMTP traffic on port 25.

The following resource on System.Web.Mail may prove to be useful:

http://systemwebmail.com

hth
GeneralRe: Winform? Pin
| Muhammad Waqas Butt |27-Aug-05 4:41
professional| Muhammad Waqas Butt |27-Aug-05 4:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.