Ejemplo n.º 1
0
        /// <summary>
        /// This will overwrite the files for which, the server pulls the same names
        /// </summary>
        public void getAttachmentsThroughMessageSummary()
        {
            using (var client = new ImapClient())
            {
                client.ServerCertificateValidationCallback = (s, c, ch, e) => true;
                client.Connect(Constant.GoogleImapHost, Constant.ImapPort, SecureSocketOptions.SslOnConnect);
                client.AuthenticationMechanisms.Remove(Constant.GoogleOAuth);
                client.Authenticate(Constant.GoogleUserName, Constant.GenericPassword);

                if (client.IsConnected == true)
                {
                    FolderAccess            inboxAccess = client.Inbox.Open(FolderAccess.ReadWrite);
                    IList <IMessageSummary> items       = client.Inbox.Fetch(0, -1,
                                                                             MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.Envelope);
                    int unnamed = 0;

                    foreach (var message in items)
                    {
                        foreach (var attachment in message.BodyParts)
                        {
                            MimeKit.MimePart mime =
                                (MimeKit.MimePart)client.Inbox.GetBodyPart(message.UniqueId, attachment);
                            string fileName = mime.FileName;

                            if (string.IsNullOrEmpty(fileName))
                            {
                                fileName = string.Format("unnamed-{0}", ++unnamed);
                            }

                            FormatOptions options = FormatOptions.Default.Clone();
                            options.ParameterEncodingMethod = ParameterEncodingMethod.Rfc2047;

                            using (
                                FileStream stream =
                                    File.Create(Path.Combine("C:\\Users\\maddirsh\\Desktop\\MimeKit\\", fileName)))
                            {
                                mime.ContentObject.DecodeTo(stream);
                            }

                            Console.WriteLine("End");
                        }
                    }
                }
                else
                {
                    throw new Exception();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Will Download both the attachments and inline attachments in one single directory
        /// Will not OVERWRITE files that have the same filenames
        /// </summary>
        public void DownloadAttachmentsFileIoWay()
        {
            using (var client = new ImapClient())
            {
                client.ServerCertificateValidationCallback = (s, c, ch, e) => true;
                client.Connect(Constant.GoogleImapHost, Constant.ImapPort, SecureSocketOptions.SslOnConnect);
                client.AuthenticationMechanisms.Remove(Constant.GoogleOAuth);
                client.Authenticate(Constant.GoogleUserName, Constant.GenericPassword);

                client.Inbox.Open(FolderAccess.ReadWrite);
                IList <UniqueId> uids = client.Inbox.Search(SearchQuery.All);
                int anotherOne        = 0;

                foreach (UniqueId uid in uids)
                {
                    MimeMessage message = client.Inbox.GetMessage(uid);

                    foreach (MimeEntity attachment in message.BodyParts)
                    {
                        string fileName          = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;
                        string regularAttachment = string.Concat(Constant.HpRegularAttachmentsDirectory, fileName);

                        if (!string.IsNullOrWhiteSpace(fileName))
                        {
                            if (File.Exists(regularAttachment))
                            {
                                string extension = Path.GetExtension(regularAttachment);
                                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(regularAttachment);
                                fileName = string.Format(fileNameWithoutExtension + "-{0}" + "{1}", ++anotherOne,
                                                         extension);
                                regularAttachment = Path.Combine(Constant.HpRegularAttachmentsDirectory, fileName);
                            }

                            using (var attachmentStream = File.Create(regularAttachment))
                            {
                                MimeKit.MimePart part = (MimeKit.MimePart)attachment;
                                part.ContentObject.DecodeTo(attachmentStream);
                            }

                            Console.WriteLine("Downloaded: " + fileName);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void IterateThroughEmail()
        {
            int anotherOne = 0;

            using (var client = new ImapClient())
            {
                //authenticate
                client.ServerCertificateValidationCallback = (s, c, ch, e) => true;
                client.Connect(Constant.GoogleImapHost, Constant.ImapPort, SecureSocketOptions.SslOnConnect);
                client.AuthenticationMechanisms.Remove(Constant.GoogleOAuth);
                client.Authenticate(Constant.GoogleUserName, Constant.GenericPassword);

                client.Inbox.Open(FolderAccess.ReadWrite);

                //IMailFolder personal = client.GetFolder(client.PersonalNamespaces[0]);

                IMailFolder personal = client.GetFolder(Constant.EnrollmentStudentServicesFolder);
                foreach (IMailFolder folder in personal.GetSubfolders())
                {
                    Console.WriteLine(folder.Name);
                    //give each folder read and write access
                    folder.Open(FolderAccess.ReadWrite);
                    //uids in the specific folder
                    IList <UniqueId> uids = folder.Search(SearchQuery.All);
                    foreach (var x in uids)
                    {
                        //in folder A
                        MimeMessage message = folder.GetMessage(x);
                        string      subject = message.Subject;
                        string      body    = message.TextBody;
                        Console.WriteLine(subject + "\n" + body);

                        foreach (MimeEntity attachment in message.BodyParts)
                        {
                            string fileName     = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;
                            string anAttachment = string.Concat(Constant.MimeKitAttachmentsDirectoryWork, fileName);

                            if (!string.IsNullOrWhiteSpace(fileName))
                            {
                                if (File.Exists(anAttachment))
                                {
                                    string extension = Path.GetExtension(anAttachment);
                                    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(anAttachment);
                                    fileName = string.Format(fileNameWithoutExtension + "-{0}" + "{1}", ++anotherOne,
                                                             extension);
                                    anAttachment = string.Concat(Constant.MimeKitAttachmentsDirectoryWork, fileName);
                                }

                                using (FileStream attachmentStream = File.Create(anAttachment))
                                {
                                    MimeKit.MimePart part = (MimeKit.MimePart)attachment;
                                    part.ContentObject.DecodeTo(attachmentStream);
                                }

                                Console.WriteLine("Downloaded: " + fileName);
                            }
                        }
                    }
                }
                client.Disconnect(true);
            }
        }