Skip to content

Commit

Permalink
Removed obsolete methods from Message class
Browse files Browse the repository at this point in the history
Using consistent save methods in both Message and MessagePart classes.
Adding more examples
Fixed some names in test cases
Removed UnfixedIssuesTests as it has not been used for a very long time

git-svn-id: https://hpop.svn.sourceforge.net/svnroot/hpop/trunk@367 0ca4f280-b5d2-4e9a-a707-da1077cca493
  • Loading branch information
foens committed Oct 29, 2011
1 parent 59e8b2d commit 37a0ec2
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 92 deletions.
28 changes: 0 additions & 28 deletions OpenPop/Mime/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,6 @@ public List<MessagePart> FindAllMessagePartsWithMediaType(string mediaType)

#region Message Persistence

/// <summary>
/// Save this <see cref="Message"/> to a file.<br/>
/// <br/>
/// Can be loaded at a later time using the <see cref="LoadFromFile"/> method.
/// </summary>
/// <param name="file">The File location to save the <see cref="Message"/> to. Existent files will be overwritten.</param>
/// <exception cref="ArgumentNullException">If <paramref name="file"/> is <see langword="null"/></exception>
/// <exception>Other exceptions relevant to file saving might be thrown as well</exception>
[Obsolete("Use one of the Save method overrides instead of SaveToFile")]
public void SaveToFile(FileInfo file)
{
Save(file);
}

/// <summary>
/// Save this <see cref="Message"/> to a file.<br/>
/// <br/>
Expand Down Expand Up @@ -349,20 +335,6 @@ public void Save(Stream messageStream)
messageStream.Write(RawMessage, 0, RawMessage.Length);
}

/// <summary>
/// Loads a <see cref="Message"/> from a file with an email in it.
/// </summary>
/// <param name="file">The File location to load the <see cref="Message"/> from. File must be existent.</param>
/// <exception cref="ArgumentNullException">If <paramref name="file"/> is <see langword="null"/></exception>
/// <exception cref="FileNotFoundException">If <paramref name="file"/> does not exist</exception>
/// <exception>Other exceptions relevant to file loading might be thrown as well</exception>
/// <returns>A <see cref="Message"/> with the content loaded from the <paramref name="file"/></returns>
[Obsolete("Use one of the Load method overrides instead of LoadFromFile")]
public static Message LoadFromFile(FileInfo file)
{
return Load(file);
}

/// <summary>
/// Loads a <see cref="Message"/> from a file containing a raw email.
/// </summary>
Expand Down
25 changes: 21 additions & 4 deletions OpenPop/Mime/MessagePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,18 +479,35 @@ public string GetBodyAsText()
}

/// <summary>
/// Save this <see cref="MessagePart"/> to a file.<br/>
/// Save this <see cref="MessagePart"/>'s contents to a file.<br/>
/// There are no methods to reload the file.
/// </summary>
/// <param name="file">The File location to save the <see cref="MessagePart"/> to. Existent files will be overwritten.</param>
/// <exception cref="ArgumentNullException">If <paramref name="file"/> is <see langword="null"/></exception>
/// <exception>Other exceptions relevant to file saving might be thrown as well</exception>
public void SaveToFile(FileInfo file)
/// <exception>Other exceptions relevant to using a <see cref="FileStream"/> might be thrown as well</exception>
public void Save(FileInfo file)
{
if (file == null)
throw new ArgumentNullException("file");

File.WriteAllBytes(file.FullName, Body);
using (FileStream stream = new FileStream(file.FullName, FileMode.OpenOrCreate))
{
Save(stream);
}
}

/// <summary>
/// Save this <see cref="MessagePart"/>'s contents to a stream.<br/>
/// </summary>
/// <param name="messageStream">The stream to write to</param>
/// <exception cref="ArgumentNullException">If <paramref name="messageStream"/> is <see langword="null"/></exception>
/// <exception>Other exceptions relevant to <see cref="Stream.Write"/> might be thrown as well</exception>
public void Save(Stream messageStream)
{
if (messageStream == null)
throw new ArgumentNullException("messageStream");

messageStream.Write(Body, 0, Body.Length);
}
#endregion
}
Expand Down
153 changes: 112 additions & 41 deletions OpenPopExamples/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
using System.IO;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using OpenPop.Common.Logging;
using OpenPop.Mime;
using OpenPop.Mime.Decode;
using OpenPop.Mime.Header;
using OpenPop.Pop3;

