/// <summary>
        /// gets all unread email from Gmail and returns a List of System.Net.Email.MailMessage Objects
        /// </summary>
        /// <param name="username">Gmail Login</param>
        /// <param name="password">mail Password</param>
        public static List<MailMessage> FetchAllUnreadMessages(string username, string password)
        {
            List<MailMessage> mlist = new List<MailMessage>();
            // The client disconnects from the server when being disposed

            try
            {
                using (Pop3Client client = new Pop3Client())
                {
                    // Connect to the server
                    client.Connect("pop.gmail.com", 995, true);

                    // Authenticate ourselves towards the server
                    client.Authenticate(username, password);

                    // Get the number of messages in the inbox
                    int messageCount = client.GetMessageCount();





                    for (int x = messageCount; x > 0; x--)
                    {

                        Message m = new Message(client.GetMessage(x).RawMessage);
                        System.Net.Mail.MailMessage mm = m.ToMailMessage();

                        mlist.Add(mm);

                    }
                    return mlist;
                }
            }
            catch { return mlist; }
        }
        public void TestToMailMessageDoesNotOverrideBodyWhenTwoPlainTextVersion()
        {
            const string multiPartMessage =
                "MIME-Version: 1.0\r\n" +
                "Content-Type: multipart/mixed;\r\n" +
                "\t\t\t\t\t\t boundary=unique-boundary-1\r\n" +
                "\r\n" +
                "This is the preamble area of a multipart message.\r\n" +
                "--unique-boundary-1\r\n" +
                "\r\n" +
                " ... Some text appears here ...\r\n" +
                "--unique-boundary-1\r\n" +
                "Content-type: text/plain; charset=US-ASCII\r\n" +
                "\r\n" +
                "This could have been part of the previous part, but\r\n" +
                "illustrates explicit versus implicit typing of body\r\n" +
                "parts.\r\n" +
                "--unique-boundary-1--\r\n";

            Message message = new Message(Encoding.ASCII.GetBytes(multiPartMessage));
            MailMessage mailMessage = message.ToMailMessage();

            Assert.NotNull(mailMessage);

            // Get the first plain text message part
            MessagePart firstPlainText = message.MessagePart.MessageParts[0];
            Assert.AreEqual(firstPlainText.GetBodyAsText(), mailMessage.Body);

            // Get the scond plain text message part
            MessagePart secondPlainText = message.MessagePart.MessageParts[1];

            // The second plain text version should be in the alternative views collection
            Assert.AreEqual(1, mailMessage.AlternateViews.Count);
            Assert.AreEqual(secondPlainText.GetBodyAsText(), Encoding.ASCII.GetString(getAttachmentBytes(mailMessage.AlternateViews[0])));
        }
	    /// <summary>
	    /// Handle an SMTP transaction, i.e. all activity between initial connect and QUIT command.
	    /// </summary>
	    /// <param name="output">output stream</param>
	    /// <param name="input">input stream</param>
	    /// <param name="messageQueue">The message queue to add any messages to</param>
	    /// <returns>List of received SmtpMessages</returns>
        private void HandleSmtpTransaction(StreamWriter output, TextReader input)
		{
			// Initialize the state machine
			SmtpState smtpState = SmtpState.CONNECT;
			SmtpRequest smtpRequest = new SmtpRequest(SmtpActionType.CONNECT, String.Empty, smtpState);

			// Execute the connection request
			SmtpResponse smtpResponse = smtpRequest.Execute();

			// Send initial response
			SendResponse(output, smtpResponse);
			smtpState = smtpResponse.NextState;

			SmtpMessage msg = new SmtpMessage();
            
			while (smtpState != SmtpState.CONNECT)
			{
				string line = input.ReadLine();

				if (line == null)
				{
					break;
				}

				// Create request from client input and current state
				SmtpRequest request = SmtpRequest.CreateRequest(line, smtpState);
				// Execute request and create response object
				SmtpResponse response = request.Execute();
				// Move to next internal state
				smtpState = response.NextState;
				// Store input in message
				msg.Store(response, request.Params);

				// If message reception is complete save it
				if (smtpState == SmtpState.QUIT)
				{
                    // Remove the last carriage return and new line
                    string mimeMessage = msg.RawMessage;
                    byte[] messageBytes = Encoding.ASCII.GetBytes(mimeMessage);
                    Message message = new Message(messageBytes, true);

                    _receivedMail.Enqueue(message.ToMailMessage());

					msg = new SmtpMessage();
                }

                // Send reponse to client after we have stored the email, so when asking for the recived mail list it will be there 
                // (this was not the way it was done before)
                SendResponse(output, response);

			}
		}
        private MailMessage GetMailMessageFromMessage(Message msg)
        {
            var mailMessage = msg.ToMailMessage();
            mailMessage.Attachments.Clear();

            var attachParts = msg.FindAllAttachments();
            foreach (var part in attachParts)
            {
                var attachment = new System.Net.Mail.Attachment(new MemoryStream(part.Body), part.FileName, part.ContentType.MediaType);
                mailMessage.Attachments.Add(attachment);
            }

            return mailMessage;
        }