繁体   English   中英

如何使用 Mimekit 从 IOS 保存附件

[英]How to save attachments from IOS using Mimekit

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

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

一切都很好,除非发件人附加了 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;
}


您需要使用 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))

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM