|
| 1 | +# Headers and Addresses |
| 2 | + |
| 3 | +## Address types |
| 4 | + |
| 5 | +| Type | Represents | |
| 6 | +|------|-----------| |
| 7 | +| `MailboxAddress` | A single `Name <addr>` email address | |
| 8 | +| `GroupAddress` | A named group containing mailboxes | |
| 9 | +| `InternetAddressList` | `IList<InternetAddress>` — the type of `To`, `Cc`, `Bcc`, `From`, `ReplyTo` | |
| 10 | + |
| 11 | +### Creating addresses |
| 12 | + |
| 13 | +```csharp |
| 14 | +var mailbox = new MailboxAddress("Alice", "alice@example.com"); |
| 15 | +var noName = new MailboxAddress(string.Empty, "anon@example.com"); |
| 16 | +var parsed = MailboxAddress.Parse("Bob <bob@example.com>"); |
| 17 | +var parsedGroup = InternetAddressList.Parse("Alice <alice@x.com>, Bob <bob@x.com>"); |
| 18 | +``` |
| 19 | + |
| 20 | +### Adding recipients |
| 21 | + |
| 22 | +```csharp |
| 23 | +message.To.Add(new MailboxAddress("Alice", "alice@example.com")); |
| 24 | +message.To.Add(new MailboxAddress("Bob", "bob@example.com")); |
| 25 | +message.Cc.Add(new MailboxAddress("Carol", "carol@example.com")); |
| 26 | +message.Bcc.Add(new MailboxAddress("Dave", "dave@example.com")); |
| 27 | +``` |
| 28 | + |
| 29 | +### Iterating mailboxes (skip group addresses) |
| 30 | + |
| 31 | +```csharp |
| 32 | +foreach (var mailbox in message.To.Mailboxes) |
| 33 | + Console.WriteLine($"{mailbox.Name} — {mailbox.Address}"); |
| 34 | +``` |
| 35 | + |
| 36 | +### Flattening mixed groups + individual addresses |
| 37 | + |
| 38 | +```csharp |
| 39 | +IEnumerable<MailboxAddress> AllMailboxes(InternetAddressList list) |
| 40 | +{ |
| 41 | + foreach (var addr in list) |
| 42 | + { |
| 43 | + if (addr is MailboxAddress mb) |
| 44 | + yield return mb; |
| 45 | + else if (addr is GroupAddress group) |
| 46 | + foreach (var groupMb in group.Members.Mailboxes) |
| 47 | + yield return groupMb; |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Working with headers |
| 55 | + |
| 56 | +### Standard headers via typed properties |
| 57 | + |
| 58 | +```csharp |
| 59 | +message.Subject = "Re: Questions"; |
| 60 | +message.Date = DateTimeOffset.UtcNow; |
| 61 | +message.MessageId = MimeUtils.GenerateMessageId(); |
| 62 | +message.InReplyTo = "<original-id@host>"; |
| 63 | +message.Priority = MessagePriority.Urgent; |
| 64 | +message.Importance = MessageImportance.High; |
| 65 | +``` |
| 66 | + |
| 67 | +### Custom and raw headers |
| 68 | + |
| 69 | +```csharp |
| 70 | +// Add custom headers |
| 71 | +message.Headers.Add("X-Custom-Header", "value"); |
| 72 | +message.Headers.Add("X-Mailer", "MyApp/1.0"); |
| 73 | + |
| 74 | +// Replace a header value |
| 75 | +message.Headers[HeaderId.Subject] = "Corrected Subject"; |
| 76 | + |
| 77 | +// Read a specific header |
| 78 | +string? xCustom = message.Headers["X-Custom-Header"]; |
| 79 | + |
| 80 | +// Enumerate all headers |
| 81 | +foreach (var header in message.Headers) |
| 82 | + Console.WriteLine($"{header.Field}: {header.Value}"); |
| 83 | + |
| 84 | +// Get all values for a multi-value header (e.g. Received) |
| 85 | +var received = message.Headers.GetAllValues(HeaderId.Received); |
| 86 | + |
| 87 | +// Remove a header |
| 88 | +message.Headers.Remove(HeaderId.XPriority); |
| 89 | +``` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## Constructing a reply message |
| 94 | + |
| 95 | +```csharp |
| 96 | +public static MimeMessage BuildReply(MimeMessage original, MailboxAddress from, bool replyToAll) |
| 97 | +{ |
| 98 | + var reply = new MimeMessage(); |
| 99 | + reply.From.Add(from); |
| 100 | + |
| 101 | + // Reply-To takes precedence over From |
| 102 | + if (original.ReplyTo.Count > 0) |
| 103 | + reply.To.AddRange(original.ReplyTo); |
| 104 | + else if (original.From.Count > 0) |
| 105 | + reply.To.AddRange(original.From); |
| 106 | + else if (original.Sender is not null) |
| 107 | + reply.To.Add(original.Sender); |
| 108 | + |
| 109 | + if (replyToAll) |
| 110 | + { |
| 111 | + reply.To.AddRange(original.To); |
| 112 | + reply.Cc.AddRange(original.Cc); |
| 113 | + } |
| 114 | + |
| 115 | + // Prefix Subject with "Re: " if not already present |
| 116 | + reply.Subject = original.Subject?.StartsWith("Re:", StringComparison.OrdinalIgnoreCase) == true |
| 117 | + ? original.Subject |
| 118 | + : "Re: " + original.Subject; |
| 119 | + |
| 120 | + // Thread headers |
| 121 | + if (!string.IsNullOrEmpty(original.MessageId)) |
| 122 | + { |
| 123 | + reply.InReplyTo = original.MessageId; |
| 124 | + foreach (var id in original.References) |
| 125 | + reply.References.Add(id); |
| 126 | + reply.References.Add(original.MessageId); |
| 127 | + } |
| 128 | + |
| 129 | + // Quote original plain-text body |
| 130 | + var sender = original.Sender ?? original.From.Mailboxes.FirstOrDefault(); |
| 131 | + var quotedText = new StringBuilder(); |
| 132 | + quotedText.AppendLine($"On {original.Date:f}, {sender?.Name ?? sender?.Address} wrote:"); |
| 133 | + using var reader = new StringReader(original.TextBody ?? string.Empty); |
| 134 | + for (string? line; (line = reader.ReadLine()) is not null;) |
| 135 | + quotedText.AppendLine("> " + line); |
| 136 | + |
| 137 | + reply.Body = new TextPart("plain") { Text = quotedText.ToString() }; |
| 138 | + return reply; |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | +## `ContentType` and `MediaType` |
| 145 | + |
| 146 | +```csharp |
| 147 | +// Access the content type of any MimeEntity |
| 148 | +var ct = entity.ContentType; |
| 149 | +Console.WriteLine(ct.MimeType); // e.g. "text/plain" |
| 150 | +Console.WriteLine(ct.MediaType); // e.g. "text" |
| 151 | +Console.WriteLine(ct.MediaSubtype); // e.g. "plain" |
| 152 | +Console.WriteLine(ct.Charset); // e.g. "utf-8" |
| 153 | +
|
| 154 | +// Compare |
| 155 | +if (ct.IsMimeType("text", "html")) { ... } |
| 156 | + |
| 157 | +// Parse from string |
| 158 | +var parsed = ContentType.Parse("application/json; charset=utf-8"); |
| 159 | +``` |
0 commit comments