117 lines
3.8 KiB
C#
117 lines
3.8 KiB
C#
using mailview.Models;
|
|
using MimeKit;
|
|
using MsgReader.Outlook;
|
|
|
|
namespace mailview;
|
|
|
|
public class EmailParserService
|
|
{
|
|
public EmailViewModel ParseEmail(IFormFile file)
|
|
{
|
|
var extension = Path.GetExtension(file.FileName).ToLowerInvariant();
|
|
using var stream = file.OpenReadStream();
|
|
|
|
if (extension == ".eml")
|
|
{
|
|
return ParseEml(stream);
|
|
}
|
|
else if (extension == ".msg")
|
|
{
|
|
return ParseMsgOnLinux(stream);
|
|
}
|
|
|
|
throw new NotSupportedException("Alleen .eml en .msg worden ondersteund.");
|
|
}
|
|
|
|
private EmailViewModel ParseEml(Stream stream)
|
|
{
|
|
var message = MimeMessage.Load(stream);
|
|
|
|
var model = new EmailViewModel
|
|
{
|
|
Subject = message.Subject ?? string.Empty,
|
|
From = message.From.ToString(),
|
|
To = message.To.ToString(),
|
|
Date = message.Date,
|
|
HtmlBody = message.HtmlBody ?? message.TextBody?.Replace("\n", "<br>") ?? string.Empty
|
|
};
|
|
|
|
foreach (var attachment in message.Attachments)
|
|
{
|
|
if (attachment is MimePart mimePart)
|
|
{
|
|
using var ms = new MemoryStream();
|
|
mimePart.Content.DecodeTo(ms);
|
|
var bytes = ms.ToArray();
|
|
|
|
model.Attachments.Add(new AttachmentDto
|
|
{
|
|
FileName = mimePart.FileName ?? "bijlage",
|
|
ContentType = mimePart.ContentType.MimeType,
|
|
Size = bytes.Length,
|
|
Base64Data = Convert.ToBase64String(bytes)
|
|
});
|
|
}
|
|
}
|
|
|
|
return model;
|
|
}
|
|
|
|
// Speciaal aangepast voor Linux / Ubuntu (omzeilt System.Drawing)
|
|
private EmailViewModel ParseMsgOnLinux(Stream stream)
|
|
{
|
|
// Laad het bericht op laag niveau zonder RTF font-initialisatie
|
|
using var reader = new Storage.Message(stream);
|
|
|
|
string body = string.Empty;
|
|
|
|
// Vraag de ruwe variabelen op zonder de RTF/Font converter te triggeren
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(reader.BodyHtml))
|
|
{
|
|
body = reader.BodyHtml;
|
|
}
|
|
else if (!string.IsNullOrEmpty(reader.BodyText))
|
|
{
|
|
body = System.Net.WebUtility.HtmlEncode(reader.BodyText).Replace("\n", "<br>");
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Mocht een property toch proberen te decompileren, val terug op ruwe text
|
|
body = !string.IsNullOrEmpty(reader.BodyText)
|
|
? System.Net.WebUtility.HtmlEncode(reader.BodyText).Replace("\n", "<br>")
|
|
: "<i>(Inhoud van .msg bestand kon niet worden uitgelezen op dit systeem)</i>";
|
|
}
|
|
|
|
var model = new EmailViewModel
|
|
{
|
|
Subject = reader.Subject ?? string.Empty,
|
|
From = reader.Sender != null ? $"{reader.Sender.DisplayName} <{reader.Sender.Email}>" : string.Empty,
|
|
To = reader.Recipients != null ? string.Join(", ", reader.Recipients.Select(t => $"{t.DisplayName} <{t.Email}>")) : string.Empty,
|
|
Date = reader.SentOn,
|
|
HtmlBody = body
|
|
};
|
|
|
|
// Bijlagen uitlezen
|
|
if (reader.Attachments != null)
|
|
{
|
|
foreach (var att in reader.Attachments)
|
|
{
|
|
if (att is Storage.Attachment attachment)
|
|
{
|
|
model.Attachments.Add(new AttachmentDto
|
|
{
|
|
FileName = attachment.FileName,
|
|
ContentType = "application/octet-stream",
|
|
Size = attachment.Data.Length,
|
|
Base64Data = Convert.ToBase64String(attachment.Data)
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
return model;
|
|
}
|
|
} |