【问题标题】:S22.IMAP does not always read attachmentsS22.IMAP 并不总是读取附件
【发布时间】:2018-03-27 17:50:53
【问题描述】:

我使用S22.IMAP库如下:

using (ImapClient client = new ImapClient(imap, 993,
                   usuario, psw, AuthMethod.Login, true))
{
    foreach (var uid in client.Search(SearchCondition.Unseen()))
    {
        var message = client.GetMessage(uid);                      
        foreach (Attachment atc in message.Attachments)
        { 
            if (System.IO.Path.GetExtension(atc.Name) == ".xml")
            {
                String archivoXML_texto = "";
                byte[] allBytes = new byte[atc.ContentStream.Length];
                int bytesRead = atc.ContentStream.Read(allBytes, 0, (int)atc.ContentStream.Length);
                using (MemoryStream memory = new MemoryStream(allBytes))
                {
                    StreamReader archivoXML = new StreamReader(memory);
                    archivoXML_texto = archivoXML.ReadToEnd();
                    archivoXML.Close();
                    memory.Dispose();
                }
                .......
                .......
                }
            }
        }
    }
}

我有一个问题:当我尝试阅读一些邮件的附件时, 我在调试代码时意识到没有阅读附件。 这个事件的奇怪之处在于,它发生在一些电子邮件,以及 gmail 和私人电子邮件(我并不是说所有这些域都不阅读附件),其他人阅读它们很好。

问题:

  1. 我想知道阻止我阅读这些电子邮件的原因是什么。会不会有一些隐私设置,例如在 gmail 中使其阻止代码中的附件?

  2. 如果这是真的,您将如何启用它?还是应该改变代码的实现方式?

  3. 最好的例子是什么?

【问题讨论】:

  • 在您投反对票之前,请编辑/批准您帖子的现有编辑,以便更好地解释您的问题。首先,Title 完全错误。它应该是Cannot red attachments using MailKit。继续前进对于您的问题,您的主要问题也有很多缺陷,无法在 cmets 中描述。据我所知,您使用的是 ImapClient 类,它是 ImapX 或 MailKit 的一部分`但是在您的情况下,它是 MailKit,您应该至少在 Tags 中添加它。但是,要进行故障排除,请转到 here
  • 他没有使用 MailKit,他使用的是 S22.Imap。
  • IMAP 使编写任何此类隐私功能变得困难或不可能。客户端要么有权访问消息的所有部分,要么无权访问。但是,如果您使用某种脑损伤病毒防护工具,可能就是这样。

标签: c# asp.net imap attachment gmail-imap


【解决方案1】:
using (var client = new ImapClient ()) {
            client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
            client.Authenticate ("mailo@gmail.com", "password");
            client.Inbox.Open(FolderAccess.ReadWrite);
            var uids = client.Inbox.Search(SearchQuery.NotSeen);

            foreach (var uid in uids)
            {
                var message = client.Inbox.GetMessage(uid);
                client.Inbox.AddFlags(uid, MessageFlags.Seen, true);
                foreach (var attachment in message.Attachments.OfType<MimePart>())
                {
                    if (System.IO.Path.GetExtension(attachment.FileName) == ".xml")
                    {
                        byte[] allBytes = new byte[attachment.Content.Stream.Length];
                        int bytesRead = attachment.Content.Stream.Read(allBytes, 0, (int)attachment.Content.Stream.Length);
                        string texto_definitivo = "";
                        String archivoXML_textoBase64 = "";
                        using (MemoryStream memory = new MemoryStream(allBytes))
                        {
                            StreamReader archivoXML = new StreamReader(memory);
                            archivoXML_textoBase64 = archivoXML.ReadToEnd();
                            byte[] temp_backToBytes = Convert.FromBase64String(archivoXML_textoBase64);
                            texto_definitivo = Encoding.ASCII.GetString(temp_backToBytes);
                            archivoXML.Close();
                            memory.Dispose();
                        }   

                    }      
                }
            }
            client.Disconnect (true);
        }

将base64之类的文本转换为字符串可能会更好。如果附件文本的内容是直接获取的,而不是转换为字节[],字节[]来字符串附件的内容(出于某种奇怪的原因是base64中的字符串),从字符串base64获取,那将是理想的到字节[],从字节[]到字符串。

如果有人有更好的解决方案,那就太棒了。

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多