| MimeParserMimeMessageEnd Event |
An event signifying the end of a
MimeMessage has been encountered.
Namespace: MimeKitAssembly: MimeKit (in MimeKit.dll) Version: 4.7.1
Syntax Remarks
An event signifying the end of a
MimeMessage has been encountered.
Example class MimeOffsets
{
public string MimeType { get; set; }
public long? MboxMarkerOffset { get; set; }
public int LineNumber { get; set; }
public long BeginOffset { get; set; }
public long HeadersEndOffset { get; set; }
public long EndOffset { get; set; }
public MimeOffsets Message { get; set; }
public List<MimeOffsets> Children { get; set; }
public long Octets { get; set; }
public int? Lines { get; set; }
}
public static void MimeOffsetsExample (string fileName)
{
using (var stream = fileName.OpenRead (fileName)) {
var messages = new Dictionary<MimeMessage, MimeOffsets> ();
var entities = new Dictionary<MimeEntity, MimeOffsets> ();
MimeOffsets messageOffsets = null;
var parser = new MimeParser (stream, MimeFormat.Entity);
parser.MimeMessageBegin += delegate (sender, args) {
var parser = (MimeParser) sender;
var offsets = new MimeOffsets {
BeginOffset = args.BeginOffset,
LineNumber = args.LineNumber
};
if (args.Parent != null) {
var parentOffsets = entities[args.Parent];
parentOffsets.Message = offsets;
} else {
offsets.MboxMarkerOffset = parser.MboxMarkerOffset;
messageOffsets = offsets;
}
messages.Add (args.Message, offsets);
};
parser.MimeMessageEnd += delegate (sender, args) {
var offsets = messages[args.Message];
offsets.Octets = args.EndOffset - args.HeadersEndOffset;
offsets.HeadersEndOffset = args.HeadersEndOffset;
offsets.EndOffset = args.EndOffset;
};
parser.MimeEntityBegin += delegate (sender, args) {
var offsets = new MimeOffsets {
MimeType = args.Entity.ContentType.MimeType,
BeginOffset = args.BeginOffset,
LineNumber = args.LineNumber
};
if (args.Parent != null && entities.TryGetValue (args.Parent, out var parentOffsets)) {
parentOffsets.Children ??= new List<MimeOffsets> ();
parentOffsets.Children.Add (offsets);
}
entities.Add (args.Entity, offsets);
};
parser.MimeEntityEnd += delegate (sender, args) {
var offsets = entities[args.Entity];
offsets.Octets = args.EndOffset - args.HeadersEndOffset;
offsets.HeadersEndOffset = args.HeadersEndOffset;
offsets.EndOffset = args.EndOffset;
offsets.Lines = args.Lines;
};
var message = parser.ParseMessage ();
foreach (var bodyPart in message.BodyParts.OfType<MimePart> ()) {
var offsets = entities[bodyPart];
Console.WriteLine ($"The offsets for the MIME part for {bodyPart.ContentType} are:");
Console.WriteLine ($" - LineNumber: {offsets.LineNumber}")
Console.WriteLine ($" - BeginOffset: {offsets.BeginOffset}");
Console.WriteLine ($" - HeadersEndOffset: {offsets.HeadersEndOffset}");
Console.WriteLine ($" - EndOffset: {offsets.BeginOffset}");
Console.WriteLine ($" - Octets: {offsets.Octets}");
Console.WriteLine ($" - Lines: {offsets.Lines}");
}
}
}
See Also