/// <summary> /// Gets a specific message from the server /// </summary> /// <param name="MessageWanted">The message that you want to pull down from the server</param> /// <returns>A new message containing the content</returns> public Message GetMessage(Message MessageWanted) { string ResponseString; Message TempMessage = new Message(); TempMessage.MessageSize = MessageWanted.MessageSize; TempMessage.MessageNumber = MessageWanted.MessageNumber; WriteMessage("RETR " + MessageWanted.MessageNumber + "\r\n"); ResponseString = GetResponse(); if (!ResponseString.StartsWith("+OK", StringComparison.InvariantCultureIgnoreCase)) { throw new Pop3Exception(ResponseString); } Regex TempRegex = new Regex(Regex.Escape("+") + "OK.*\r\n"); ResponseString = TempRegex.Replace(ResponseString, ""); TempRegex = new Regex("\r\n.\r\n$"); TempMessage.Retrieved = true; string BodyText = ""; while (true) { if (TempRegex.Match(ResponseString).Success || string.IsNullOrEmpty(ResponseString)) { BodyText += ResponseString; BodyText = TempRegex.Replace(BodyText, ""); break; } else { BodyText += ResponseString; } ResponseString = GetResponse(); } TempMessage.MessageBody = new Utilities.Web.Email.MIME.MIMEMessage(BodyText); return TempMessage; }
/// <summary> /// Gets a list of messages from the server /// </summary> /// <returns>A list of messages (only contains message number and size)</returns> public List<Message> GetMessageList() { string ResponseString; List<Message> ReturnArray = new List<Message>(); WriteMessage("LIST\r\n"); ResponseString = GetResponse(); if (!ResponseString.StartsWith("+OK", StringComparison.InvariantCultureIgnoreCase)) { throw new Pop3Exception(ResponseString); } bool Done = false; while (!Done) { Regex TempRegex = new Regex(Regex.Escape("+") + "OK.*\r\n"); if (!ResponseString.EndsWith("\r\n.\r\n")) { while (!ResponseString.EndsWith("\r\n.\r\n")) ResponseString += GetResponse(); } ResponseString = TempRegex.Replace(ResponseString, ""); string[] Seperator = { "\r\n" }; string[] Values = ResponseString.Split(Seperator, StringSplitOptions.RemoveEmptyEntries); foreach (string Value in Values) { string[] NewSeperator = { " " }; string[] Pair = Value.Split(NewSeperator, StringSplitOptions.RemoveEmptyEntries); if (Pair.Length > 1) { Message TempMessage = new Message(); TempMessage.MessageNumber = Int32.Parse(Pair[0]); TempMessage.MessageSize = Int32.Parse(Pair[1]); TempMessage.Retrieved = false; ReturnArray.Add(TempMessage); } else { Done = true; break; } } } return ReturnArray; }
/// <summary> /// Deletes a message from the server /// </summary> /// <param name="MessageToDelete">Message to delete</param> public void Delete(Message MessageToDelete) { string ResponseString; WriteMessage("DELE " + MessageToDelete.MessageNumber + "\r\n"); ResponseString = GetResponse(); if (!ResponseString.StartsWith("+OK", StringComparison.InvariantCultureIgnoreCase)) { throw new Pop3Exception(ResponseString); } }