【问题标题】:S22.Imap Attachment Count Always Return 0S22.Imap 附件计数始终返回 0
【发布时间】:2015-10-01 15:21:57
【问题描述】:

我正在尝试从存储在我的 Gmail 收件箱中的电子邮件中下载电子邮件附件。使用 S22.Imap 库 (https://github.com/smiley22/S22.Imap) 我得到消息列表,然后遍历它们以下载附件。我遇到的问题是 MailMessage.Attachments.Count 始终为 0,即使肯定有附加文件也是如此。这是我下载附件的代码。

            foreach (MailMessage m in messages)
            {
                if(m.Subject.Contains("Activity Statement") && (m.Attachments.Count > 0))
                {
                    foreach (Attachment a in m.Attachments)
                    {
                        var fileStream = File.Create(Properties.Settings.Default.ETL_ATTACHMENT_PROCESSING_DIR + a.Name);
                        a.ContentStream.Seek(0, SeekOrigin.Begin);
                        a.ContentStream.CopyTo(fileStream);
                        fileStream.Close();
                    }
                }
            }

【问题讨论】:

    标签: c# imap email-attachments gmail-imap


    【解决方案1】:

    你去吧..Net.Mail.Attachment.ContentStream 会给你一个流,你可以复制到一个文件中。要检查大小,请使用ContentStream.Length

    foreach (Attachment attachment in message.Attachments)
    {
        byte[] allBytes = new byte[attachment.ContentStream.Length];
        int bytesRead = attachment.ContentStream.Read(allBytes, 0, (int)attachment.ContentStream.Length);
        string destinationFile = @"C:\\Path\\" + attachment.Name;
        BinaryWriter writer = new BinaryWriter(new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
        writer.Write(allBytes);
        writer.Close();
    }
    

    【讨论】:

    • message.Attachments 始终为空,因此我的附件保存代码永远不会运行。即使邮件中肯定有附件,它似乎也不会接收到它。也许我不完全理解 imap 消息的构成?
    • 关于附件状态:有些身体部位被声明为附件,有些被声明不是,但许多(也许大多数)没有声明,分类取决于读者。 S22,您很可能不同意如何对某事物进行分类。如果 S22 完全尝试。我怀疑 Mailkit 会更同意你的看法。
    • 他确实说message.Attachments 什么也没返回,那么这个答案有什么用?你的循环仍然不会运行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    相关资源
    最近更新 更多