2

I have a problem with setting the expiration time of an e-mail.

I use System.Net.Mail.MailMessage and I didn't find any property that will handle this. I've seen in Outlook that it is possible. I've done some search on StackOverflow, MSDN and Google and I couldn't find an answer that suits my needs.

This is how it is set in Outlook http://blogs.mccombs.utexas.edu/the-most/2011/02/24/set-outlook-emails-to-delete-in-the-future/

The question is how to set Expires After on MailMessage using C#. Is there any header needed to be set or is there any other way to do it? Is this possible?

Robert
  • 19,800
  • 5
  • 55
  • 85

2 Answers2

1

As far as I know our Exchange server handles the expiration on messages recieved. So from your point of view, this is nothing you can put in the mail header, but gets handled by the clients email-account-provider.

On the other hand: The EmailMessageClass has the property you are looking for.

/edit: If the email you send contains a link, which redirects the user back to your page you can check on your database if the email is valid or not (expired) Every time you send an email, save the Timestamp and the user you have send the email to. If the user returns through the link provided by your mail you can check if the actual time is before the expiration date. (for example DateSend + 2 weeks)

Marco
  • 22,856
  • 9
  • 75
  • 124
  • Does this class works with every IMAP server or just MES. I want this message to be removed/deleted after expiration and I don't know if there is such possiblity. – Robert May 22 '13 at 07:42
  • I honestly don't know, but I suppose not. Since this a class in the Exchange namespace my guess is, that it is limited to Microsoft Exchange mail objects. – Marco May 22 '13 at 07:47
1
mail.Headers.Add("expiry-date", sTime);

e.g.

namespace RedmineMailService
{


    // https://stackoverflow.com/questions/637866/sending-mail-without-installing-an-smtp-server
    // http://www.nullskull.com/articles/20030316.asp
    // https://www.redmine.org/boards/2/topics/26198
    // https://www.redmine.org/boards/2/topics/22259
    static class MailSender
    {


        public static void Test()
        {
            using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient())
            {

                using (System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage())
                {
                    client.Port = 25;
                    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    client.UseDefaultCredentials = false;
                    // client.UseDefaultCredentials = true;

                    // Must be after UseDefaultCredentials 
                    // client.Credentials = new System.Net.NetworkCredential("mailboxname", "password", "example.com");
                    // client.Port = 587;
                    // client.EnableSsl = true;

                    client.Host = "COR-EXCHANGE.cor.local";
                    mail.Subject = "this is a test email.";
                    mail.Body = "Test";


                    // https://www.iana.org/assignments/message-headers/message-headers.xhtml
                    // https://tools.ietf.org/html/rfc4021#page-32
                    // mail.Headers.Add("Importance", "High"); //  High, normal, or low.

                    mail.Priority = System.Net.Mail.MailPriority.High;

                    // for read-receipt
                    mail.Headers.Add("Disposition-Notification-To", RedmineMailService.Trash.UserData.info);

                    string sTime = System.DateTime.UtcNow.AddDays(-1).ToString("dd MMM yyyy", System.Globalization.CultureInfo.InvariantCulture) + " " +
                        System.DateTime.UtcNow.ToShortTimeString() + " +0000"; // Fixed, from +0100 - just take UTC - works in .NET 2.0 - no need for offset


                    // Set a message expiration date
                    // When the expiration date passes, the message remains visible 
                    // in the message list with a strikethrough. 
                    // It can still be opened, but the strikethrough gives a visual clue 
                    // that the message is out of date or no longer relevant.
                    mail.Headers.Add("expiry-date", sTime);


                    // https://tools.ietf.org/html/rfc2076#page-16
                    // https://tools.ietf.org/html/rfc1911
                    // The case-insensitive values are "Personal" and "Private" 
                    // Normal, Confidential, 

                    // If a sensitivity header is present in the message, a conformant
                    // system MUST prohibit the recipient from forwarding this message to
                    // any other user.  If the receiving system does not support privacy and
                    // the sensitivity is one of "Personal" or "Private", the message MUST
                    // be returned to the sender with an appropriate error code indicating
                    // that privacy could not be assured and that the message was not
                    // delivered [X400].
                    mail.Headers.Add("Sensitivity", "Company-confidential");





                    // for delivery receipt
                    mail.DeliveryNotificationOptions = 
                          System.Net.Mail.DeliveryNotificationOptions.OnSuccess
                        | System.Net.Mail.DeliveryNotificationOptions.OnFailure;


                    // mail.From = new System.Net.Mail.MailAddress("somebody@friends.com", "SomeBody");
                    mail.From = new System.Net.Mail.MailAddress(RedmineMailService.Trash.UserData.info, "COR ServiceDesk");


                    mail.To.Add(new System.Net.Mail.MailAddress(RedmineMailService.Trash.UserData.Email, "A"));
                    // mail.To.Add(new System.Net.Mail.MailAddress("user1@friends.com", "B"));
                    // mail.To.Add(new System.Net.Mail.MailAddress("user2@friends.com", "B"));
                    // mail.To.Add(new System.Net.Mail.MailAddress(RedmineMailService.Trash.UserData.info, "ServiceDesk"));


                    try
                    {
                        System.Console.WriteLine("Host: " + client.Host);
                        System.Console.WriteLine("Credentials: " + System.Convert.ToString(client.Credentials));
                        client.Send(mail);
                        System.Console.WriteLine("Mail versendet");
                    }
                    catch (System.Exception ex)
                    {
                        do
                        {
                            System.Console.Write("Fehler: ");
                            System.Console.WriteLine(ex.Message);
                            System.Console.WriteLine("Stacktrace: ");
                            System.Console.WriteLine(ex.StackTrace);
                            System.Console.WriteLine(System.Environment.NewLine);
                            ex = ex.InnerException;
                        } while (ex != null);
                    } // End Catch 

                } // End Using mail 

            } // End Using client 

        } // End Sub Test 


    } // End Class MailSender 


} // End Namespace RedmineMailService.Trash 
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442