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; } } } } }
private static void DeleteMessage(string uid) { using (var imap = new ImapClient(Host, UserName, Password, ImapClient.AuthMethods.Login, Port, false)) { imap.DeleteMessage(uid); } }
//Metode for å hente inn mail public static void mottaMail() { ImapClient ic = null; try { //Lager nytt objekt av klassen ImapClient ic = new ImapClient(host, sendMail.email, sendMail.password, ImapClient.AuthMethods.Login, port, secure); ic.SelectMailbox("INBOX"); //Array som henter inn alle mail i innboksen MailMessage[] mail = ic.GetMessages(0, 1, false, true); //If-setning som sjekker om det er mail i innboksen if (mail.Length != 0) { //Tre variabler som henter ut informasjon fra den siste motatte mailen from = mail[mail.Length - 1].From.Address; //Løkke med if-setning som sjekker om mailen er lagt til i databasen foreach (DataRow dtRow in GUI_FORM.dtEmails.Rows) { if (from == dtRow["Adresser"].ToString()) { subject = mail[mail.Length - 1].Subject; body = mail[mail.Length - 1].Body; } } //Løkke som sletter alle mail etter den har hentet inn den siste foreach (MailMessage m in mail) ic.DeleteMessage(m); } warningSentMotta = false; } catch (Exception ex) { if (warningSentMotta == false) { warningSentMotta = true; //Unngår spam Logger.Error(ex, module); } } finally { if (ic != null) ic.Dispose(); } }