protected void DeleteContactButton_Click(object sender, EventArgs e) { if (ContactsListBox.SelectedIndex > -1) //Make sure the user has made a selection { int index = ContactsListBox.SelectedIndex; Contact con = _contacts[index]; RequestSettings settings = new RequestSettings("ProQuorum Messaging Module", (String)Session["username"], (String)Session["password"]); ContactsRequest cr = new ContactsRequest(settings); try { cr.Delete(con); _contacts.RemoveAt(index); //Also remove the contacts from the lists so the ContactsListBox updates accordingly. _contactNames.RemoveAt(index); ContactsListBox.DataSource = null; //update the listbox. ContactsListBox.DataSource = _contactNames; ContactsListBox.DataBind(); title.Text = "Contact List (" + _contacts.Count.ToString() + " entries)"; //MessageBox.Show(con.Name.FullName + " was successfully deleted from your contact list."); successOutput.Text = con.Name.FullName + " was successfully deleted from your contact list."; ClientScript.RegisterStartupScript(GetType(), "Show", "<script> $('#successModal').modal('toggle');</script>"); } catch (GDataVersionConflictException ex) { //MessageBox.Show("There was an Etag mismatch. Deletion could not be completed. Error: " + ex); errorOutput.Text = "There was an Etag mismatch. Deletion could not be completed. Error: " + ex; ClientScript.RegisterStartupScript(GetType(), "Show", "<script> $('#errorModal').modal('toggle');</script>"); } } else { errorOutput.Text = "Please select a contact from the list."; ClientScript.RegisterStartupScript(GetType(), "Show", "<script> $('#errorModal').modal('toggle');</script>"); //MessageBox.Show("Please select a contact from the list."); } }
//This event controls the submit button for both the add contact modal and the edit contact modal. protected void addSubmitButton_Click(object sender, EventArgs e) { RequestSettings settings = new RequestSettings("ProQuorum Messaging Module", (String)Session["username"], (String)Session["password"]); ContactsRequest cr = new ContactsRequest(settings); if (!editContactFlag) //If we are adding a contact and not editing one. { Contact newEntry = new Contact(); // Set the contact's name. if (fullName.Text != "") //make sure the user has put in a full name. { newEntry.Name = new Name() { FullName = fullName.Text //GivenName = "Elizabeth", //FamilyName = "Bennet", }; } if (notes.Text != "") { newEntry.Content = notes.Text; } // Set the contact's e-mail addresses. if (prEmail.Text != "") //make sure the user has put in a primary email. { newEntry.Emails.Add(new EMail() { Primary = true, Rel = ContactsRelationships.IsHome, Address = prEmail.Text }); } // Set the contact's phone numbers. if (homePhone.Text != "") { newEntry.Phonenumbers.Add(new PhoneNumber() { Primary = true, Rel = ContactsRelationships.IsHome, Value = homePhone.Text }); } /* Set the contact's IM information. * newEntry.IMs.Add(new IMAddress() * { * Primary = true, * Rel = ContactsRelationships.IsHome, * Protocol = ContactsProtocols.IsGoogleTalk, * });*/ // Set the contact's postal address. if (address.Text != "" || city.Text != "" || state.Text != "" || zip.Text != "" || country.Text != "") { newEntry.PostalAddresses.Add(new StructuredPostalAddress() { Rel = ContactsRelationships.IsWork, Primary = true, Street = address.Text, City = city.Text, Region = state.Text, Postcode = zip.Text, Country = country.Text //FormattedAddress = "1600 Amphitheatre Pkwy Mountain View", }); } // Insert the contact. Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default")); Contact createdEntry = cr.Insert(feedUri, newEntry); //Update the contacts list box to reflect the new contact as well as the contacts data structures. _contacts.Add(newEntry); _contactNames.Add(newEntry.Name.FullName); //Sort both the lists of contacts in alphabetical order _contactNames.Sort(); _contacts = _contacts.OrderBy(o => o.Name.FullName).ToList(); ContactsListBox.DataSource = null; //update the listbox. ContactsListBox.DataSource = _contactNames; ContactsListBox.DataBind(); title.Text = "Contact List (" + _contacts.Count.ToString() + " entries)"; //MessageBox.Show("Contact was successfully added."); successOutput.Text = "Contact was successfully added."; ClientScript.RegisterStartupScript(GetType(), "Show", "<script> $('#successModal').modal('toggle');</script>"); } else //We are editing a contact. { try { //set all of the contacts fields to the new values of the text fields. if (fullName.Text.Length > 0) { contact.Name.FullName = fullName.Text; } else { contact.Name.FullName = ""; } if (notes.Text.Length > 0) { contact.Content = notes.Text; } else { contact.Content = ""; } if (prEmail.Text.Length > 0) { contact.PrimaryEmail.Address = prEmail.Text; } else { contact.PrimaryEmail.Address = ""; } if (homePhone.Text.Length > 0) { contact.PrimaryPhonenumber.Value = homePhone.Text; } else { contact.PrimaryPhonenumber.Value = ""; } if (address.Text.Length > 0) { contact.PrimaryPostalAddress.Street = address.Text; } else { contact.PrimaryPostalAddress.Street = ""; } if (city.Text.Length > 0) { contact.PrimaryPostalAddress.City = city.Text; } else { contact.PrimaryPostalAddress.City = ""; } if (state.Text.Length > 0) { contact.PrimaryPostalAddress.Region = state.Text; } else { contact.PrimaryPostalAddress.Region = ""; } if (zip.Text.Length > 0) { contact.PrimaryPostalAddress.Postcode = zip.Text; } else { contact.PrimaryPostalAddress.Postcode = ""; } if (country.Text.Length > 0) { contact.PrimaryPostalAddress.Country = country.Text; } else { contact.PrimaryPostalAddress.Country = ""; } Contact updatedContact = cr.Update(contact); //MessageBox.Show("Contact was updated successfully."); successOutput.Text = "Contact was updated successfully."; ClientScript.RegisterStartupScript(GetType(), "Show", "<script> $('#successModal').modal('toggle');</script>"); } catch (GDataVersionConflictException ex) { //MessageBox.Show("Etag mismatch. Could not update contact."); errorOutput.Text = "Etag mismatch. Could not update contact."; ClientScript.RegisterStartupScript(GetType(), "Show", "<script> $('#errorModal').modal('toggle');</script>"); } } }
List <Contact> _contacts = new List <Contact>(); //We need have a list of type Contact so we can reference it for editing and deleting. protected void Page_Load(object sender, EventArgs e) { if (Session["username"] == null || Session["password"] == null) { /*oAuth 2.0 Legacy * code = Request.QueryString["code"]; //extract oAUTH code from the URL that google has provided us * accessToken = getToken(); * Session["accessToken"] = accessToken; //Save access token for all future interactions with google for this session. * emailInfo.Text = "New Access Token: " + accessToken; */ //Redirect the user to the login page if these variables are not set Response.Redirect("http://localhost:1172/Default.aspx"); } else { /*oAuth 2.0 Legacy * accessToken = (String)Session["accessToken"]; * emailInfo.Text = "Same Session Token: " + accessToken; */ } //Extract the contacts from the address book. RequestSettings settings = new RequestSettings("ProQuorum Messaging Module", (String)Session["username"], (String)Session["password"]); //Setting autopaging to true means all of the contacts will be extracted instead of a portion. settings.AutoPaging = true; ContactsRequest cr = new ContactsRequest(settings); Feed <Contact> f = cr.GetContacts(); //Input all names into a list, which will be used as the data source for the ListBox. foreach (Contact entry in f.Entries) { _contacts.Add(entry); if (entry.Name != null) { Name name = entry.Name; if (!string.IsNullOrEmpty(name.FullName)) { _contactNames.Add(name.FullName); } else { _contactNames.Add("No full name found."); } } else { _contactNames.Add("No name found."); } /* * foreach (EMail email in entry.Emails) * { * _emails.Add(email.Address); * }*/ } //Sort both the lists of contacts in alphabetical order _contactNames.Sort(); _contacts = _contacts.OrderBy(o => o.Name.FullName).ToList(); //Set the title label on top of the address book to the number of contacts. title.Text = "Contact List (" + _contacts.Count.ToString() + " entries)"; if (!Page.IsPostBack) //this needs to be checked so that the listbox can read what index the user is selecting. { ContactsListBox.DataSource = _contactNames; ContactsListBox.DataBind(); } //Populate the inbox with the emails try { tcpc = new System.Net.Sockets.TcpClient("imap.gmail.com", 993); ssl = new System.Net.Security.SslStream(tcpc.GetStream()); ssl.AuthenticateAsClient("imap.gmail.com"); receiveResponse(""); //IMAP login command string googleResponse = receiveResponse("$ LOGIN " + (String)Session["username"] + " " + (String)Session["password"] + " \r\n"); System.Diagnostics.Debug.WriteLine(googleResponse + " |LOGIN END|"); //This command lists the folders (inbox,sentmail,users labels ) //receiveResponse("$ LIST " + "\"\"" + " \"*\"" + "\r\n"); //Select the inbox folder googleResponse = receiveResponse("$ SELECT INBOX\r\n"); System.Diagnostics.Debug.WriteLine(googleResponse + " |SELECT INBOX END|"); //Get the status of the inbox. Response includes the number of messages. googleResponse = receiveResponse("$ STATUS INBOX (MESSAGES)\r\n"); System.Diagnostics.Debug.WriteLine(googleResponse + " |STATUS INBOX END|"); //Use String.Split to extract the number of emails and parses. string[] stringSeparators = new string[] { "(MESSAGES", ")" }; string[] words = googleResponse.ToString().Split(stringSeparators, StringSplitOptions.None); try{ numEmails = int.Parse(words[1]); } catch { MessageBox.Show("Error parsing number of emails."); } //Extract all emails and organize them into the table. if (numEmails > 0) { for (int i = numEmails; i >= 1; i--) { TableRow r = new TableRow(); //Highlight when cursor is over the message. r.Attributes["onmouseover"] = "highlight(this, true);"; //Remove the highlight when the curser exits the message r.Attributes["onmouseout"] = "highlight(this, false);"; r.Attributes["style"] = "cursor:pointer;"; r.Attributes["onclick"] = "link(this, false);"; r.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF"); //The first cell in the row will be a checkbox TableCell c0 = new TableCell(); c0.HorizontalAlign = HorizontalAlign.Center; System.Web.UI.WebControls.CheckBox checkBox = new System.Web.UI.WebControls.CheckBox(); //Add the js function to the checkbox that will highlight the row when checked and unhighlight when unchecked. checkBox.Attributes.Add("onclick", "javascript:colorRow(this);"); c0.Controls.Add(checkBox); r.Cells.Add(c0); //Specifc email number to fetch. googleResponse = receiveResponse("$ FETCH " + i + " body[HEADER.FIELDS (DATE FROM SUBJECT)]\r\n"); while (!googleResponse.Contains("$ OK Success")) { googleResponse += receiveResponse(""); } System.Diagnostics.Debug.WriteLine(googleResponse + " |HEADER " + i + " END|"); string[] orderedHeaders = organizeHeaders(googleResponse); //The second cell will be who the message is from (email) TableCell c1 = new TableCell(); //c1.Controls.Add(new LiteralControl(headers[3])); c1.Controls.Add(new LiteralControl(orderedHeaders[0])); r.Cells.Add(c1); //The third cell will be the subject. TableCell c2 = new TableCell(); c2.Controls.Add(new LiteralControl(orderedHeaders[1])); r.Cells.Add(c2); //The fourth cell will be the Date. TableCell c3 = new TableCell(); //Parse the date into a more readable format string[] ss = new string[] { " " }; string[] dateSplit = orderedHeaders[2].Split(ss, StringSplitOptions.None); DateTime time = Convert.ToDateTime(dateSplit[5]); c3.Controls.Add(new LiteralControl(dateSplit[1] + " " + dateSplit[2] + " " + dateSplit[3] + " " + dateSplit[4] + " " + time.ToShortTimeString())); r.Cells.Add(c3); googleResponse = receiveResponse("$ FETCH " + i + " body[text]\r\n"); System.Threading.Thread.Sleep(1000); googleResponse += receiveResponse(""); System.Diagnostics.Debug.WriteLine(googleResponse + " |MESSAGE " + i + " END|"); //Remove the beginning metadata int beginningDataIndex = googleResponse.IndexOf("}"); if (beginningDataIndex != -1) { googleResponse = googleResponse.Remove(0, beginningDataIndex + 1); } googleResponse = googleResponse.Trim(); //Remove the ") $ OK Success" at the end of the header int index = googleResponse.LastIndexOf(")"); if (index > -1) { googleResponse = googleResponse.Remove(index); } //Add a hidden cell so the javascript can access the message and put it in a modal when clicked on. TableCell c4 = new TableCell(); //c4.Controls.Add(new LiteralControl(emailBody[1])); c4.Controls.Add(new LiteralControl(googleResponse.ToString())); r.Cells.Add(c4); c4.Attributes.Add("hidden", "true"); InboxTable.Rows.Add(r); } } else { TableRow r = new TableRow(); TableCell c1 = new TableCell(); c1.Controls.Add(new LiteralControl("No Messages")); r.Cells.Add(c1); InboxTable.Rows.Add(r); } receiveResponse("$ LOGOUT\r\n"); } catch (Exception ex) { MessageBox.Show("error fetching emails: " + ex.Message); } finally { if (ssl != null) { ssl.Close(); ssl.Dispose(); } if (tcpc != null) { tcpc.Close(); } } }