Created
March 4, 2014 11:34
-
-
Save rgl/9344874 to your computer and use it in GitHub Desktop.
Purge a POP3 account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenPop.Pop3; // from http://www.nuget.org/packages/OpenPop.NET/ | |
using System; | |
namespace PurgePop3Account | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// use the pop3s scheme for SSL connection (recommended). | |
// use the pop3 scheme for non-SSL connection. | |
var accountUri = new Uri("pop3s://TODO_USERNAME:TODO_PASSWORD@TODO_HOSTNAME"); | |
while (true) | |
{ | |
using (var client = CreatePopClient(accountUri)) | |
{ | |
Console.WriteLine("Connected"); | |
var messageCount = client.GetMessageCount(); | |
Console.WriteLine("End poll. messageCount={0}", messageCount); | |
if (messageCount == 0) | |
{ | |
Console.WriteLine("DONE!"); | |
return; | |
} | |
// delete in batches. | |
messageCount = Math.Min(100, messageCount); | |
Console.WriteLine("Deleting a batch of {0} messages...", messageCount); | |
// NB message numbers start at 1. | |
for (var messageNumber = 1; messageNumber <= messageCount; ++messageNumber) | |
{ | |
// NB the message is only actually deleted after we disconnect from the | |
// server. though, its marked as deleted and we cannot access it | |
// anymore. thats why we batch the deletes. | |
client.DeleteMessage(messageNumber); | |
} | |
} | |
} | |
} | |
private static Pop3Client CreatePopClient(Uri uri) | |
{ | |
var ssl = uri.Scheme.EndsWith("s"); | |
var port = uri.Port > 0 ? uri.Port : (ssl ? 995 : 110); | |
var userInfo = uri.UserInfo.Split(new[] { ':' }, 2); | |
var username = Uri.UnescapeDataString(userInfo[0]); | |
var password = Uri.UnescapeDataString(userInfo[1]); | |
var client = new Pop3Client(); | |
Console.WriteLine("Connection to POP3 server at {0}:{1} SSL={2}", uri.Host, port, ssl); | |
client.Connect(uri.Host, port, ssl); | |
Console.WriteLine("Authenticating with user {0}", username); | |
client.Authenticate(username, password); | |
return client; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment