Skip to content

Commit

Permalink
Added a Pop3Client.Size property
Browse files Browse the repository at this point in the history
Fixes issue #1623
  • Loading branch information
jstedfast committed Aug 16, 2023
1 parent 99559e7 commit b663f5f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
27 changes: 27 additions & 0 deletions MailKit/Net/Pop3/Pop3Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,33 @@ public int LoginDelay {
get { return engine.LoginDelay; }
}

/// <summary>
/// Get the size of the POP3 mailbox, in bytes.
/// </summary>
/// <remarks>
/// <para>Gets the size of the POP3 mailbox, in bytes.</para>
/// <para>This value is updated as a side-effect of calling <see cref="GetMessageCount"/> or <see cref="GetMessageCountAsync"/>.</para>
/// </remarks>
/// <value>The size of the mailbox if available.</value>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="Pop3Client"/> has been disposed.
/// </exception>
/// <exception cref="ServiceNotConnectedException">
/// The <see cref="Pop3Client"/> is not connected.
/// </exception>
/// <exception cref="ServiceNotAuthenticatedException">
/// The <see cref="Pop3Client"/> is not authenticated.
/// </exception>
public long Size {
get {
CheckDisposed ();
CheckConnected ();
CheckAuthenticated ();

return octets;
}
}

void CheckDisposed ()
{
if (disposed)
Expand Down
40 changes: 40 additions & 0 deletions UnitTests/Net/Pop3/Pop3ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,7 @@ public void TestBasicPop3Client ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
var sizes = client.GetMessageSizes ();
Expand Down Expand Up @@ -1305,6 +1306,7 @@ public async Task TestBasicPop3ClientAsync ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
var sizes = await client.GetMessageSizesAsync ();
Expand Down Expand Up @@ -1390,6 +1392,7 @@ public void TestBasicPop3ClientUnixLineEndings ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
var sizes = client.GetMessageSizes ();
Expand Down Expand Up @@ -1475,6 +1478,7 @@ public async Task TestBasicPop3ClientUnixLineEndingsAsync ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
var sizes = await client.GetMessageSizesAsync ();
Expand Down Expand Up @@ -1555,6 +1559,7 @@ public void TestProbedUidlSupport ()
Assert.AreEqual (0, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

client.GetMessageUids ();

Expand Down Expand Up @@ -1606,6 +1611,7 @@ public async Task TestProbedUidlSupportAsync ()
Assert.AreEqual (0, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

await client.GetMessageUidsAsync ();

Expand Down Expand Up @@ -1656,6 +1662,7 @@ public void TestProbedUidlSupportError ()
Assert.AreEqual (0, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

Assert.Throws<NotSupportedException> (() => client.GetMessageUids ());

Expand Down Expand Up @@ -1706,6 +1713,7 @@ public async Task TestProbedUidlSupportErrorAsync ()
Assert.AreEqual (0, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

Assert.ThrowsAsync<NotSupportedException> (() => client.GetMessageUidsAsync ());

Expand Down Expand Up @@ -1849,6 +1857,7 @@ public void TestEnableUTF8AfterAuthenticate ()

Assert.AreEqual (LangCapa1, client.Capabilities);
Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

try {
client.EnableUTF8 ();
Expand Down Expand Up @@ -1904,6 +1913,7 @@ public async Task TestEnableUTF8AfterAuthenticateAsync ()

Assert.AreEqual (LangCapa1, client.Capabilities);
Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

try {
await client.EnableUTF8Async ();
Expand Down Expand Up @@ -2053,6 +2063,7 @@ public void TestGetMessageCountParseExceptions ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

Assert.Throws<Pop3ProtocolException> (() => client.GetMessageCount ());
Assert.IsTrue (client.IsConnected);
Expand Down Expand Up @@ -2115,6 +2126,7 @@ public async Task TestGetMessageCountParseExceptionsAsync ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

Assert.ThrowsAsync<Pop3ProtocolException> (async () => await client.GetMessageCountAsync ());
Assert.IsTrue (client.IsConnected);
Expand Down Expand Up @@ -2177,6 +2189,7 @@ public void TestGetMessageSizeParseExceptions ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

Assert.Throws<Pop3ProtocolException> (() => client.GetMessageSize (0));
Assert.IsTrue (client.IsConnected);
Expand Down Expand Up @@ -2239,6 +2252,7 @@ public async Task TestGetMessageSizeParseExceptionsAsync ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

Assert.ThrowsAsync<Pop3ProtocolException> (async () => await client.GetMessageSizeAsync (0));
Assert.IsTrue (client.IsConnected);
Expand Down Expand Up @@ -2301,6 +2315,7 @@ public void TestGetMessageSizesParseExceptions ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

Assert.Throws<Pop3ProtocolException> (() => client.GetMessageSizes ());
Assert.IsTrue (client.IsConnected);
Expand Down Expand Up @@ -2363,6 +2378,7 @@ public async Task TestGetMessageSizesParseExceptionsAsync ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

Assert.ThrowsAsync<Pop3ProtocolException> (async () => await client.GetMessageSizesAsync ());
Assert.IsTrue (client.IsConnected);
Expand Down Expand Up @@ -2421,6 +2437,7 @@ public void TestGetMessageUidParseExceptions ()
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);

Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

Assert.Throws<Pop3ProtocolException> (() => client.GetMessageUid (0));
Assert.IsTrue (client.IsConnected);
Expand Down Expand Up @@ -2476,6 +2493,7 @@ public async Task TestGetMessageUidParseExceptionsAsync ()
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);

Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

Assert.ThrowsAsync<Pop3ProtocolException> (async () => await client.GetMessageUidAsync (0));
Assert.IsTrue (client.IsConnected);
Expand Down Expand Up @@ -2531,6 +2549,7 @@ public void TestGetMessageUidsParseExceptions ()
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);

Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

Assert.Throws<Pop3ProtocolException> (() => client.GetMessageUids ());
Assert.IsTrue (client.IsConnected);
Expand Down Expand Up @@ -2586,6 +2605,7 @@ public async Task TestGetMessageUidsParseExceptionsAsync ()
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);

Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

Assert.ThrowsAsync<Pop3ProtocolException> (async () => await client.GetMessageUidsAsync ());
Assert.IsTrue (client.IsConnected);
Expand Down Expand Up @@ -2649,6 +2669,7 @@ public void TestSaslAuthentication ()
Assert.IsTrue (client.AuthenticationMechanisms.Contains ("LOGIN"), "Expected SASL LOGIN auth mechanism");

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
client.Disconnect (true);
Expand Down Expand Up @@ -2706,6 +2727,7 @@ public async Task TestSaslAuthenticationAsync ()
Assert.IsTrue (client.AuthenticationMechanisms.Contains ("LOGIN"), "Expected SASL LOGIN auth mechanism");

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
await client.DisconnectAsync (true);
Expand Down Expand Up @@ -2804,6 +2826,7 @@ public void TestRedactApop ()
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);

Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

try {
client.Disconnect (true);
Expand Down Expand Up @@ -2861,6 +2884,7 @@ public async Task TestRedactApopAsync ()
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);

Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

try {
await client.DisconnectAsync (true);
Expand Down Expand Up @@ -2923,6 +2947,7 @@ public void TestRedactAuthentication ()
Assert.IsTrue (client.AuthenticationMechanisms.Contains ("LOGIN"), "Expected SASL LOGIN auth mechanism");

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
client.Disconnect (true);
Expand Down Expand Up @@ -2985,6 +3010,7 @@ public async Task TestRedactAuthenticationAsync ()
Assert.IsTrue (client.AuthenticationMechanisms.Contains ("LOGIN"), "Expected SASL LOGIN auth mechanism");

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
await client.DisconnectAsync (true);
Expand Down Expand Up @@ -3039,6 +3065,7 @@ public void TestRedactUserPass ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
client.Disconnect (true);
Expand Down Expand Up @@ -3094,6 +3121,7 @@ public async Task TestRedactUserPassAsync ()
Assert.AreEqual (-1, client.ExpirePolicy);

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
await client.DisconnectAsync (true);
Expand Down Expand Up @@ -3156,6 +3184,7 @@ public void TestRedactSaslAuthentication ()
Assert.IsTrue (client.AuthenticationMechanisms.Contains ("LOGIN"), "Expected SASL LOGIN auth mechanism");

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
client.Disconnect (true);
Expand Down Expand Up @@ -3217,6 +3246,7 @@ public async Task TestRedactSaslAuthenticationAsync ()
Assert.IsTrue (client.AuthenticationMechanisms.Contains ("LOGIN"), "Expected SASL LOGIN auth mechanism");

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
await client.DisconnectAsync (true);
Expand Down Expand Up @@ -3281,6 +3311,7 @@ public void TestExchangePop3Client ()
Assert.IsTrue (client.AuthenticationMechanisms.Contains ("LOGIN"), "Expected SASL LOGIN auth mechanism");

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
var uids = client.GetMessageUids ();
Expand Down Expand Up @@ -3357,6 +3388,7 @@ public async Task TestExchangePop3ClientAsync ()
Assert.IsTrue (client.AuthenticationMechanisms.Contains ("LOGIN"), "Expected SASL LOGIN auth mechanism");

Assert.AreEqual (7, client.Count, "Expected 7 messages");
Assert.AreEqual (1800662, client.Size, "Expected 1800662 octets");

try {
var uids = await client.GetMessageUidsAsync ();
Expand Down Expand Up @@ -3448,6 +3480,7 @@ void TestGMailPop3Client (List<Pop3ReplayCommand> commands, bool disablePipelini
Assert.AreEqual (GMailCapa2, client.Capabilities);
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);
Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

if (disablePipelining)
client.Capabilities &= ~Pop3Capabilities.Pipelining;
Expand Down Expand Up @@ -3693,6 +3726,7 @@ async Task TestGMailPop3ClientAsync (List<Pop3ReplayCommand> commands, bool disa
Assert.AreEqual (GMailCapa2, client.Capabilities);
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);
Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

if (disablePipelining)
client.Capabilities &= ~Pop3Capabilities.Pipelining;
Expand Down Expand Up @@ -4024,6 +4058,7 @@ public void TestGetEnumerator ()
Assert.AreEqual (GMailCapa2, client.Capabilities);
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);
Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

int count = 0;
foreach (var message in client)
Expand Down Expand Up @@ -4089,6 +4124,7 @@ public void TestLangExtension ()
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);

Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

var languages = client.GetLanguages ();
Assert.AreEqual (6, languages.Count);
Expand Down Expand Up @@ -4161,6 +4197,7 @@ public async Task TestLangExtensionAsync ()
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);

Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

var languages = await client.GetLanguagesAsync ();
Assert.AreEqual (6, languages.Count);
Expand Down Expand Up @@ -4224,6 +4261,7 @@ public void TestLangNotSupported ()
Assert.AreEqual (GMailCapa2, client.Capabilities);
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);
Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

Assert.Throws<NotSupportedException> (() => client.GetLanguages ());
Assert.Throws<NotSupportedException> (() => client.SetLanguage ("en"));
Expand Down Expand Up @@ -4278,6 +4316,7 @@ public void TestParseExceptions ()
Assert.AreEqual (GMailCapa2, client.Capabilities);
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);
Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

try {
client.GetMessage (0);
Expand Down Expand Up @@ -4344,6 +4383,7 @@ public async Task TestParseExceptionsAsync ()
Assert.AreEqual (GMailCapa2, client.Capabilities);
Assert.AreEqual (0, client.AuthenticationMechanisms.Count);
Assert.AreEqual (3, client.Count, "Expected 3 messages");
Assert.AreEqual (221409, client.Size, "Expected 221409 octets");

try {
await client.GetMessageAsync (0);
Expand Down

0 comments on commit b663f5f

Please sign in to comment.