使用 MailKit / MimeKit 从电子邮件中删除附件

2024-01-28

我正在使用 MailKit 库来处理电子邮件,它运行良好。但是,我试图将电子邮件拆分为其组成文件 a) 主电子邮件(无附件) b) 单个附件文件,以存储在文件系统上。

我可以单独保存附件,但似乎无法从电子邮件正文代码中删除它们。 IE。它们与主电子邮件一起保存,因此重复数据。 :/

我试过了:

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();

如果有人可以提供帮助,我将非常感激。

实施的解决方案仅供参考:

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是附件https://github.com/jstedfast/MimeKit/blob/master/MimeKit/MimeMessage.cs#L734 https://github.com/jstedfast/MimeKit/blob/master/MimeKit/MimeMessage.cs#L734然后迭代所有Multipart并致电https://github.com/jstedfast/MimeKit/blob/master/MimeKit/Multipart.cs#L468 https://github.com/jstedfast/MimeKit/blob/master/MimeKit/Multipart.cs#L468以便删除附件。

下面的示例对邮件做出了一些假设,例如只有一个Multipart一些电子邮件客户端(Outlook)在邮件的制作方式上非常有创意。

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));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 MailKit / MimeKit 从电子邮件中删除附件 的相关文章

随机推荐