Beispiel #1
0
        public AeEmailClient(IConfiguration configuration)
        {
            configuration = configuration.GetSubsection("EmailClient");
            client        = new ImapClient(
                configuration.GetValue("Host"),
                configuration.GetValue("Username"),
                configuration.GetValue("Password"),
                ImapClient.AuthMethods.Login,
                configuration.GetValue <int>("Port"),
                configuration.GetValue <bool>("Secure"),
                configuration.GetValue <bool>("SkipSslValidation"));

            var unreadMessages = client.SearchMessages(SearchCondition.Unseen());

            foreach (var message in unreadMessages)
            {
                client.SetFlags(Flags.Seen, message.Value);
            }

            unread.AddRange(unreadMessages.Select(message => GetInputMailMessage(message.Value)));

            client.NewMessage += (sender, args) =>
            {
                var message = client.GetMessage(args.MessageCount - 1, false, true);
                client.SetFlags(Flags.Seen, message);
                unread.Add(GetInputMailMessage(message));

                if (null != MailMessageReceived)
                {
                    MailMessageReceived(this, args);
                }
            };
        }
        public Task <string> GetTwoFactorCodeAsync()
        {
            return(Task.Run(async() =>
            {
                var regex = new Regex(@"(?<=<strong>)\d{6}");

                try {
                    using (var client = new ImapClient(_hostName, _username, _password, AuthMethods.Login, _port, _useUseSsl))
                    {
                        for (var i = 0; i < 5; i++)
                        {
                            var messages = client.SearchMessages(SearchCondition.Unseen().And(SearchCondition.To(_emailto)));
                            foreach (var match in messages.Select(message => regex.Match(message.Value.Body)).Where(match => match.Success))
                            {
                                return match.Value;
                            }
                            await Task.Delay(5000);
                        }
                    }
                }
                catch (Exception ex)
                {
                    var fehler = ex.Message;
                }
                throw new FutException("Unable to find the two-factor authentication code.");
            }));
        }