Expand All @@ -14,7 +17,7 @@ namespace OpenPopExamples
/// <see cref="OpenPop"/>.NET POP3 library
/// </summary>
public class Examples
{
{
/// <summary>
/// Example showing:
/// - how to fetch all messages from a POP3 server
Expand All @@ -26,7 +29,7 @@ public class Examples
/// <param name="password">Password of the user on the server</param>
/// <returns>All Messages on the POP3 server</returns>
public static List<Message> FetchAllMessages(string hostname, int port, bool useSsl, string username, string password)
{
{
// The client disconnects from the server when being disposed
using(Pop3Client client = new Pop3Client())
{
Expand All @@ -52,7 +55,7 @@ public static List<Message> FetchAllMessages(string hostname, int port, bool use
// Now return the fetched messages
return allMessages;
}
}
}

/// <summary>
/// Example showing:
Expand Down Expand Up @@ -95,7 +98,7 @@ public static void FindPlainTextInMessage(Message message)
if(plainText != null)
{
// Save the plain text to a file, database or anything you like
plainText.SaveToFile(new FileInfo("plainText.txt"));
plainText.Save(new FileInfo("plainText.txt"));
}
}

Expand All @@ -111,7 +114,7 @@ public static void FindHtmlInMessage(Message message)
if (html != null)
{
// Save the plain text to a file, database or anything you like
html.SaveToFile(new FileInfo("html.txt"));
html.Save(new FileInfo("html.txt"));
}
}

Expand Down Expand Up @@ -232,25 +235,25 @@ public static void DeleteMessageOnServer(string hostname, int port, bool useSsl,
}
}

/// <summary>
/// Example showing:
/// - how to use UID's (unique ID's) of messages from the POP3 server
/// - how to download messages not seen before
/// (notice that the POP3 protocol cannot see if a message has been read on the server
/// before. Therefore the client need to maintain this state for itself)
/// </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>
/// <param name="seenUids">
/// List of UID's of all messages seen before.
/// New message UID's will be added to the list.
/// Consider using a HashSet if you are using >= 3.5 .NET
/// </param>
/// <returns>A List of new Messages on the server</returns>
public static List<Message> FetchUnseenMessages(string hostname, int port, bool useSsl, string username, string password, List<string> seenUids)
/// <summary>
/// Example showing:
/// - how to use UID's (unique ID's) of messages from the POP3 server
/// - how to download messages not seen before
/// (notice that the POP3 protocol cannot see if a message has been read on the server
/// before. Therefore the client need to maintain this state for itself)
/// </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>
/// <param name="seenUids">
/// List of UID's of all messages seen before.
/// New message UID's will be added to the list.
/// Consider using a HashSet if you are using >= 3.5 .NET
/// </param>
/// <returns>A List of new Messages on the server</returns>
public static List<Message> FetchUnseenMessages(string hostname, int port, bool useSsl, string username, string password, List<string> seenUids)
{
// The client disconnects from the server when being disposed
using(Pop3Client client = new Pop3Client())
Expand Down Expand Up @@ -295,15 +298,15 @@ public static List<Message> FetchUnseenMessages(string hostname, int port, bool
}
}

/// <summary>
/// Example showing:
/// - how to set timeouts
/// - how to override the SSL certificate checks with your own implementation
/// </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="timeouts">Read and write timeouts used by the Pop3Client</param>
public static void BypassSslCertificateCheck(string hostname, int port, int timeouts)
/// <summary>
/// Example showing:
/// - how to set timeouts
/// - how to override the SSL certificate checks with your own implementation
/// </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="timeouts">Read and write timeouts used by the Pop3Client</param>
public static void BypassSslCertificateCheck(string hostname, int port, int timeouts)
{
// The client disconnects from the server when being disposed
using (Pop3Client client = new Pop3Client())
Expand All @@ -318,12 +321,12 @@ public static void BypassSslCertificateCheck(string hostname, int port, int time
}
}

private static bool certificateValidator(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslpolicyerrors)
{
private static bool certificateValidator(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslpolicyerrors)
{
// We should check if there are some SSLPolicyErrors, but here we simply say that
// the certificate is okay - we trust it.
return true;
}
return true;
}

/// <summary>
/// Example showing:
Expand All @@ -346,9 +349,77 @@ public static Message SaveAndLoadFullMessage(Message message)
// use the message again
return loadedMessage;
}
}

// Other examples to show, that is in the library
// Show how to build a TreeNode representation of the Message hierarchy using the
// TreeNodeBuilder class in OpenPopTest
/// <summary>
/// Example showing:
/// - How to change logging
/// - How to implement your own logger
/// </summary>
public static void ChangeLogging()
{
// All logging is sent trough logger defined at DefaultLogger.Log
// The logger can be changed by calling DefaultLogger.SetLog(someLogger)

// By default all logging is sent to the System.Diagnostics.Trace facilities.
// These are not very useful if you are not debugging
// Instead, lets send logging to a file:
DefaultLogger.SetLog(new FileLogger());
FileLogger.LogFile = new FileInfo("MyLoggingFile.log");

// It is also possible to implement your own logging:
DefaultLogger.SetLog(new MyOwnLogger());
}

