【问题标题】:How to set e-mail expiration time in C# MailMessage如何在 C# MailMessage 中设置电子邮件过期时间
【发布时间】:2013-05-17 03:53:34
【问题描述】:

我在设置电子邮件的过期时间时遇到问题。

我使用System.Net.Mail.MailMessage,但没有找到可以处理此问题的任何属性。我在 Outlook 中看到这是可能的。我在 StackOverflow、MSDN 和 Google 上进行了一些搜索,但找不到适合我需要的答案。

这是在 Outlook 中设置的http://blogs.mccombs.utexas.edu/the-most/2011/02/24/set-outlook-emails-to-delete-in-the-future/

问题是如何使用 C# 在 MailMessage 上设置 Expires After。是否需要设置任何标题或是否有任何其他方法可以做到这一点?这可能吗?

【问题讨论】:

    标签: c# .net .net-4.0 .net-4.5 mailmessage


    【解决方案1】:
    mail.Headers.Add("expiry-date", sTime);
    

    例如

    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 
    

    【讨论】:

      【解决方案2】:

      据我所知,我们的 Exchange 服务器会处理收到的邮件的过期问题。 因此,从您的角度来看,这不是您可以放在邮件标题中的任何内容,而是由客户的电子邮件帐户提供商处理。

      另一方面:EmailMessageClass 拥有您正在寻找的属性。

      /edit:如果您发送的电子邮件包含一个链接,它将用户重定向回您的页面,您可以检查您的数据库,如果电子邮件有效或无效(过期) 每次发送电子邮件时,请保存时间戳和您发送电子邮件的用户。 如果用户通过您邮件提供的链接返回,您可以检查实际时间是否在到期日期之前。 (例如 DateSend + 2 周)

      【讨论】:

      • 这个类是否适用于每个 IMAP 服务器或仅适用于 MES。我希望在过期后删除/删除这条消息,我不知道是否有这种可能性。
      • 我真的不知道,但我想不知道。由于这是 Exchange 命名空间中的一个类,我猜测它仅限于 Microsoft Exchange 邮件对象。
      猜你喜欢
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 2011-03-01
      相关资源
      最近更新 更多