2 using System.Collections.Specialized;
4 using System.Globalization;
5 using System.Collections.Generic;
7 namespace System.Net.Mail {
9 // Enumeration of the well-known headers.
10 // If you add to this enum you MUST also add the appropriate initializer in m_HeaderInfo below.
11 internal enum MailHeaderID {
19 ContentTransferEncoding,
45 ZMaxEnumValue = XSender, // Keep this to equal to the last "known" enum entry if you add to the end
49 internal static class MailHeaderInfo {
51 // Structure that wraps information about a single mail header
52 private struct HeaderInfo {
53 public readonly string NormalizedName;
54 public readonly bool IsSingleton;
55 public readonly MailHeaderID ID;
56 public readonly bool IsUserSettable;
57 public readonly bool AllowsUnicode;
58 public HeaderInfo(MailHeaderID id, string name, bool isSingleton, bool isUserSettable, bool allowsUnicode) {
60 NormalizedName = name;
61 IsSingleton = isSingleton;
62 IsUserSettable = isUserSettable;
63 AllowsUnicode = allowsUnicode;
69 // Table of well-known mail headers.
70 // Keep the initializers in sync with the enum above.
71 private static readonly HeaderInfo[] m_HeaderInfo = {
72 // ID NormalizedString IsSingleton IsUserSettable AllowsUnicode
73 new HeaderInfo(MailHeaderID.Bcc, "Bcc", true, false, true),
74 new HeaderInfo(MailHeaderID.Cc, "Cc", true, false, true),
75 new HeaderInfo(MailHeaderID.Comments, "Comments", false, true, true),
76 new HeaderInfo(MailHeaderID.ContentDescription, "Content-Description", true, true, true),
77 new HeaderInfo(MailHeaderID.ContentDisposition, "Content-Disposition", true, true, true),
78 new HeaderInfo(MailHeaderID.ContentID, "Content-ID", true, false, false),
79 new HeaderInfo(MailHeaderID.ContentLocation, "Content-Location", true, false, true),
80 new HeaderInfo(MailHeaderID.ContentTransferEncoding, "Content-Transfer-Encoding", true, false, false),
81 new HeaderInfo(MailHeaderID.ContentType, "Content-Type", true, false, false),
82 new HeaderInfo(MailHeaderID.Date, "Date", true, false, false),
83 new HeaderInfo(MailHeaderID.From, "From", true, false, true),
84 new HeaderInfo(MailHeaderID.Importance, "Importance", true, false, false),
85 new HeaderInfo(MailHeaderID.InReplyTo, "In-Reply-To", true, true, false),
86 new HeaderInfo(MailHeaderID.Keywords, "Keywords", false, true, true),
87 new HeaderInfo(MailHeaderID.Max, "Max", false, true, false),
88 new HeaderInfo(MailHeaderID.MessageID, "Message-ID", true, true, false),
89 new HeaderInfo(MailHeaderID.MimeVersion, "MIME-Version", true, false, false),
90 new HeaderInfo(MailHeaderID.Priority, "Priority", true, false, false),
91 new HeaderInfo(MailHeaderID.References, "References", true, true, false),
92 new HeaderInfo(MailHeaderID.ReplyTo, "Reply-To", true, false, true),
93 new HeaderInfo(MailHeaderID.ResentBcc, "Resent-Bcc", false, true, true),
94 new HeaderInfo(MailHeaderID.ResentCc, "Resent-Cc", false, true, true),
95 new HeaderInfo(MailHeaderID.ResentDate, "Resent-Date", false, true, false),
96 new HeaderInfo(MailHeaderID.ResentFrom, "Resent-From", false, true, true),
97 new HeaderInfo(MailHeaderID.ResentMessageID, "Resent-Message-ID", false, true, false),
98 new HeaderInfo(MailHeaderID.ResentSender, "Resent-Sender", false, true, true),
99 new HeaderInfo(MailHeaderID.ResentTo, "Resent-To", false, true, true),
100 new HeaderInfo(MailHeaderID.Sender, "Sender", true, false, true),
101 new HeaderInfo(MailHeaderID.Subject, "Subject", true, false, true),
102 new HeaderInfo(MailHeaderID.To, "To", true, false, true),
103 new HeaderInfo(MailHeaderID.XPriority, "X-Priority", true, false, false),
104 new HeaderInfo(MailHeaderID.XReceiver, "X-Receiver", false, true, true),
105 new HeaderInfo(MailHeaderID.XSender, "X-Sender", true, true, true)
108 private static readonly Dictionary<string, int> m_HeaderDictionary;
110 static MailHeaderInfo() {
113 // Check that enum and header info array are in sync
114 for(int i = 0; i < m_HeaderInfo.Length; i++) {
115 if((int)m_HeaderInfo[i].ID != i) {
116 throw new Exception("Header info data structures are not in sync");
121 // Create dictionary for string-to-enum lookup. Ordinal and IgnoreCase are intentional.
122 m_HeaderDictionary = new Dictionary<string, int>((int)MailHeaderID.ZMaxEnumValue + 1, StringComparer.OrdinalIgnoreCase);
123 for(int i = 0; i < m_HeaderInfo.Length; i++) {
124 m_HeaderDictionary.Add(m_HeaderInfo[i].NormalizedName, i);
128 internal static string GetString(MailHeaderID id) {
130 case MailHeaderID.Unknown:
131 case MailHeaderID.ZMaxEnumValue+1:
134 return m_HeaderInfo[(int)id].NormalizedName;
138 internal static MailHeaderID GetID(string name) {
140 if(m_HeaderDictionary.TryGetValue(name, out id)) {
141 return (MailHeaderID)id;
143 return MailHeaderID.Unknown;
146 internal static bool IsWellKnown(string name) {
148 return m_HeaderDictionary.TryGetValue(name, out dummy);
151 internal static bool IsUserSettable(string name) {
153 if (m_HeaderDictionary.TryGetValue(name, out index)) {
154 return m_HeaderInfo[index].IsUserSettable;
156 //values not in the list of well-known headers are always user-settable
160 internal static bool IsSingleton(string name) {
162 if(m_HeaderDictionary.TryGetValue(name, out index)) {
163 return m_HeaderInfo[index].IsSingleton;
168 internal static string NormalizeCase(string name) {
170 if(m_HeaderDictionary.TryGetValue(name, out index)) {
171 return m_HeaderInfo[index].NormalizedName;
176 internal static bool IsMatch(string name, MailHeaderID header) {
178 if(m_HeaderDictionary.TryGetValue(name, out index) && (MailHeaderID)index == header) {
184 internal static bool AllowsUnicode(string name) {
186 if (m_HeaderDictionary.TryGetValue(name, out index)) {
187 return m_HeaderInfo[index].AllowsUnicode;