Skip to content

Instantly share code, notes, and snippets.

@jstedfast
Created June 19, 2014 14:17
Show Gist options
  • Save jstedfast/93a1a842c5ec7c99177d to your computer and use it in GitHub Desktop.
Save jstedfast/93a1a842c5ec7c99177d to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Text;
using System.Threading;
using System.Security.Cryptography;
using MailKit.Net.Imap;
using MailKit.Net.Pop3;
using MailKit;
using MimeKit;
namespace TestClient {
class Program
{
public static void Main (string[] args)
{
string userName = "[email protected]";
string password = "password";
UploadMessages (userName, password, "issue58.msg");
DownloadMessages (userName, password);
}
static void UploadMessages (string userName, string password, string fileName)
{
using (var client = new ImapClient ()) {
var credentials = new NetworkCredential (userName, password);
var uri = new Uri ("imaps://imap.gmail.com");
client.Connect (uri);
client.AuthenticationMechanisms.Remove ("XOAUTH2");
client.Authenticate (credentials);
client.Inbox.Open (FolderAccess.ReadWrite);
var message = MimeMessage.Load (fileName);
for (int i = 0; i < 10; i++) {
message.MessageId = MimeKit.Utils.MimeUtils.GenerateMessageId ();
client.Inbox.Append (message, MessageFlags.Seen, message.Date);
}
client.Disconnect (true);
}
}
static void DownloadMessages (string userName, string password)
{
using (var client = new Pop3Client (new ProtocolLogger ("gmail-pop3.log"))) {
var credentials = new NetworkCredential (userName, password);
var uri = new Uri ("pops://pop.gmail.com:995");
client.Connect (uri);
client.AuthenticationMechanisms.Remove ("XOAUTH2");
client.Authenticate (credentials);
int count = client.GetMessageCount ();
for (int i = 0; i < count; i++) {
var message = client.GetMessage (i);
using (var jpeg = new MemoryStream ()) {
var attachment = message.Attachments.FirstOrDefault ();
attachment.ContentObject.DecodeTo (jpeg);
jpeg.Position = 0;
using (var md5 = new MD5CryptoServiceProvider ()) {
var md5sum = HexEncode (md5.ComputeHash (jpeg));
if (md5sum == "5b1b8b2c9300c9cd01099f44e1155e2b")
Console.WriteLine ("MD5 checksums match!");
else
Console.WriteLine ("MD5 checksums do not match.");
}
}
client.DeleteMessage (i);
}
client.Disconnect (true);
}
}
static string HexEncode (byte[] digest)
{
var hex = new StringBuilder ();
for (int i = 0; i < digest.Length; i++)
hex.Append (digest[i].ToString ("x2"));
return hex.ToString ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment