我正在使用MailKit库来处理电子邮件,这些邮件一直运行良好。但是,我试图将电子邮件分成他们的组成文件a)主电子邮件(无附件)b)单独的附件文件存储在文件系统上。
I'm using MailKit library to handle emails, which has been working well. However, I'm trying to split emails into their constituent files a) Main email (no attachments) b) Individual attachment files, to store on the filesystem.
我可以单独保存附件,但似乎不能将其从电子邮件正文代码中删除。即它们与主电子邮件一起被保存,因此复制数据。 :/
I can save the attachments individually, but can't seem to remove them from the email body code. I.e. they're getting saved along with the main email, so duplicating data. :/
我尝试过:
foreach (MimePart part in inMessage.BodyParts) { if (part.IsAttachment) { // Remove MimePart < This function isn't available on the collection. } }还尝试过:
var builder = new BodyBuilder(); foreach (MimePart part in inMessage.BodyParts) { if (!part.IsAttachment) { // Add MimeParts to collection < This function isn't available on the collection. } } outMessage.Body = builder.ToMessageBody();如果有人可以帮忙,我非常感谢。
If anyone can help with this, I'd much appreciate it.
解决方案实现FYI:
private string GetMimeMessageOnly(string outDirPath) { MimeMessage message = (Master as fsEmail).GetMimeMessage(); if (message.Attachments.Any()) { var multipart = message.Body as Multipart; if (multipart != null) { while (message.Attachments.Count() > 0) { multipart.Remove(message.Attachments.ElementAt(0)); } } message.Body = multipart; } string filePath = outDirPath + Guid.NewGuid().ToString() + ".eml"; Directory.CreateDirectory(Path.GetDirectoryName(outDirPath)); using (var cancel = new System.Threading.CancellationTokenSource()) { using (var stream = File.Create(filePath)) { message.WriteTo(stream, cancel.Token); } } return filePath; }仅获取附件:
private List<string> GetAttachments(string outDirPath) { MimeMessage message = (Master as fsEmail).GetMimeMessage(); List<string> list = new List<string>(); foreach (MimePart attachment in message.Attachments) { using (var cancel = new System.Threading.CancellationTokenSource()) { string filePath = outDirPath + Guid.NewGuid().ToString() + Path.GetExtension(attachment.FileName); using (var stream = File.Create(filePath)) { attachment.ContentObject.DecodeTo(stream, cancel.Token); list.Add(filePath); } } } return list; }推荐答案
您可以检索所有 MimePart s是附件 github/jstedfast/MimeKit/blob/master/MimeKit/MimeMessage.cs#L734 ,然后迭代所有 Multipart s和致电 github/jstedfast/MimeKit/blob/ master / MimeKit / Multipart.cs#L468 ,以删除附件。
You could retrieve all MimeParts that are attachments github/jstedfast/MimeKit/blob/master/MimeKit/MimeMessage.cs#L734 and then iterate over the all Multiparts and call github/jstedfast/MimeKit/blob/master/MimeKit/Multipart.cs#L468 for the attachments to remove.
下面的示例对邮件做了一些假设,例如只有一个 Multipart 某些电子邮件客户端(Outlook)非常有创意如何制作邮件。
The sample below makes a few assumptions about the mail e.g. there is only one Multipart some email client (Outlook) are very creative how mails are crafted.
static void Main(string[] args) { var mimeMessage = MimeMessage.Load(@"x:\sample.eml"); var attachments = mimeMessage.Attachments.ToList(); if (attachments.Any()) { // Only multipart mails can have attachments var multipart = mimeMessage.Body as Multipart; if (multipart != null) { foreach(var attachment in attachments) { multipart.Remove(attachment); } } mimeMessage.Body = multipart; } mimeMessage.WriteTo(new FileStream(@"x:\stripped.eml", FileMode.CreateNew)); }更多推荐
使用MailKit / MimeKit从电子邮件中剥离附件
发布评论