【问题标题】:I am using Koolwired.Imap to retrieve attachments via iMAP.我正在使用 Koolwired.Imap 通过 iMAP 检索附件。
【发布时间】:2013-11-18 17:52:51
【问题描述】:

我正在使用 Koolwired.Imap 来检索附件。以下是我编写的代码。

使用 K = Koolwired.Imap;

    public void GetAttachmentsTest(string thread, string selectFolder, string fileName)
    {

        K.ImapConnect connect = new K.ImapConnect(Global.host);

        K.ImapCommand command = new K.ImapCommand(connect);

        K.ImapAuthenticate auth = new K.ImapAuthenticate(connect, Global.username, Global.password);
        connect.Open();
        auth.Login();

        K.ImapMailbox mailBox = command.Select(Global.inbox);
        mailBox = command.Fetch(mailBox);

        K.ImapMailboxMessage mbstructure = new K.ImapMailboxMessage();

        while (true)
        {
            try
            {

                    int mailCount = mailBox.Messages.Count;

                    if (mailCount == 0)
                    {
                        Console.WriteLine("no more emails");
                        break;
                    }

                    for (int i = 0; i < mailCount; ++i)
                    {
                        mbstructure = mailBox.Messages[mailCount - 1];
                        mbstructure = command.FetchBodyStructure(mbstructure);

                        for (int j = 0; j < mbstructure.BodyParts.Count; ++j)
                        {
                            if (mbstructure.BodyParts[j].Attachment)
                            {
                                //Attachment
                                command.FetchBodyPart(mbstructure, mbstructure.BodyParts.IndexOf(mbstructure.BodyParts[j]));

                                //Write Binary File
                                string tempPath = Path.GetTempPath();
                                FileStream fs = new FileStream(tempPath + mbstructure.BodyParts[j].FileName, FileMode.Create);
                                int length = Convert.ToInt32(mbstructure.BodyParts[j].DataBinary.Length);
                                fs.Write(mbstructure.BodyParts[j].DataBinary, 0,length);
                                fs.Flush();
                                fs.Close();

                            }
                        }

                    }



            }
            catch (Exception ex)
            {
                Console.WriteLine("T1 " + ex.Message);
                Console.WriteLine("T1 " + ex.StackTrace);
                if (ex.InnerException != null)
                    Console.WriteLine("T1 " + ex.InnerException.Message);
            }
        }

    }

我在声明中遇到错误:

int 长度 = Convert.ToInt32(mbstructure.BodyParts[j].DataBinary.Length);

fs.Write(mbstructure.BodyParts[j].DataBinary, 0,length);

错误是:

输入不是有效的 Base-64 字符串,因为它包含非 base-64 字符、两个以上的填充字符或填充字符中的非法字符。

上面的代码在只有 1 个附件时显示的行分解。

如果有多个附件:

然后代码就在线分解了

mbstructure = command.FetchBodyStructure(mbstructure);

错误是:

无效格式无法解析正文部分标头。

我非常接近完成这项任务。谁能帮帮我。

我也想知道一旦我检索到这些电子邮件,如何删除它们。

谢谢。

【问题讨论】:

    标签: c# imap


    【解决方案1】:

    我遇到了同样的问题 如果有人关心,我解决了从 codeplex 下载库的最新源代码的问题。 一旦编译,它就可以正常工作。看来他们已经修好了。

    也用于删除电子邮件,只需将其标记为删除: command.SetDeleted(n, true); //-> 其中n是消息号。

    如果是 IMAP 连接,实际上你必须删除已删除的邮件才能完成删除。

    command.Expunge();

    希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-04-20
      • 2011-06-12
      • 2012-02-29
      • 2012-09-16
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多