Esempio n. 1
0
 protected virtual void OnRecievedMessage(WatcherServiceEventArgs e)
 {
     EventHandler<WatcherServiceEventArgs> handler = RecievedMessage;
     if (handler != null)
     {
         handler(this, e);
     }
 }
        private void recievedMessage(uint MessageUID)
        {
            // Download the message (Set as seen)
            MailMessage msg = this.ImapConnection.GetMessage(MessageUID, true);

            // Side case for iPhones/"Smart Phones"
            if (msg.Attachments.Count > 0)
            {
                foreach (Attachment a in msg.Attachments)
                {
                    // Only get text files
                    if (a.ContentType.MediaType.Equals("text/plain", StringComparison.OrdinalIgnoreCase))
                    {
                        Stream dataStream = a.ContentStream;
                        byte[] dataBuffer = new byte[dataStream.Length];
                        dataStream.Position = 0;
                        dataStream.Read(dataBuffer, 0, dataBuffer.Length);
                        String textData = Encoding.ASCII.GetString(dataBuffer);
                        // Do it twice in-case theres a different order
                        textData = textData.TrimEnd('\r');
                        textData = textData.TrimEnd('\n');
                        textData = textData.TrimEnd('\r');
                        textData = textData.TrimEnd('\n');

                        msg.Body += textData;
                    }
                }
            }

            // Alert the watchers!
            WatcherServiceEventArgs args = new WatcherServiceEventArgs();
            Message msgObj = new Message();

            // TODO: Just return a System.Net.MailMessage (It has more info than we'll ever need)
            msgObj.FullMessage = msg.Body;
            msgObj.Sender = msg.From.Address;
            msgObj.Reciever = null;

            args.MessageObj = msgObj;
            args.MessageString = msg.Body;
            this.OnRecievedMessage(args);

            // Delete the message
            //this.ImapConnection.DeleteMessage(MessageUID);
        }