Archived Forum Post

Index of archived forum posts

Question:

Is this Chilikat IMAP Email code in C#

Jun 02 '16 at 09:49

Hi,

I am very new to C# code in VS2010. I have had an application written that is not 100% to what I want so I am learning fast and making the changes myself.

One of the issues I have is that the email import is not 100% right and I am told that it should be chilikat IMAP. I have just bought the chilikat imap package so hopefully that might help, but can anyone tell me if the below code is chilikat IMAP email reading of the inbox? If it's not, what do i need to change to get it to read IMAP.

I am also looking to add from and date of the emails to the fields that show when the form loads. I suspect this would be fairly easy to troubleshoot through but any ideas would be appreciated.

string appPath = Application.StartupPath; string emailidPath = @"seenUidls.txt"; string fullpath = Path.Combine(appPath, emailidPath);

            //  The mailman object is used for receiving (POP3)
            //  and sending (SMTP) email.
            Chilkat.MailMan mailman = new Chilkat.MailMan();
            //  Any string argument automatically begins the 30-day trial.
            bool success;
            success = mailman.UnlockComponent("30-day trial");
            if (success != true)
            {
                MessageBox.Show("Component unlock failed");
                return;
            }
            //  Set the POP3 server's hostname
            mailman.MailHost = HostName;
            mailman.MailPort = Convert.ToInt32(Port);
            //  Set the POP3 login/password.
            mailman.PopUsername = UserName.Trim().ToString();
            mailman.PopPassword = DecodeFrom64(Password);
            /////////////////
            //  You may keep a list of already-seen UIDLs in a text file:
            //  If this is the first time you're running this example,
            //  create an empty text file named "seenUidls.txt";
            Chilkat.StringArray saSeenUidls = new Chilkat.StringArray();
            success = saSeenUidls.LoadFromFile(fullpath);
            if (success != true)
            {
                Console.WriteLine("failed to load seenUidls.txt");
                return;
            }
            //  Get the complete list of UIDLs on the mail server.
            Chilkat.StringArray saUidls = null;
            saUidls = mailman.GetUidls();

            if (saUidls == null)
            {
                Console.WriteLine(mailman.LastErrorText);
                return;
            }
            //  Create a new string array object (it's an object, not an actual array)
            //  and add the UIDLs from saUidls that aren't already seen.
            Chilkat.StringArray saUnseenUidls = new Chilkat.StringArray();
            int k;
            int p;
            p = saUidls.Count;
            for (k = 0; k <= p - 1; k++)
            {
                if (saSeenUidls.Contains(saUidls.GetString(k)) != true)
                {
                    saUnseenUidls.Append(saUidls.GetString(k));
                }

            }

            if (saUnseenUidls.Count == 0)
            {
                button3.Visible = false;
                btnCheckAll.Visible = false;
                MessageBox.Show("No unseen emails found!");

                return;
            }
            /////////////

            Chilkat.EmailBundle bundle = null;
            //  Copy the all email from the user's POP3 mailbox        
            //  into a bundle object.  The email remains on the server.             
            Cursor.Current = Cursors.WaitCursor;
            bundle = mailman.FetchMultiple(saUnseenUidls);

            //bundle = mailman.CopyMail(); 
            Cursor.Current = Cursors.AppStarting;
            if (bundle == null)
            {
                MessageBox.Show(mailman.LastErrorText);
                return;
            }
            //string dirPath;
            //dirPath = "c:/myAttachments";
            int i;
            Chilkat.Email email = null;
            for (i = 0; i <= bundle.MessageCount - 1; i++)
            {
                email = bundle.GetEmail(i);
                //  You may save all the attachments to the specified directory
                //  by calling SaveAllAttachments.
                //  The SaveAllAttachments method will automatically create the directory
                //  if it does not already exist.
                //success = email.SaveAllAttachments(dirPath);
                //if (success != true)
                //{
                //    MessageBox.Show(email.LastErrorText);
                //    return;
                //}
                //  The OverwriteExisting property controls whether already-existing files
                //  are automatically overwritten.  By default, it is set to true so that existing
                //  files will be overwritten.
                //  Setting OverwriteExisting = false will cause the attachment-saving methods to generate
                //  unique filenames if a file with the same name already exists.  The actual filename(s)
                //  saved will be present by calling GetAttachmentFilename for each attachment *after*
                //  saving.
                //  For example...
                //email.OverwriteExisting = false;
                //success = email.SaveAllAttachments(dirPath);
                //if (success != true)
                //{
                //    MessageBox.Show(email.LastErrorText);
                //    return;
                //}
                int n;
                n = email.NumAttachments;
                int j;
                string Subject = email.Subject;
                table.Rows.Add(Subject, email, n);
                //for (j = 0; j <= n - 1; j++)
                //{
                //    //  If the attachment filename was changed to prevent overwriting,
                //    //  GetAttachmentFilename will return the new filename.
                //    //textBox1.Text += email.GetAttachmentFilename(j) + "\r\n";
                //}
                //  You may also save individual attachments:
                //for (j = 0; j <= n - 1; j++)
                //{
                //    //textBox1.Text += "Original Filename: "
                //    //     + email.GetAttachmentFilename(j) + "\r\n";
                //    success = email.SaveAttachedFile(j, dirPath);
                //    if (success != true)
                //    {
                //        MessageBox.Show(email.LastErrorText);
                //        return;
                //    }
                //    //  If OverwriteExisting = true, the saved filename will always equal the original filename,
                //    //  unless there are characters present in the filename that are not allowed by Windows,
                //    //  such as * ? < > | etc.  In those cases the illegal characters are either removed or replaced
                //    //  with underscore characters to allow the file to be saved.
                //    //  textBox1.Text += "Saved Filename: " + email.GetAttachmentFilename(j) + "\r\n";
                //}
            }
            saUidls.SaveToFile(fullpath);
            dataGridView1.DataSource = table.DefaultView;
            DataGridViewCheckBoxColumn checkBoxColumn3 = new DataGridViewCheckBoxColumn();
            checkBoxColumn3.HeaderText = "Check";
            checkBoxColumn3.Width = 60;
            checkBoxColumn3.Name = "Check";
            dataGridView1.Columns.Insert(3, checkBoxColumn3);
            dataGridView1.Columns["Subject"].Width = 710;
            dataGridView1.Columns["MetaData"].Visible = false;
        }
        catch (Exception ex)
        {
            ExceptionHelper.LogFile(ex.Message, ex.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.FindForm().Name);
        }
    }

Answer

The Chilkat.MailMan object is for POP3 and SMTP. You want to use the Chilkat.Imap object for IMAP.

I'm going to delete this post after a day because the title and content could mislead other developers (i.e. code showing MailMan, but the title is about IMAP)

See the IMAP examples here: https://www.example-code.com/csharp/imap.asp