Unable to use IMAPX with asp.net mvc

Topics: Archive, Feature Requests, Help requests, Issues, Releases
May 13, 2014 at 12:29 AM
i'm implementing a method to fetch emails from gmail, i'm sharing my code

Action Method:
    public async Task<ActionResult> ConversationHistory(int? id, int? TicketId)
    {
        var viewModel = new CategoryViewModel();

        getMessage();
        return View(viewModel);

    }
GetMessage Method:
    public void getMessage()
    {
        ViewBag.Message = "sadasd";
        var client = new ImapClient();

        client.Port = 993;
        client.SslProtocol = SslProtocols.Default;
        client.Host = "imap.gmail.com";
        client.ValidateServerCertificate = true;

        if (client.Connect())
        {
            if (client.Login("defabc@gmail.com", "212123"))
            {
                var viewModel = new CategoryViewModel();
                var excludeLabels = new string[] { "Processed", "Research", "MANUAL", "Failed", "Success", "Duplicate" };
                var senders = new string[] { "mail-noreply@google.com", "mail-noreply@google.com" };

                ImapX.Folder myInbox = client.Folders.Inbox;

                var query = string.Format("X-GM-RAW \"{0} -({1})\"",
string.Join(" OR ", senders.Select(sender => "(from:" + sender + ")")),
string.Join(" OR ", excludeLabels.Select(label => "(label:" + label + ")")));
                foreach (ImapX.Message m in client.Folders["INBOX"].Messages)
                {
                    ImapX.Message[] messages = myInbox.Search(query, ImapX.Enums.MessageFetchMode.None, 1000);
                    viewModel.Messages = messages.ToList();
                    foreach (var msg in messages)
                    {
                       msg.Download(ImapX.Enums.MessageFetchMode.Basic | ImapX.Enums.MessageFetchMode.GMailExtendedData);
                    }

                }

                // login successful
            }
        }
        else
        {
            ModelState.AddModelError("", "Connection Failed");
        }

    }
In addition i would like to say that i didn't found any suitable solution for this problems since two days, so kindly tell me how can i do this?
Many Thanks
Coordinator
May 13, 2014 at 9:02 AM
Edited May 13, 2014 at 9:17 AM
Hi usamasheikh,

can you give more details? Do you get any exceptions? Can you connect? Which step exactly doesn't work?

One guess I have is that google not lets you sign into your account as it's being recognized as an unusual place. For more details see this thread: https://imapx.codeplex.com/discussions/446860

Greets,

Pavel
May 13, 2014 at 2:51 PM
hi pavel_azanov,

thanks for reviewing my problem i'm really happy that you replied because i'm searching for the solution from 7 days. now get back to the problem. in my action method it is not giving any exception, it connects successfully but i can't see any messages on the view page of my MVC Application.
Coordinator
May 13, 2014 at 5:37 PM
Hi usamasheikh,

you are not checking the result of the login method, and it is most likely false in your case. Can you check on this?

What's also helpful, you can create a log file of all requests and responses. For this do the following:
// Add this on application startup, remember to change the path 
Debug.Listeners.Add(new TextWriterTraceListener(@"C:\users\public\test.txt"));
Debug.AutoFlush = true;

// After you created the client, set:
client.IsDebug = true;
After that a log file will be generated and you can send it to info@imapx.org, I will review it and can tell you more.

Greets,

Pavel
May 14, 2014 at 11:29 PM
hi pavel_azanov

i have sent you an email with attachment of log text file at info@imapx.org, please review and guide me becasue i'm just stucked into this problem since many days, even i didn't found any suitable solution on internet regarding this problem, i have also mentioned the exception detail in email.

i cannot retrieve inbox messages bodies and cannot embed them in my MVC View. please guide me with best solution.

i'm waiting for your reply

Greets,
Usama