public ActionResult SubmitDownload(EmailDownloadViewModel model)
        {
            try
            {
                EmailDownloadDto emailDownloadDto = new EmailDownloadDto()
                {
                    Email         = model.Email,
                    Password      = model.Password,
                    Port          = model.Port,
                    ServerAddress = model.ServerAddress,
                    Username      = model.Username,
                    UseSSL        = model.UseSSL
                };

                FetchListServiceResponse <EmailXML> downloadedEmails = _emailService.DownloadEmailMessagesFromEmailAccount(emailDownloadDto);
                this.AddToastMessage("Success", "Emails were downloaded.", ToastType.Success);
            }
            catch (Exception e)
            {
                this.AddToastMessage("Error", e.Message, ToastType.Error);
            }

            return(View("Index", model));
        }
Exemplo n.º 2
0
        public FetchListServiceResponse <EmailXML> DownloadEmailMessagesFromEmailAccount(EmailDownloadDto emailDownloadDto)
        {
            FetchListServiceResponse <EmailXML> response = new FetchListServiceResponse <EmailXML>();
            HashSet <EmailXML> emails = new HashSet <EmailXML>();

            if ((emailDownloadDto.ServerAddress == "pop.gmail.com" || emailDownloadDto.ServerAddress == "imap.gmail.com") &&
                emailDownloadDto.UseSSL == false)
            {
                throw new Exception("Connection to GMAIL requires SSL connection.");
            }

            try
            {
                using (Imap imap = new Imap())
                {
                    imap.Connect(emailDownloadDto.ServerAddress, emailDownloadDto.Port, emailDownloadDto.UseSSL);
                    imap.Login(emailDownloadDto.Username, emailDownloadDto.Password);

                    MailBuilder builder = new MailBuilder();

                    imap.SelectInbox();

                    HashSet <long> emailUIDs = new HashSet <long>(imap.GetAll().Take(100));

                    foreach (long uid in emailUIDs)
                    {
                        IMail emailRaw = builder.CreateFromEml(imap.GetMessageByUID(uid));

                        EmailXML emailXml = new EmailXML()
                        {
                            InReplyToId = emailRaw.InReplyTo,
                            MessageId   = emailRaw.MessageID,
                            Sender      = emailRaw.Sender.Address,
                            RawSender   = emailRaw.Sender.Name,
                            Sent        = emailRaw.Date,
                            Subject     = emailRaw.Subject
                        };

                        emails.Add(emailXml);
                    }
                    imap.Close();
                }

                response.Items     = emails;
                response.Succeeded = true;

                CreateEmailXMLFile(emails, response);
            }
            catch (Exception e)
            {
                response.Succeeded = false;
                throw new Exception($"Download failed with an error: {e.Message}");
            }

            return(response);
        }