// Step 4: Can we use the Access Token with IMAP? // This does not work. We don't know how to use an OAuth1 access token // with the IMAP protocol. We don't know how to obtain (via Yahoo) an OAuth2 // access token for the IMAP XOAUTH2 authentication method. private void button3_Click(object sender, EventArgs e) { Chilkat.Imap imap = new Chilkat.Imap(); imap.KeepSessionLog = true; // Connect to they Yahoo! IMAP server. imap.Port = 993; imap.Ssl = true; bool success = imap.Connect("imap.mail.yahoo.com"); if (success != true) { textBox1.Text += "\r\n" + imap.LastErrorText; textBox1.Text += "\r\n" + imap.SessionLog; return; } // Send the non-standard ID command... string rawResponse; rawResponse = imap.SendRawCommand("ID (\"GUID\" \"1\")"); if (rawResponse == null) { textBox1.Text += "\r\n" + imap.LastErrorText; textBox1.Text += "\r\n" + imap.SessionLog; return; } //rawResponse = imap.SendRawCommand("AUTHENTICATE XOAUTH2 " + OAuthAccessToken); rawResponse = imap.SendRawCommand("AUTHENTICATE OAUTHBEARER " + OAuthAccessToken); if (rawResponse == null) { textBox1.Text += "\r\n" + imap.LastErrorText; textBox1.Text += "\r\n" + imap.SessionLog; return; } textBox1.Text += "\r\n" + imap.LastErrorText; textBox1.Text += "\r\n" + imap.SessionLog; // Login //success = imap.Login(YahooMailUsername, YahooMailPassword); //if (success != true) // { // textBox1.Text = imap.LastErrorText; // return; // } // Select an IMAP mailbox success = imap.SelectMailbox("Inbox"); if (success != true) { textBox1.Text += "\r\n" + imap.LastErrorText; return; } // Continue with whatever you wish to do... // (see other examples..) // Disconnect from the IMAP server. imap.Disconnect(); textBox1.Text += "\r\nLogin Success!"; }
static void Main(string[] args) { try { SqlConnection conn = new SqlConnection(Properties.Settings.Default.conn); SqlCommand sql; Chilkat.Imap imap = new Chilkat.Imap(); bool success; // Anything unlocks the component and begins a fully-functional 30-day trial. success = imap.UnlockComponent("rodi"); if (success != true) { throw new Exception(imap.LastErrorText); } // Connect to an IMAP server. success = imap.Connect(Properties.Settings.Default.mail_host); if (success != true) { throw new Exception(imap.LastErrorText); } // Login success = imap.Login(Properties.Settings.Default.mail_user, Properties.Settings.Default.mail_pass); if (success != true) { throw new Exception(imap.LastErrorText); } // Select an IMAP mailbox success = imap.SelectMailbox("Inbox"); if (success != true) { throw new Exception(imap.LastErrorText); } Chilkat.MessageSet messageSet; #region traitement des delivery failures // We can choose to fetch UIDs or sequence numbers. bool fetchUids; fetchUids = true; // Get the message IDs of all the emails in the mailbox messageSet = imap.Search("BODY \"Reason: Remote host said: 421\"", fetchUids); messageSet = imap.Search("SUBJECT \"Delivery Failure\"", fetchUids); if (messageSet == null) { throw new Exception(imap.LastErrorText); } // Fetch the emails into a bundle object: Chilkat.EmailBundle bundle; bundle = imap.FetchHeaders(messageSet); if (bundle == null) { throw new Exception(imap.LastErrorText); } //if(!imap.CopyMultiple(messageSet, "Delivery Failure")) // Console.WriteLine("Erreur copy"); //success = imap.SetFlags(messageSet, "Deleted", 1); //if (success != true) //{ // Console.WriteLine("Erreur effacement"); //} //success = imap.SelectMailbox("Delivery Failure"); //if (success != true) //{ // throw new Exception(imap.LastErrorText); //} bundle = imap.FetchBundle(messageSet); if (bundle == null) { throw new Exception(imap.LastErrorText); } #region clear delivery failure // Loop over the bundle and display the FROM and SUBJECT of each. int i; for (i = 0; i <= bundle.MessageCount - 1; i++) { Chilkat.Email email; email = bundle.GetEmail(i); try { if (conn.State != System.Data.ConnectionState.Open) { conn.Open(); } // Console.WriteLine(email.From + " : " + email.Subject + " : "+email.ReplyTo); string body = email.GetPlainTextBody(); string ch = "Failed Recipient:"; string recipient = ""; int index = body.IndexOf(ch); int index1 = 0; if (index > 0) { index1 = body.IndexOf("\r", index); index += ch.Length; recipient = body.Substring(index, index1 - index).Trim(); } ch = "Reason:"; string reason = ""; index = body.IndexOf(ch); if (index > 0) { index1 = body.IndexOf("\r", index); index += ch.Length; reason = body.Substring(index, index1 - index).Trim(); } ch = "X-AIS-Ref:"; string reference = ""; index = body.IndexOf(ch); if (index > 0) { index1 = body.IndexOf("\r", index); index += ch.Length; reference = body.Substring(index, index1 - index).Trim(); } Console.WriteLine(recipient + " : " + reason); sql = new SqlCommand("insert into ais_newsletters_error (dt,reference,recipient,reason) values (@dt,@reference,@recipient,@reason)", conn); sql.Parameters.AddWithValue("dt", email.EmailDate); sql.Parameters.AddWithValue("reference", reference); sql.Parameters.AddWithValue("recipient", recipient); sql.Parameters.AddWithValue("reason", reason); if (sql.ExecuteNonQuery() == 0) { throw new Exception("Erreur insertion"); } success = imap.SetMailFlag(email, "Deleted", 1); if (success != true) { throw new Exception(imap.LastErrorText); } } catch (Exception ee) { Console.WriteLine(ee.Message); } //textBox1.Text += email.From + "\r\n"; //textBox1.Refresh(); //textBox1.Text += email.Subject + "\r\n"; //textBox1.Refresh(); //textBox1.Text += "--" + "\r\n"; //textBox1.Refresh(); } success = imap.ExpungeAndClose(); if (success != true) { throw new Exception("Erreur ExpungeAndClose"); } #endregion #region clear of correctly delivered and been read messageSet = null; // Select an IMAP mailbox success = imap.SelectMailbox("Inbox"); if (success != true) { throw new Exception(imap.LastErrorText); } fetchUids = true; messageSet = imap.Search("ALL", fetchUids); if (messageSet == null) { throw new Exception(imap.LastErrorText); } // Fetch the emails into a bundle object: bundle = imap.FetchHeaders(messageSet); if (bundle == null) { throw new Exception(imap.LastErrorText); } bundle = imap.FetchBundle(messageSet); if (bundle == null) { throw new Exception(imap.LastErrorText); } String[] prefixes = new String[] { "Lu: ", "Lu : ", "Lu :", "Read: ", "Read: ", "Confirmation de lecture : ", "lu :", "Lu :***SPAM*** ", "lu :*** SPAM *** ", "Lu :***SPAM*** ", "Confirmation de lecture : ", "Read-Receipt: ", "Accusé de réception (lu): ", "Confirmation de lecture : " }; String[] prefixes_nonlus = new String[] { "Not read: " }; for (i = 0; i <= bundle.MessageCount - 1; i++) { Chilkat.Email email; email = bundle.GetEmail(i); bool lu = false; string subject = email.Subject; string dest = email.FromAddress; Console.WriteLine(subject + "\t" + dest); foreach (string prefixe in prefixes_nonlus) { if (subject.StartsWith(prefixe)) { subject = subject.Substring(prefixe.Length); break; } } foreach (string prefixe in prefixes) { if (subject.StartsWith(prefixe)) { subject = subject.Substring(prefixe.Length); lu = true; break; } } try { if (conn.State != System.Data.ConnectionState.Open) { conn.Open(); } sql = new SqlCommand("select id from ais_newsletters where title=@title", conn); sql.Parameters.AddWithValue("title", subject); string res = "" + sql.ExecuteScalar(); if (res != "") { if (lu) { sql = new SqlCommand("Update ais_newsletters_out set [read]='Y' where status='T' and email=@email and newsletter_id=@id", conn); sql.Parameters.AddWithValue("id", res); sql.Parameters.AddWithValue("email", dest); int nb = sql.ExecuteNonQuery(); } imap.SetMailFlag(email, "Deleted", 1); } } catch { } } success = imap.ExpungeAndClose(); #endregion #endregion imap.Disconnect(); if (conn.State != ConnectionState.Open) { conn.Open(); } sql = new SqlCommand("SELECT * FROM ais_newsletters_error ", conn); SqlDataAdapter da = new SqlDataAdapter(sql); DataSet ds = new DataSet(); da.Fill(ds); if (ds.Tables.Count > 0) { foreach (DataRow row in ds.Tables[0].Rows) { string reference = ("" + row["reference"]).Trim(); if (!reference.Equals("")) { if (("" + row["reason"]).StartsWith("Remote host said: 421")) { sql = new SqlCommand("UPDATE ais_newsletters_out SET status='A' WHERE email =@email AND newsletter_id =@reference", conn); sql.Parameters.AddWithValue("email", row["recipient"]); sql.Parameters.AddWithValue("reference", new Guid(reference)); if (sql.ExecuteNonQuery() > 0) { sql = new SqlCommand("DELETE ais_newsletters_error WHERE id =@id", conn); sql.Parameters.AddWithValue("id", row["id"]); sql.ExecuteNonQuery(); sql = new SqlCommand("UPDATE ais_newsletters SET status='E' WHERE id =@reference", conn); sql.Parameters.AddWithValue("reference", new Guid("" + row["reference"])); sql.ExecuteNonQuery(); } } else { sql = new SqlCommand("UPDATE ais_newsletters_out SET status='E',error=@error WHERE email =@email AND newsletter_id =@reference", conn); sql.Parameters.AddWithValue("error", row["reason"]); sql.Parameters.AddWithValue("email", row["recipient"]); sql.Parameters.AddWithValue("reference", new Guid(reference)); if (sql.ExecuteNonQuery() > 0) { sql = new SqlCommand("DELETE ais_newsletters_error WHERE id =@id", conn); sql.Parameters.AddWithValue("id", row["id"]); sql.ExecuteNonQuery(); } } } } } } catch (Exception ee) { Console.WriteLine(ee.Message); Console.WriteLine("--- Press Me ---"); Console.ReadLine(); } }