【问题标题】:How to save attachments from IOS using Mimekit如何使用 Mimekit 从 IOS 保存附件
【发布时间】:2020-05-05 11:34:51
【问题描述】:

我的这段代码实际上在所有情况下都有效,除非图像是由 IOS 设备发送的,在这种情况下附件显示为空白。

我通过 IMAP 连接并获取 UIDS,然后提取有关电子邮件的信息、保存附件并保存整个电子邮件。

一切都很好,除非发件人通过 IOS 附加文档或图像,在这种情况下,代码无法找到任何文档。

我能做什么?

谢谢

try {
    IMailFolder mailFolder = imapClient.GetFolder(Folder);
    mailFolder.Open(FolderAccess.ReadOnly);

    MimeMessage m = mailFolder.GetMessage(new UniqueId(Decimal.ToUInt32(UID)));
    //MailMessage m = imapClient.GetMessage(UID, false, false);


    Subject = m.Subject;
    From_Name = (m.Sender != null) ? m.Sender.ToString() : "";//m.Sender.DisplayName;
    From_Address = (m.From != null) ? m.From.ToString() : "";
    Cc_Address = (m.Cc != null) ? m.Cc.ToString() : "";
    Date_Sent = m.Date.DateTime;
    MessageID = m.MessageId;    
    Body = m.TextBody;
    m.WriteTo (EmailName);


    if ( Save_Attachments && Attachments_Path != "") {
        System.IO.Directory.CreateDirectory(Attachments_Path);


        DataTable dt = new DataTable();

        dt.Columns.Add("Path", typeof(String));

        foreach (MimeEntity attachment in m.Attachments)
        {
            if (attachment is MimePart)
            {
                MimePart part = (MimePart)attachment;
                string path = System.IO.Path.Combine(Attachments_Path, removeIllegal(part.FileName));

                using (var stream = File.Create(path))
                {
                    part.Content.DecodeTo(stream);
                }

                DataRow dr = dt.NewRow();
                dr["Path"] = path;

                dt.Rows.Add(dr);

             }
        }

        Attachments = dt;
    } else {
        Attachments = null;
    }

}
catch (Exception ex) {
    Subject = "";
    From_Name = "";
    From_Address = "";
    Cc_Address = "";
    Date_Sent = new DateTime();
    MessageID = "";

    Body = "";

    Attachments = null;
    throw ex;
}


【问题讨论】:

    标签: c# ios imap mailkit mimekit


    【解决方案1】:

    您需要使用 MimeMessage.BodyParts,而不是使用 MimeMessage.Attachments - 但请注意 BodyParts 包含消息的文本正文。

    我在这里有更多信息:http://www.mimekit.net/docs/html/Working-With-Messages.htm

    完成您想做的事情的一种快速而肮脏的方式可能是这样的:

    foreach (var attachment in m.BodyParts.Where (x => x.ContentDisposition?.FileName != null))
    

    (注意:您需要确保在 C# 文件的顶部添加 using System.Linq;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      相关资源
      最近更新 更多