class MyOwnLogger : ILog
{
public void LogError(string message)
{
Console.WriteLine("ERROR!!!: " + message);
}

public void LogDebug(string message)
{
// Dont want to log debug messages
}
}

/// <summary>
/// Example showing:
/// - How to provide custom Encoding class
/// - How to use UTF8 as default Encoding
/// </summary>
/// <param name="customEncoding">Own Encoding implementation</param>
public void InsertCustomEncodings(Encoding customEncoding)
{
// Lets say some email contains a characterSet of "iso-9999-9" which
// is fictional, but is really just UTF-8.
// Lets add that mapping to the class responsible for finding
// the Encoding from the name of it
EncodingFinder.AddMapping("iso-9999-9", Encoding.UTF8);

// It is also possible to implement your own Encoding if
// the framework does not provide what you need
EncodingFinder.AddMapping("specialEncoding", customEncoding);

// Now, if the EncodingFinder is not able to find an encoding, lets
// see if we can find one ourselves
EncodingFinder.FallbackDecoder = CustomFallbackDecoder;
}

Encoding CustomFallbackDecoder(string characterSet)
{
// Is it a "foo" encoding?
if (characterSet.StartsWith("foo"))
return Encoding.ASCII; // then use ASCII

// If no special encoding could be found, provide UTF8 as default.
// You can also return null here, which would tell OpenPop that
// no encoding could be found. This will then throw an exception.
return Encoding.UTF8;
}

// Other examples to show, that is in the library
// Show how to build a TreeNode representation of the Message hierarchy using the
// TreeNodeBuilder class in OpenPopTest
}
}
2 changes: 1 addition & 1 deletion OpenPopTest/TestForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ private void ListAttachmentsAttachmentSelected(object sender, TreeViewEventArgs
// Lets try to save to the file
try
{
attachment.SaveToFile(file);
attachment.Save(file);

MessageBox.Show(this, "Attachment saved successfully");
} catch (Exception e)
Expand Down
8 changes: 4 additions & 4 deletions OpenPopUnitTests/Mime/MessagePartTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void TestIsTextTextHtml()
}

[Test]
public void TestIsTextMessageRFC822()
public void TestIsTextMessageRfc822()
{
const string messagePartContent =
"Content-Type: message/rfc822\r\n" +
Expand Down Expand Up @@ -119,15 +119,15 @@ public void TestSaveToFile()
"Ljk2IDg0Mi4wNF0gL0NvbnRlbnRzIDQgMCBSL0dyb3VwPDwvVHlwZS9Hcm91cC9TL1RyYW5z\r\n" +
"cGFyZW5jeS9DUy9EZXZpY2VSR0I+Pi9UYWJzL1MvU3RydWN0UGFyZW50cyAwPj4NCmVuZG9i";

const string partPDF =
const string partPdf =
"Content-Type: application/pdf;\r\n" +
" name=\"=?ISO-8859-1?Q?=D8nskeliste=2Epdf?=\"\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n" +
base64;

// Base 64 is only in ASCII
Message message = new Message(Encoding.ASCII.GetBytes(partPDF));
Message message = new Message(Encoding.ASCII.GetBytes(partPdf));

MessagePart messagePart = message.MessagePart;

Expand All @@ -141,7 +141,7 @@ public void TestSaveToFile()
Assert.AreEqual(correctBytes, messagePart.Body);

FileInfo testFile = new FileInfo("test_message_save_.testFile");
messagePart.SaveToFile(testFile);
messagePart.Save(testFile);

byte[] fileBytes = File.ReadAllBytes(testFile.ToString());
testFile.Delete();
Expand Down
1 change: 0 additions & 1 deletion OpenPopUnitTests/OpenPopUnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
<Compile Include="Pop3\POPClientPositiveTests.cs" />
<Compile Include="Pop3\POPClientUnconnectedTests.cs" />
<Compile Include="Shared\StreamUtilityTests.cs" />
<Compile Include="UnfixedIssuesTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenPOP\OpenPop.csproj">
Expand Down
13 changes: 0 additions & 13 deletions OpenPopUnitTests/UnfixedIssuesTests.cs

This file was deleted.

0 comments on commit 37a0ec2

Please sign in to comment.