示例#1
0
        public int GetMails(short idBox, Boolean IsProtocolBox, string boxRecipient, Func <string, string, bool> headerExistHandler, string defaultSubject)
        {
            MailBuilder builder = new MailBuilder();
            var         counter = 0;

            using (Imap mailClient = CreateMailClient())
            {
                List <long> mails = GetMailsUids(mailClient, Pars.ImapSearchFlag, Pars.ImapStartDate, Pars.ImapEndDate);

                for (int messageCounter = mails.Count - 1;
                     messageCounter >= 0 && counter < Pars.MaxMailsForSession;
                     messageCounter--)
                {
                    if (Pars.UserCanceled())
                    {
                        return(counter);
                    }

                    long uid = mails[messageCounter];

                    LogAction(string.Format("Get Headers Mail Uid:{0}", uid));
                    byte[] headers    = mailClient.GetHeadersByUID(uid);
                    string headerHash = headers.ComputeSHA256Hash();

                    // Verifico se già presente, controlla header hash
                    MessageInfo info = mailClient.GetMessageInfoByUID(uid);
                    LogAction(String.Format("Check Header Checksum Mail Uid:{0} / Subject:{1} / Data:{2}", uid, info.Envelope.Subject, info.Envelope.Date));
                    if (headerExistHandler(headerHash, boxRecipient))
                    {
                        LogAction(String.Format("Mail Uid:{0} - Skipped", uid));
                        //TODO: inserire logica di 'eliminazione o identificare la pec come già presente
                        continue;
                    }

                    //controlla se ho già scaricata nel drop folder
                    if (MailInfo.CheckMailExist(Pars.DropFolder, headerHash))
                    {
                        //TODO: inserire logica di 'emilinazione o identificare la pec come già presente
                        continue;
                    }

                    string outpathEml, outpathXml;
                    Utils.GetDropFilenames(Pars.DropFolder, out outpathEml, out outpathXml);

                    //mail info
                    MailInfo mInfo = new MailInfo
                    {
                        Client           = MailInfo.ClientType.Imap,
                        EmlFilename      = outpathEml,
                        IDPECMailBox     = idBox,
                        IsProtocolBox    = IsProtocolBox,
                        MailBoxRecipient = boxRecipient,
                        MailUID          = uid.ToString(CultureInfo.InvariantCulture)
                    };

                    IMail email = builder.CreateFromEml(headers);
                    mInfo.Parse(email);
                    mInfo.Body = DownloadBody(mailClient, info);
                    mInfo.Size = GetMailSize(info);
                    mInfo.SaveAs(outpathXml);

                    //download eml
                    LogAction("Download Mail Uid:" + uid);
                    byte[] eml = mailClient.GetMessageByUID(uid);
                    File.WriteAllBytes(outpathEml, eml);

                    //salva eml hash del messaggio
                    mInfo.HeaderHash = headerHash;
                    mInfo.EmlHash    = eml.ComputeSHA256Hash();
                    mInfo.UpdateStatus(MailInfo.ProcessStatus.Downloaded);
                    mInfo.Save();
                    counter++;
                }

                mailClient.Close();
                return(counter);
            }
        }
示例#2
0
        public int GetMails(short idBox, Boolean IsProtocolBox, string boxRecipient, Func <string, string, bool> headerExistHandler, string defaultSubject)
        {
            var builder = new MailBuilder();
            var counter = 0;

            using (Pop3 mailClient = CreateMailClient())
            {
                var currentStats = mailClient.GetAccountStat();

                for (var messageCounter = (int)currentStats.MessageCount;
                     messageCounter > 0 && counter < Pars.MaxMailsForSession;
                     messageCounter--)
                {
                    if (Pars.UserCanceled())
                    {
                        return(counter);
                    }

                    var uid = mailClient.GetUID(messageCounter);

                    LogAction("Get Headers Mail Uid:" + uid);
                    byte[] headers    = mailClient.GetHeadersByUID(uid);
                    string headerHash = headers.ComputeSHA256Hash();

                    // Verifico se già presente, controlla header hash
                    var email = builder.CreateFromEml(headers);
                    LogAction(string.Format("Check Headers Mail Uid:{0} / Subject:{1} / Data:{2}", uid, email.Subject, email.Date));
                    if (headerExistHandler(headerHash, boxRecipient))
                    {
                        continue;
                    }

                    //controlla se ho già scaricata nel drop folder
                    if (MailInfo.CheckMailExist(Pars.DropFolder, headerHash))
                    {
                        continue;
                    }

                    string outpathEml, outpathXml;
                    Utils.GetDropFilenames(Pars.DropFolder, out outpathEml, out outpathXml);

                    //mail info
                    MailInfo mInfo = new MailInfo
                    {
                        Client           = MailInfo.ClientType.Pop3,
                        EmlFilename      = outpathEml,
                        IDPECMailBox     = idBox,
                        IsProtocolBox    = IsProtocolBox,
                        MailBoxRecipient = boxRecipient,
                        MailUID          = uid
                    };

                    mInfo.Parse(email);
                    mInfo.Body = "#";
                    mInfo.Size = GetMailSize(mailClient, messageCounter);
                    mInfo.SaveAs(outpathXml);

                    //download eml
                    LogAction("Download Mail Uid:" + uid);
                    byte[] eml = mailClient.GetMessageByUID(uid);
                    File.WriteAllBytes(outpathEml, eml);

                    //Aggiorna il Body
                    //Pop3 deve forzatamente scaricare l'intero messaggio per ottenere il body della mail
                    email = builder.CreateFromEml(eml);

                    mInfo.HeaderHash = headerHash;
                    mInfo.EmlHash    = eml.ComputeSHA256Hash();
                    mInfo.Body       = email.GetBodyAsHtml();
                    mInfo.UpdateStatus(MailInfo.ProcessStatus.Downloaded);
                    mInfo.Save();

                    counter++;
                }

                return(counter);
            }
        }