static void Main(string[] args) { string imapAddr = "imap.gmail.com"; int portNo = 993; bool computerList = false; MailMessage computerMessage; bool secureConn = true; string myEmailID = "*****@*****.**"; string myPasswd = "password123"; //Connect to gmail's Imap server using its address, my email, password and port number using (ImapClient ic = new ImapClient(imapAddr, myEmailID, myPasswd, AuthMethods.Login, portNo, secureConn)) { //Enter the drafts inbox ic.SelectMailbox("[Gmail]/Drafts"); bool headersOnly = false; while (true) { //Currently Checking every 3 seconds for testing purposes System.Threading.Thread.Sleep(3000); //Find all the messages that are not answered ( All of them because they are drafts ) Lazy<MailMessage>[] messages = ic.SearchMessages(SearchCondition.Unanswered(), headersOnly, false); //List the amount of messages found Console.WriteLine(messages.Length); foreach (Lazy<MailMessage> message in messages) { MailMessage m = message.Value; string subjectLineCheck = m.Subject; //Compare the subject line to computer list if (subjectLineCheck == "Computer List") { computerList = true; computerMessage = m; } } if (computerList = true) { //CURRENTLY WORKING HERE } // Run through each message: foreach (Lazy<MailMessage> message in messages) { MailMessage m = message.Value; string sender = m.From.Address; //Get the subject line string subjectLine = m.Subject; Console.WriteLine(subjectLine); //Check for the Command if (subjectLine == "[COMMAND]") { Console.WriteLine("Command found Activating Shutdown."); ic.DeleteMessage(m.Uid); //These lines are commented out so i don't have to restart my computer every time I want to test my code //These will be enabled in the final product because they launch the shutdown sequence. /* var psi = new System.Diagnostics.ProcessStartInfo("shutdown", "/s /t 0"); psi.CreateNoWindow = true; psi.UseShellExecute = false; System.Diagnostics.Process.Start(psi); */ return; } } } } }
public void Run() { //Console.WriteLine("running"); //Console.WriteLine("GOT HERE"); using (var ic = new AE.Net.Mail.ImapClient("IMAP.gmail.com", "*****@*****.**", "TUBA2112", ImapClient.AuthMethods.Login, 993, true)){ ic.SelectMailbox("INBOX"); bool headersOnly = false; //initial population of new/unseen messages //Console.WriteLine("connected?"); while (true) { try { Monitor.Enter(this); messages = ic.SearchMessages(SearchCondition.Unseen(), headersOnly); } finally { Monitor.Exit(this); } //Console.WriteLine("spawning child thread"); //need to spawn a thread with the mesages array to check for more conditions Thread messageSearcher = new Thread(new ThreadStart(refine)); messageSearcher.Start(); // Console.WriteLine("Child Spawned"); messageSearcher.Join(); } } }
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 AeEventBasedEmailClient(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")); client.NewMessage += (sender, args) => { if (args.MessageCount > 0) { unread.AddRange(client.SearchMessages(SearchCondition.New()) .Select(message => new MailMessage { Date = message.Value.Date, Sender = message.Value.Sender, Receivers = message.Value.To, Subject = message.Value.Subject, Body = message.Value.Body })); } }; }
public IEnumerable<IMessage> GetMessagesAfter(DateTime minDateTime) { var MessageList = new List<IMessage>(); using (ImapClient Imap = new ImapClient(_Host, _Username, _Password, ImapClient.AuthMethods.Login, _Port, _IsSSL)) { var Messages = Imap.SearchMessages(SearchCondition.SentSince(minDateTime.AddDays(-1))).ToArray (); foreach (var ImapMessage in Messages) { DateTime date = ImapMessage.Value.Date; if (date < minDateTime) continue; string from = ImapMessage.Value.From == null ? "" : ImapMessage.Value.From.Address; string sender = ImapMessage.Value.Sender== null? "":ImapMessage.Value.Sender.Address; string subject = ImapMessage.Value.Subject; string body; var htmlAlternateView = ImapMessage.Value.AlternateViews.FirstOrDefault(a => a.ContentType == "text/html"); if (htmlAlternateView !=null) { body = htmlAlternateView.Body; } else { body = ImapMessage.Value.Body ; } IMessage Message = new Message(ImapMessage.Value.From == null ? "" : from, subject, body, date, sender, htmlAlternateView!=null); MessageList.Add(Message); } } return MessageList.ToArray(); }
public void Mails_in_Inbox_auflisten() { var mailAccess = MailAccessRepository.LoadFrom("mailfollowup.spikes.mail.access.txt", Assembly.GetExecutingAssembly()); using (var imap = new ImapClient(mailAccess.Server, mailAccess.Username, mailAccess.Password)) { var mailbox= imap.SelectMailbox("Archiv"); Console.WriteLine("No. of messages in Archiv: {0}", mailbox.NumMsg); const string domain = "@cc.lieser-online.de"; var msgs = imap.SearchMessages(SearchCondition.Header("Envelope-To", domain)); foreach (var msg in msgs) { Console.WriteLine(); Console.WriteLine("From {0}: {1}", msg.Value.From.Address, msg.Value.Subject); Console.WriteLine("To {0}", string.Join(",", msg.Value.To)); Console.WriteLine("Cc {0}", string.Join(",", msg.Value.Cc)); } } }
// 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(); } }
/// <summary> /// Проверка почты /// </summary> private void Check_mail() { //Проверяем доступность почты как таковой try { //Используем методы из библиотеки AE.Net.Mail //Создаем коннект на почту var imap = new ImapClient("imap.gmail.com", Properties.Settings.Default.Login, Properties.Settings.Default.Password, ImapClient.AuthMethods.Login, 993, true); //Идем во входящие imap.SelectMailbox("INBOX"); //Ищем во входящих все сообщения, которые еще не прочитаны var unseenList = imap.SearchMessages(SearchCondition.Unseen()); //Если такие есть if (unseenList.Count() != 0) { //Выводим всплывающее окошко в трее о их наличии notifyIcon1.ShowBalloonTip(1000, "Gmail", "Непрочтитанных сообщений - " + unseenList.Count().ToString(), ToolTipIcon.Info); } } //ловим исключение catch (Exception) { //останавливаем таймер timer.Stop(); //Сообщение о недоступности почты MessageBox.Show("Не могу зайти на почту, проверьте настройки."); //Запускаем метод создания формы настроек Settings_form(); } }