Folder.AppendMessage hangs and does not return

Jul 15, 2013 at 9:35 PM
Here is my code. What should I be doing? The call to append message hangs.

static void CreateMessage(Folder value)
    {
        Message m = new Message();
        MailAddress adr = new MailAddress("xxxxxxx", "xxxxxxxxx");
        MessageContent mc = new MessageContent();
        mc.TextData = "Message Test from visual studio";
        m.To.Add(adr);
        m.Subject = "test from visual studio";
        m.TextBody = mc;          

        bool retVal = value.AppendMessage(m, ImapFlags.DRAFT);
    }
Debug windows shows the following:

IMAP0015 SELECT "INBOX"
  • 1 EXISTS
  • 0 RECENT
  • FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
  • OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
  • OK [UNSEEN 1] Is the first unseen message
  • OK [UIDVALIDITY 14] UIDVALIDITY value
  • OK [UIDNEXT 21] The next unique identifier value
    IMAP0015 OK [READ-WRITE] SELECT completed.
    IMAP0016 APPEND "INBOX" (\Draft) {448}
  • Ready for additional command text.
Jul 16, 2013 at 12:10 AM
Hi Pavel,

I found the bug in the code and have developed a work around. The issue is in sending the append request, via the current sendAndRecieve call that is part of the base library. Bellow is the fix.

The following should go into ImapBase.cs

public bool SendAndReceiveMsg(string command, ref ArrayList sResultArray, Message msg)
    {
        bool result = true;
        CommandCount++;
        string text = string.Concat(new object[]
        {
            "IMAP00", 
            CommandCount, 
            " ", 
            command
        });
        byte[] bytes = Encoding.ASCII.GetBytes(text.ToCharArray());
        if (isDebug)
        {
            Console.WriteLine(text);
        }
        try
        {
            if (UseSSL)
            {

                ImapSslStream.Write(bytes, 0, bytes.Length);
            }
            else
            {
                ImapStream.Write(bytes, 0, bytes.Length);
            }
            bool flag = true;
            while (flag)
            {
                string text2 = UseSSL ? ImapSslStreamReader.ReadLine() : ImapStreamReader.ReadLine();
                if (isDebug)
                {
                    Console.WriteLine(text2);
                }
                if (text2 != null)
                {
                    sResultArray.Add(text2);
                    if (text2.StartsWith("IMAP00" + CommandCount + " OK"))
                    {
                        flag = false;
                    }
                    else
                    {
                        if (text2.StartsWith("IMAP00" + CommandCount + " NO"))
                        {
                            flag = false;
                            result = false;
                        }
                        else
                        {
                            if (text2.StartsWith("IMAP00" + CommandCount + " BAD"))
                            {
                                flag = false;
                                result = false;
                            }
                            else
                            {
                              //
                              // Server is waiting for the message
                              //
                                if (text2.StartsWith("+ ") )
                                {
                                    string data = msg.MessageBuilder();
                                    this.SendData(data);
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception)
        {
            result = false;
        }
        return result;
    }
The following should go into folder.cs

public bool AppendMessage(Message msg, string flag)
    {
        if (Client == null || !Client.IsConnected)
        {
            throw new ImapException("Dont Connect");
        }
        if (msg == null)
        {
            throw new ImapException("Message is null");
        }
        string selectedFolder = Client.SelectedFolder;
        Select();
        var arrayList = new ArrayList();
        string text = msg.MessageBuilder();
        int length = text.Length;
        if (string.IsNullOrEmpty(flag))
        {
            flag = "\\draft";
        }
        string command = string.Concat(new object[]
                                           {
                                               "APPEND \"",
                                               FolderPath,
                                               "\" (",
                                               flag,
                                               ") {",
                                               length - 2,
                                               "}\r\n"
                                           });
        if (Client.SendAndReceiveMsg(command, ref arrayList, msg) )
        {
            Client.SelectFolder(selectedFolder);
            return true;
        }
        Client.SelectFolder(selectedFolder);
        return false;
    }
Coordinator
Jul 16, 2013 at 9:01 PM
Hi hank6764,

thank you really much for the code updates! I will take a closer look at it, and include your corrections.


Best regards,

Pavel Azanov
Coordinator
Sep 10, 2013 at 10:10 AM
Good day hank6764,

your code snippet is now part of the library, i modified it in some parts to include the latest fixes.

Thank you really much!

Greets,

Pavel