Header ListInsert(Int 32, String, String) Method
Insert a header with the specified field and value at the given index.
Definition
Namespace: MimeKit
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public void Insert(
int index,
string field,
string value
)Parameters
Remarks
Inserts the header at the specified index in the list.
Example
C#
public static void ModifyHeaders (MimeMessage message)
{
// Add() appends a new header to the end of the list, even if a header with the same
// name already exists.
message.Headers.Add ("X-Mailer", "MyMailer v1.0");
message.Headers.Add (HeaderId.Keywords, "MimeKit, Examples");
// Replace() replaces the value of the first header with the given name, and removes
// any other headers with that name. If no header exists, a new header is appended.
message.Headers.Replace (HeaderId.Organization, "Central Perk");
// The indexer may also be used to get or set the value of the first header with the
// given name. Getting the value of a header that does not exist returns null.
message.Headers["X-Priority"] = "1";
string? mailer = message.Headers["X-Mailer"];
Console.WriteLine ("X-Mailer: {0}", mailer ?? "(none)");
// Remove() removes only the first header with the given name, while RemoveAll()
// removes every header with that name.
message.Headers.Remove ("X-Priority");
message.Headers.RemoveAll (HeaderId.Received);
// Insert() can be used to control where a header goes. Trace headers, for example,
// are meant to be prepended to the existing headers.
message.Headers.Insert (0, "X-Trace", "Scanned by MyVirusScanner");
}
Exceptions
| ArgumentNullException |
field is . -or- value is . |
| ArgumentException | The field contains illegal characters. |
| ArgumentOutOfRangeException | index is out of range. |