zoukankan      html  css  js  c++  java
  • MailKit系列之附件分离

    本文主要谈谈实现思路,不提供完整代码

    一、分离基础

    1.MIME邮件的multipart类型

    引用文章:https://blog.csdn.net/wangyu13476969128/article/details/72724179
    MIME,英文全称为"Multipurpose Internet Mail Extensions",即多用途互联网邮件扩展,是目前互联网电子邮件普通遵循的技术规则。
    邮件体包含邮件的内容, 它的类型由邮件头的“Content-Type”域指出。常见的简单类型有text/plain(纯文本)和text/html(超文本)。
    MIME邮件Content-Type域常见的主类型如下:

    主类型 常见属性 参数含义
    text charset 文本信息所使用的字符集
    image name 图像的名称应用程序的名称
    application name 应用程序的名称
    multipart boundary 邮件分段边界标识

    对于multipart类型,下面有三种子类型:mixed、alternative、related
    multipart/mixed可以包含附件。
    multipart/related可以包含内嵌资源。
    multipart/alternative 纯文本与超文本共存

    二、附件分离

    引用文章:C#使用MailKit获取邮件中的附件(QQ邮箱/163网易邮箱)

    1.保存附件

    通过获取到的MimeMessage可以轻松得到附件,注意超过50M的文件为超大附件,不能下载,如qq的处理是将发一个链接提供下载。

    /// <summary>
    /// 获取邮件的附件
    /// </summary>
    /// <param name="attachments"></param>
    /// <param name="messageId"></param>
    /// <returns></returns>
    public List<EmailAttachmentDto> GetEmailAttachments(IEnumerable<MimeEntity> attachments, string messageId)
    {
        var emailAttachments = new List<EmailAttachmentDto>();
        foreach (var attachment in attachments)
        {
            if (attachment.IsAttachment)
            {
                var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;
                var filePath = MailHelper.GetEmlAttachmentFilePath(fileName, messageId);
                using (var stream = File.Create(filePath))
                {
                    if (attachment is MessagePart rfc822)
                    {
                        rfc822.Message.WriteTo(stream);
                    }
                    else
                    {
                        var part = (MimePart)attachment;
                        part.Content.DecodeTo(stream);
                    }
                }
    
                var mailFileInfo = new FileInfo(filePath);
                emailAttachments.Add(new EmailAttachmentDto() { FilePath = filePath, FileName = fileName, FileSize = mailFileInfo.Length });
            }
        }
        return emailAttachments;
    }
    

    2.保存EML

    其实,这一步才是关键,移除附件,只保存其他内容到eml,这样才不会读取时由于文件太大导致卡死

    if (mimeMessage.Body is Multipart multipart)
    {
        while (mimeMessage.Attachments.Any())
        {
            multipart.Remove(mimeMessage.Attachments.ElementAt(0));
        }
    
        var mineMessage = new MimeMessage()
        {
            Sender = mimeMessage.Sender,
            Body = multipart,
            MessageId = customerMimeMessage.Id.ToString(),
        };
    
        if (!System.IO.File.Exists(fileName))
        {
            await mineMessage.WriteToAsync(fileName);
        }
    }
    
    

    这样就完成了,附件和邮件eml分开存储

    参考文章

    http://www.it1352.com/675410.html
    https://www.cnblogs.com/pengze0902/p/8519715.html
    https://www.cnblogs.com/rocketRobin/p/8337055.html

  • 相关阅读:
    DC中为什么要用Uniquify?
    hdu 1596 find the safest road
    hdu2112 HDU Today
    hdu 2066 一个人的旅行
    poj 3026 Borg Maze
    poj 1979 Red and Black
    poj 1321 棋盘问题
    hdu 1010 Tempter of the Bone
    hdu 4861 Couple doubi
    codeforces584B Kolya and Tanya
  • 原文地址:https://www.cnblogs.com/xcsn/p/10701399.html
Copyright © 2011-2022 走看看