/// <summary> /// Example showing: /// - how to fetch all messages from a POP3 server /// </summary> /// <param name="hostname">Hostname of the server. For example: pop3.live.com</param> /// <param name="port">Host port to connect to. Normally: 110 for plain POP3, 995 for SSL POP3</param> /// <param name="useSsl">Whether or not to use SSL to connect to server</param> /// <param name="username">Username of the user on the server</param> /// <param name="password">Password of the user on the server</param> /// <returns>All Messages on the POP3 server</returns> public void ProcessNewMessages(string hostname, int port, bool useSsl, string username, string password) { // The client disconnects from the server when being disposed using (Pop3Client client = new Pop3Client()) { // Connect to the server client.Connect(hostname, port, useSsl); // Authenticate ourselves towards the server client.Authenticate(username, password); // Get the number of messages in the inbox int messageCount = client.GetMessageCount(); // We don't want to download all messages // List<Message> allMessages = new List<Message>(messageCount); List<string> uids = client.GetMessageUids(); // Messages are numbered in the interval: [1, messageCount] // Ergo: message numbers are 1-based. for (int i = 1; i <= messageCount; i++) { String uid = client.GetMessageUid(i); if (seenUids.Contains(uid)) { continue; } Message m = client.GetMessage(i); if (m.MessagePart != null) { if (m.MessagePart.MessageParts != null) { for (int j = 0; j < m.MessagePart.MessageParts.Count; j++) { //if (m.MessagePart.MessageParts[j].Body != null) //{ // System.Console.Write(ASCIIEncoding.ASCII.GetString(m.MessagePart.MessageParts[j].Body)); //} if (((m.MessagePart.MessageParts[j].IsAttachment) && (m.MessagePart.MessageParts[j].FileName == "invite.ics")) || ((m.MessagePart.MessageParts[j].ContentType != null) && (m.MessagePart.MessageParts[j].ContentType.MediaType.StartsWith("text/calendar")))) { //System.Console.Write(ASCIIEncoding.ASCII.GetString(m.MessagePart.MessageParts[j].Body)); List<String> lines = ParseICalendar(m.MessagePart.MessageParts[j].Body); // Then find the relevant lines. // Make sure this is a VCALENDAR attachment. if ((lines != null) && lines[0].StartsWith("BEGIN:VCALENDAR")) { Meeting meeting = new Meeting(); ExtractAttributes(lines, meeting); String json = meeting.ToJson(); System.Console.WriteLine("Processing Message: " + uid); System.Console.WriteLine("JSON: " + json); seenUids.Add(uid); //PostJsonToWebServer(json); } } } } } //System.Console.Write(ASCIIEncoding.ASCII.GetString(m.MessagePart.MessageParts[0].Body)); //allMessages.Add(m); } } }
private void ExtractAttributes(List<String> lines, Meeting m) { for (int i = 0; i < lines.Count; i++) { if (lines[i].StartsWith("ATTENDEE;CUTYPE=")) { Person attendee = new Person(); String[] words = lines[i].Split(';'); for (int j = 0; j < words.Length; j++) { if (words[j].StartsWith("CN=")) { String[] parts = words[j].Split('='); if (parts.Length > 1) { attendee.name = parts[1]; } } else if (words[j].StartsWith("X-NUM-GUESTS")) { String[] part = words[j].Split(':'); if ((part.Length == 3) && (part[0].StartsWith("X-NUM-GUESTS")) && (part[1].StartsWith("mailto"))) { attendee.email = part[2]; } } } m.attendees.Add(attendee); } if (lines[i].StartsWith("ATTENDEE;ROLE=")) { Person attendee = new Person(); String[] words = lines[i].Split(';'); for (int j = 0; j < words.Length; j++) { if (words[j].StartsWith("CN=")) { String[] part = words[j].Split(':'); if ((part.Length == 3) && (part[0].StartsWith("CN")) && (part[1].StartsWith("mailto", StringComparison.CurrentCultureIgnoreCase))) { String[] subparts = part[0].Split('='); if (subparts.Length > 1) { attendee.name = subparts[1]; } attendee.email = part[2]; } } } m.attendees.Add(attendee); } else if (lines[i].StartsWith("ORGANIZER")) { Person organizer = new Person(); String[] words = lines[i].Split(';'); for (int j = 0; j < words.Length; j++) { if (words[j].StartsWith("CN=")) { String[] part = words[j].Split(':'); if ((part.Length == 3) && (part[0].StartsWith("CN")) && (part[1].StartsWith("mailto", StringComparison.CurrentCultureIgnoreCase))) { String[] subparts = part[0].Split('='); if (subparts.Length > 1) { organizer.name = subparts[1]; } organizer.email = part[2]; } } } m.organizer = organizer; } else if (lines[i].StartsWith("SUMMARY")) { String[] words = lines[i].Split(':'); if ((words.Length == 2) && (words[0].StartsWith("SUMMARY"))) { m.title = words[1]; } } else if (lines[i].StartsWith("LOCATION")) { String[] words = lines[i].Split(':'); if ((words.Length == 2) && (words[0].StartsWith("LOCATION"))) { m.location = words[1]; } } else if (lines[i].StartsWith("DTSTART")) { String[] words = lines[i].Split(':'); if ((words.Length == 2) && (words[0].StartsWith("DTSTART"))) { m.startDate = words[1]; } } else if (lines[i].StartsWith("DTEND")) { String[] words = lines[i].Split(':'); if ((words.Length == 2) && (words[0].StartsWith("DTEND"))) { m.endDate = words[1]; } } } }