We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

IMAP is using Windows Authentication irrespective of provided credentials

Hi Everyone,

We are trying to use IMAP class of Aspose.Email. When I tried this code on my personal Gmail, it works perfectly but when I use this with my organization’s outlook(Exchange Server) it is automatically using my windows account to fetch email. I provided email id, password, server address and port explicitly but somehow I’m connecting to my personal outlook email instead of the given service account. This looks a bit weird. If someone knows what might be happening here, please let me know. Any help is much appreciated. Following is my code. Thanks in advance.

public static void GetEmails2(string path, string email, string pwd, string serverIMAP, int port)
            // The path to the File directory.

            string dataDir = path;
            string server = serverIMAP;
            string emailId = email;
            string passwrd = pwd;
            int serverport = port;

            //Create an instance of the ImapClient class

            ImapClient client = new ImapClient(server, serverport);
            // Set the security mode to implicit
            //client.SecurityOptions = SecurityOptions.SSLImplicit;

                // ExStart:FetchEmailMessagesFromIMAPServer
                // Select the inbox folder and Get the message info collection

                ImapMessageInfoCollection list = client.ListMessages();

                // Download each message
                for (int i = 0; i < list.Count; i++)

                    var dataDirTemp = dataDir + list[i].UniqueId + "\\";

                    _ = Directory.CreateDirectory(dataDirTemp);

                    // Save the EML file locally
                    client.SaveMessage(list[i].UniqueId, dataDirTemp + list[i].UniqueId + ".eml");

                    //Get the email from server
                    Aspose.Email.MailMessage Message = client.FetchMessage(list[i].UniqueId);

                    //Saves all attachments to the local directory by looping through all attachments
                    foreach (var att in Message.Attachments)

                        att.Save(dataDirTemp + att.Name);

                        String extension = Path.GetExtension(dataDirTemp + att.Name);

                        if (extension == ".jpeg" || extension == ".jpg" || extension == ".png" || extension == ".JPEG" || extension == ".JPG" || extension == ".PNG")
                            // Initialize new PDF document
                            Aspose.Pdf.Document doc = new Aspose.Pdf.Document();

                            // Add empty page in empty document
                            Page page = doc.Pages.Add();

                            Aspose.Pdf.Image image = new Aspose.Pdf.Image();

                            image.File = dataDirTemp + att.Name;

                            // Add image on a page

                            // Save output PDF file
                            doc.Save(dataDirTemp + att.Name + ".pdf");

                            Console.WriteLine("individual PDF Created");

                        else if (extension == ".txt" || extension == ".TXT")

                            // Read the text file as array of string
                            var lines = System.IO.File.ReadAllLines(dataDirTemp + att.Name);

                            // Instantiate a Document object by calling its empty constructor
                            Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();

                            // Add a new page in Pages collection of Document
                            Page page = pdfDocument.Pages.Add();

                            // Set left and right margins for better presentation
                            page.PageInfo.Margin.Left = 20;
                            page.PageInfo.Margin.Right = 10;
                            page.PageInfo.DefaultTextState.Font = FontRepository.FindFont("Courier New");
                            page.PageInfo.DefaultTextState.FontSize = 12;

                            foreach (var line in lines)
                                // check if line contains "form feed" character
                                if (line.StartsWith("\x0c"))
                                    page = pdfDocument.Pages.Add();
                                    page.PageInfo.Margin.Left = 20;
                                    page.PageInfo.Margin.Right = 10;
                                    page.PageInfo.DefaultTextState.Font = FontRepository.FindFont("Courier New");
                                    page.PageInfo.DefaultTextState.FontSize = 12;
                                    // Create an instance of TextFragment and
                                    // pass the line to its
                                    // constructor as argument
                                    TextFragment text = new TextFragment(line);

                                    // Add a new text paragraph in paragraphs collection and pass the TextFragment object

                            // Save resultant PDF file
                            pdfDocument.Save(dataDirTemp + att.Name + ".pdf");

                            Console.WriteLine("Text file is converted to pdf");


                            Console.WriteLine("Attachment is not Image or Text.");

                    //Move messages to another folder after processing
                    //client.MoveMessage(list[i].UniqueId, "Downloaded");

                    MemoryStream ms = new MemoryStream();

                    var so = Aspose.Email.SaveOptions.DefaultMhtml;

                    so.SaveAttachments = false;

                    //Copy Mhtml file as a Steam
                    Message.Save(ms, so);

                    Aspose.Words.LoadOptions loadOptions = new Aspose.Words.LoadOptions();

                    loadOptions.LoadFormat = Aspose.Words.LoadFormat.Mhtml;

                    Aspose.Words.Document document = new Aspose.Words.Document(ms, loadOptions);

                    Aspose.Words.Saving.PdfSaveOptions saveOptions = new Aspose.Words.Saving.PdfSaveOptions();

                    document.Save(dataDirTemp + list[i].UniqueId + "out.pdf", saveOptions);

                    Console.WriteLine("Email Converted to pdf");

                    MergeAllPdfs2(Message.Attachments, dataDirTemp, list[i].UniqueId);

                // ExEnd:FetchEmailMessagesFromIMAPServer

                // Disconnect to the remote IMAP server
            catch (Exception ex)
                Console.Write(Environment.NewLine + ex);

        public static void MergeAllPdfs2(Aspose.Email.AttachmentCollection attachments, string dir, string mainFile)
            var i = 0;

            string[] files = new string[50];
            files[0] = dir + mainFile + "out.pdf";
            foreach (var att in attachments)
                files[i] = dir + att.Name + ".pdf";


            // Create PdfFileEditor object
            PdfFileEditor pdfEditor = new PdfFileEditor();

            // Concatenate files
            pdfEditor.Concatenate(files, dir + mainFile + "_MainFile.pdf");


Can you please verify on your end as this is not likely possible. Aspose.Email IMapClient will get connected to an account for which credentials will be set in user name, password, Imap server and port.

@mudassir.fayyaz Thank you for the quick reply,

Thank you @mudassir.fayyaz, I have verified that it was an issue on our end. Sorry for the very very late reply.