Beispiel #3
0
        public static void Main()
        {
            const string host     = "imap.gmail.com";
            const string username = "******";
            const string password = "******";
            const short  port     = 993;
            const bool   secure   = true;

            try
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

                using var client = new ImapClient(host, username, password, AuthMethods.Login, port,
                                                  secure);

                var messages = client.SearchMessages(SearchCondition.Unseen());

                var messagesList = messages.Select(s => s.Value.Uid)
                                   .Select(messageId => client.GetMessage(messageId, false, true))
                                   .Select(dummy => dummy).ToList();

                foreach (var message in messagesList)
                {
                    Console.WriteLine(message.Subject);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public void Message_With_Attachments()
        {
            using (ImapClient imap = new ImapClient("imap-mail.outlook.com", "*****@*****.**", "password", AuthMethods.Login, 993, true))
            {
                var msg = imap.SearchMessages(SearchCondition.Larger(100 * 1000)).FirstOrDefault().Value;

                var att = msg.Attachments;
            }
        }
Beispiel #5
0
        public MailMessage GetLastEmail(string username, string password = "******")
        {
            using (var imap = new ImapClient("imap.gmail.com", username, password, port: 993, secure: true, skipSslValidation: true))
            {
                var msgs = imap.SearchMessages(
                    SearchCondition.Unseen().And(
                        SearchCondition.From("*****@*****.**"),
                        SearchCondition.SentSince(DateTime.Now.Subtract(TimeSpan.FromMinutes(20)))
                        ));

                return(msgs.Length > 0 ? msgs[0].Value : null);
            }
        }
Beispiel #6
0
        static void connection()
        {
            string path   = "../../docs";
            string server = "imap.gmail.com";
            string email  = "";
            string pw     = "";

            try
            {
                using (ImapClient ic = new ImapClient(server, email, pw, AuthMethods.Login, 993, true))
                {
                    ic.SelectMailbox("inbox");
                    Console.WriteLine("running");
                    int x = 0;
                    Lazy <AE.Net.Mail.MailMessage>[] messages = ic.SearchMessages(SearchCondition.Undeleted(), false);

                    foreach (Lazy <AE.Net.Mail.MailMessage> msg in messages)
                    {
                        AE.Net.Mail.MailMessage m = msg.Value;
                        string sender             = m.From.Address;
                        string FileName           = string.Empty;

                        if (sender == "*****@*****.**")
                        {
                            FileName = "../../docs/boardingpass";
                            Directory.CreateDirectory(path);
                            foreach (AE.Net.Mail.Attachment attachment in m.Attachments)
                            {
                                if (attachment.Filename.Contains("invoice") == false)
                                {
                                    x++;
                                    FileName = FileName + x;
                                    attachment.Save(FileName + Path.GetExtension(attachment.Filename));
                                    Pdf2text(FileName);
                                }
                            }

                            sendemail(cl);
                            Directory.Delete(path, true);
                            ic.DeleteMessage(m);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
            }
        }
Beispiel #7
0
        public void MarkAllAsSeen(string username, string password = "******")
        {
            using (var imap = new ImapClient("imap.gmail.com", username, password, port: 993, secure: true, skipSslValidation: true))
            {
                var msgs = imap.SearchMessages(
                    SearchCondition.Unseen().And(
                        SearchCondition.From("*****@*****.**")
                        ));

                foreach (var msg in msgs)
                {
                    imap.SetFlags(Flags.Seen, msg.Value);
                }
            }
        }
 // This is where all the magic happens.
 void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     StatusBarText = "Connecting to server...";
     try
     {
         ImapClient ic = new ImapClient(ServerName, Username, Password, AuthMethods.Login, 993, true);
         ic.SelectMailbox("INBOX");
         Lazy <MailMessage>[] messages = ic.SearchMessages(SearchCondition.Unseen(), true, false);
         display.printNumber(messages.Length);
         StatusBarText = "Number of unread messages: " + messages.Length;
         ic.Disconnect();
         ic.Dispose();
     }
     catch (Exception ex)
     {
         StatusBarText = ex.Message;
         StopRunning();
     }
 }
Beispiel #9
0
        public async Task <string> getImageFrom(string from, int index = 0)
        {
            // Connect to the IMAP server. The 'true' parameter specifies to use SSL
            // which is important (for Gmail at least)
            ImapClient ic = new ImapClient("imap.gmail.com", "*****@*****.**", "20201999", AuthMethods.Login, 993, true);

            // Select a mailbox. Case-insensitive
            ic.SelectMailbox("INBOX");

            var mm = ic.SearchMessages(SearchCondition.From(from));

            MailMessage msg   = mm[Math.Abs((mm.Length - index - 1) % mm.Length)].Value;
            string      image = msg.Attachments.ElementAt(0).Body;

            // Probably wiser to use a using statement
            ic.Dispose();

            return(image);
        }
Beispiel #10
0
        private List <MailMessage> Fetch_UnseenMails(ImapClient client)
        {
            try
            {
                List <MailMessage> myMsg = new List <MailMessage>();

                client.SelectMailbox("INBOX");
                System.Lazy <MailMessage>[] myMessages = client.SearchMessages(aenetmail_csharp.Imap.SearchCondition.Unseen());
                foreach (System.Lazy <MailMessage> message in myMessages)
                {
                    myMsg.Add(message.Value);
                }
                return(myMsg);
            }
            catch
            {
                List <MailMessage> myMessages = new List <MailMessage>();
                return(myMessages);
            }
        }
Beispiel #11
0
        public IEnumerable <MailMessage> SearchEmail(string mailBox, string searchString)
        {
            ConnectToServer();
            List <MailMessage> result = new List <MailMessage>();

            try
            {
                Imap.SelectMailbox(mailBox);
            }
            catch
            {
                throw new MailException("Something went wrong while selecting the mailbox '" + mailBox + "' possibly it does not exist");
            }
            Lazy <MailMessage>[] LazyMailArray = Imap.SearchMessages(SearchCondition.Subject(searchString));
            foreach (Lazy <MailMessage> LM in LazyMailArray)
            {
                result.Add(LM.Value);
            }
            DisconnectFromServer();
            return(result);
        }
Beispiel #12
0
        private List<MailMessage> Fetch_UnseenMails(ImapClient client)
        {
            try
            {
                List<MailMessage> myMsg = new List<MailMessage>();

                client.SelectMailbox("INBOX");
                System.Lazy<MailMessage>[] myMessages = client.SearchMessages(aenetmail_csharp.Imap.SearchCondition.Unseen());
                foreach (System.Lazy<MailMessage> message in myMessages)
                    myMsg.Add(message.Value);
                return myMsg;
            }
            catch
            {
                List<MailMessage> myMessages = new List<MailMessage>();
                return myMessages;
            }
        }
        public static string Process()
        {
            MailMessage messages = new MailMessage();

            string[] body;

            using (var cliente = new ImapClient("imap.gmail.com", "*****@*****.**", "T0m4s1t0", AuthMethods.Login, 993, true))
            {
                var msj = cliente.SearchMessages(SearchCondition.Unseen()).Where(x => x.Value.Subject.Contains("LEADS")).Where(x => x.Value.Subject.Contains(":"));

                foreach (var id in msj.Select(x => x.Value.Uid))
                {
                    try
                    {
                        string Nombre    = string.Empty;
                        string Correo    = string.Empty;
                        string Telefono1 = string.Empty;
                        string Telefono2 = string.Empty;

                        messages = cliente.GetMessage(id, false, false);

                        string[] salto = new string[] { "\r\n" };
                        body = messages.Body.Replace("Nombre", "").Replace("Correo electrónico", "").Replace("Teléfono 1", "").Replace("Teléfono 2", "").Replace(": ", ":").Replace("________________________________", "").Split(salto, StringSplitOptions.None);

                        for (int i = 0; i < body.Length; i++)
                        {
                            if (body[i].Contains(":"))
                            {
                                if (Nombre == string.Empty)
                                {
                                    Nombre = body[i].Replace(":", "");
                                }
                                else if (Correo == string.Empty)
                                {
                                    Correo = body[i].Replace(":", "");
                                }
                                else if (Telefono1 == string.Empty)
                                {
                                    Telefono1 = body[i].Replace(":", "");
                                }
                                else if (Telefono2 == string.Empty)
                                {
                                    Telefono2 = body[i].Replace(":", "");
                                    break;
                                }
                            }
                        }

                        Dictionary <string, object> Parms = new Dictionary <string, object>();

                        Parms.Add("Nombre", Nombre);
                        Parms.Add("Correo", Correo);
                        Parms.Add("Telefono1", Telefono1);
                        Parms.Add("Telefono2", Telefono2);
                        Parms.Add("Origen", "LeadCorreo");

                        if (messages.Headers["From"].Value.Contains("automate"))
                        {
                            Parms.Add("Fuente", "facebook");
                        }
                        else if (messages.Headers["From"].Value.Contains("mccclictag"))
                        {
                            Parms.Add("Fuente", "clictag");
                        }

                        Parms.Add("Medio", "CPL");

                        char[] array   = messages.Subject.ToCharArray();
                        string camp    = "";
                        bool   campana = false;

                        for (int i = 0; i < array.Length; i++)
                        {
                            if (campana)
                            {
                                camp += array[i];
                            }

                            if (array[i] == ':' && array[i + 1] != ' ')
                            {
                                campana = true;
                                if (array[i + 1] == 'h' && array[i + 2] == '_')
                                {
                                    Parms.Add("Oferta", "Hogares");
                                }
                                else if (array[i + 1] == 'n' && array[i + 2] == '_')
                                {
                                    Parms.Add("Oferta", "Negocios");
                                }
                                else if (array[i + 1] == 'm' && array[i + 2] == '_')
                                {
                                    Parms.Add("Oferta", "Móviles");
                                }
                                else if (array[i + 1] == 'p' && array[i + 2] == '_')
                                {
                                    Parms.Add("Oferta", "Planta");
                                }
                            }
                            else if (!Parms.ContainsKey("Oferta") && array[i + 1] == ' ')
                            {
                                if (array[i + 2] == 'h' && array[i + 3] == '_')
                                {
                                    Parms.Add("Oferta", "Hogares");
                                }
                                else if (array[i + 2] == 'n' && array[i + 3] == '_')
                                {
                                    Parms.Add("Oferta", "Negocios");
                                }
                                else if (array[i + 2] == 'm' && array[i + 3] == '_')
                                {
                                    Parms.Add("Oferta", "Móviles");
                                }
                                else if (array[i + 2] == 'p' && array[i + 3] == '_')
                                {
                                    Parms.Add("Oferta", "Planta");
                                }

                                if (Parms.ContainsKey("Oferta"))
                                {
                                    campana = true;
                                }
                            }
                        }

                        if (!Parms.ContainsKey("Oferta"))
                        {
                            Parms.Add("Oferta", "Otros");
                        }

                        Parms.Add("Campaña", camp);

                        WS_Resources.Resources res = new WS_Resources.Resources();

                        string resultado = res.InsertMail_Agosto(Parms["Nombre"].ToString(), Parms["Correo"].ToString(), Parms["Telefono1"].ToString(), Parms["Telefono2"].ToString(), Parms["Origen"].ToString(), Parms["Fuente"].ToString(), Parms["Medio"].ToString(), Parms["Oferta"].ToString(), Parms["Campaña"].ToString());

                        cliente.MoveMessage(id, "ArchivadosLeader");
                    }
                    catch (Exception ex)
                    {
                        //return ex.Message;
                    }
                }

                return("TODO BIEN");
            }
        }