private void HandleMessage(Message msg) { Console.WriteLine("Received: " + msg.Text); InvokeOnMainThread (() => { string[] listOfAllAddresses = new string[msg.Others.Length + 1]; listOfAllAddresses[0] = msg.From.Address; for (int i = 0; i < msg.Others.Length; i++) { listOfAllAddresses[i + 1] = msg.Others[i].Address; } Conversation conv = ConversationDatabase.GetConversationForParticipants(listOfAllAddresses); if (conv == null) throw new InvalidOperationException("Conversation cannot be null"); ChatMessage newMsg = new ChatMessage (); newMsg.Type = ChatMessageType.Incoming; newMsg.Sender = ContactDatabase.GetContact(msg.From.Address); newMsg.Text = msg.Text; newMsg.Unread = true; conv.AddMessage(newMsg); conversationTable.ReloadData(); ChatViewController cvc = this.NavigationController.TopViewController as ChatViewController; if (cvc != null && cvc.Conversation == conv) { cvc.HandleMessage (newMsg); } }); }
private async Task ReadMessages() { try { if (client.IsConnected == false) { Console.WriteLine("Connect is required"); await client.ConnectAsync (Account.ImapAddress, Account.ImapPort, SecureSocketOptions.SslOnConnect); } if (client.IsAuthenticated == false) { Console.WriteLine("Auth is required"); client.AuthenticationMechanisms.Remove ("XOAUTH"); await client.AuthenticateAsync (Account.Address.Address, Account.Password); } // The Inbox folder is always available on all IMAP servers... var inbox = client.Inbox; FolderAccess access = await inbox.OpenAsync(FolderAccess.ReadWrite); Console.WriteLine("Got folder access: " + access); var query = SearchQuery.SubjectContains("[OpenFlow").And(SearchQuery.NotSeen); IList<UniqueId> list = (await inbox.SearchAsync(query)); Console.WriteLine(list.Count + " new message(s)"); for (int i = 0; i < list.Count; i++) { var message = await inbox.GetMessageAsync (list[i]); string msg = message.TextBody; await inbox.SetFlagsAsync(list[i], MessageFlags.Seen, true); MailboxAddress[] receipients = new MailboxAddress[message.To.Count]; for (int j = 0; j < receipients.Length; j++) receipients[j] = message.To[j] as MailboxAddress; Message newMsg = new Message(); newMsg.From = message.From[0] as MailboxAddress; newMsg.Others = receipients; newMsg.Text = msg; if (newMsg.Text.EndsWith("\r\n")) newMsg.Text = newMsg.Text.Substring(0, newMsg.Text.Length - "\r\n".Length); OnMessageReceived?.Invoke(newMsg); } } finally { } }