Exemple #1
0
 private static void saveLogMail(LogEmail logMail)
 {
     try
     {
         using (var context = new TicketDbEntities())
         {
             context.LogEmail.Add(logMail);
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         _log.LogError(ex.Message, "saveLogMail<<-- Guarda en Bd los emails -->>");
     }
 }
Exemple #2
0
        public static void LogInLogOut()
        {
            try
            {
                Mail email = new Mail();
                Console.WriteLine("Se lee el email: " + _smkInfo.UserName);
                using (ImapClient client = new ImapClient(_smkInfo.HostName, _smkInfo.Port, _smkInfo.UserName, _smkInfo.Password, AuthMethod.Login, true))
                {
                    // Returns a collection of identifiers of all mails matching the specified search criteria.
                    uint lastEmail          = GetLastUid();
                    IEnumerable <uint> uids = client.Search(SearchCondition.Subject(_smkInfo.Subject).And(SearchCondition.GreaterThan(lastEmail)));
                    uids = GetUidEmail(uids.ToList());

                    List <Mail> messages = new List <Mail>();
                    foreach (var item in uids)
                    {
                        messages.Add(new Mail
                        {
                            Uid         = item,
                            MailMessage = client.GetMessage(item)
                        });
                    }
                    foreach (var item in messages)
                    {
                        var date             = item.MailMessage.Date();
                        int totalAttachments = item.MailMessage.Attachments.Count;

                        LogEmail logMail = new LogEmail
                        {
                            BodyMail      = item.MailMessage.Body,
                            DateMail      = DateTime.Parse(item.MailMessage.Date().ToString()),
                            FromMail      = item.MailMessage.From.ToString(),
                            IsLoad        = false,
                            NoAttachments = item.MailMessage.Attachments.Count,
                            SubjectMail   = item.MailMessage.Subject,
                            UidMail       = int.Parse(item.Uid.ToString())
                        };
                        saveLogMail(logMail);

                        for (int i = 0; i < totalAttachments; i++)
                        {
                            var stream = item.MailMessage.Attachments[i].ContentStream;

                            // var path = System.IO.Directory.GetCurrentDirectory();
                            var        path       = _smkInfo.Path + @"UnSeen\";
                            FileStream fileStream = File.Create(path + item.MailMessage.Attachments[i].Name, (int)stream.Length);
                            // Initialize the bytes array with the stream length and then fill it with data
                            byte[] bytesInStream = new byte[stream.Length];
                            stream.Read(bytesInStream, 0, bytesInStream.Length);
                            // Use write method to write to the file specified above
                            fileStream.Write(bytesInStream, 0, bytesInStream.Length);
                            //Close the filestream
                            fileStream.Close();

                            ReadExcel(item.Uid, item.MailMessage.Attachments[i].Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.LogError(ex.Message, "saveLogMail<<--Lee el correo inserta en BD descarga los attachments-->>");
            }
        }