Search code examples
c#gmailimapattachmentimapx

Gmail IMAP - Attachments not appearing


I have been working on an IMAP client to get emails from Gmail. My application worked fine until about an hour ago, when attachments stopped being retrieved.

The connection and messaging is being handled by imapX.

Connection is OKAY Login is OKAY Getting folders is OKAY Getting messages is OKAY

At this point attachments.Count == 0. It was working earlier this afternoon so I wonder if I have been over testing and Google have blacklisted my computer for a while? Does anyone know if this is the case? - Been running perhaps once every 5-10 minutes, maybe more at times so this could be a plausible issue.

I have attempted to send a new email with a totally new file and it still does not see the attachment (but it is (always) seeing the messages themselves).

Can any anyone shine some light on this issue?

EDIT : Header includes the following tag {[X-MS-Has-Attach, yes]}

EDIT (code) :

private void PollMailFolders(object state)
    {
        try
        {

            if(_imapClient == null || !_imapClient.IsConnected)
                _imapClient = new ImapClient(_config.Server, _config.Port, true);

            if (_imapClient.Connection())
            {
                if(!_imapClient.IsLogined)
                    _imapClient.LogIn(_config.Username, _config.Password);

                string dateSearch = string.Format(
                    "SINCE {0:d-MMM-yyyy}{1}", DateTime.Today.AddDays(-_config.HistoryOnStartupDays),
                                                  _isFirstTime ? "" : " UNSEEN");

                _isFirstTime = false;

                foreach (Folder folder in _imapClient.Folders["SSForecasts"].SubFolder)
                {
                    var messages = _imapClient.Folders[folder.Name].Search(dateSearch, false);

                    foreach (Message m in messages)
                    {
                        m.Process();

                        foreach (var a in m.Attachments)
                        {
                            SendDataToParser(_encoding.GetString(a.FileData), folder.Name);
                        }
                        m.SetFlag(ImapFlags.SEEN);
                    }
                }
            }
        }
        catch(Exception e)
        {
            _diagnostics.Logger.ErrorFormat("Error in PollMailFolders: {0}", e);
        }
    }

Solution

  • I have produced a work around which allows me to get the attachment data. Not the solution I had in mind, though it does work.

    Simple filename extension check followed by a conversion of the message data.

    BTW: _encoding = Encoding.GetEncoding(1252);

                if (bodyPart.ContentFilename.EndsWith(".csv"))
                {
                    return _encoding.GetString(Convert.FromBase64String(bodyPart.ContentStream));
                }