Reputation: 37
Im trying connect to a pop3 account and getting all emails with all headers included. Im using mailkit for do this task in C#. The problem is that I searching example of how I can get all header in one string but I dont found the way to do that.
using (var client = new Pop3Client())
{
try
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("pop3.host.com", 110, false);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate("[email protected]", "password");
for (int i = 0; i < client.Count; i++)
{
// below I have the problem. I dont know how request the complete headers of a message/mail
header = client.getGetMessageHeaders(i).ToArray;
Console.WriteLine("..." + header);
}
client.Disconnect(true);
}
catch (Exception ex)
{
Console.WriteLine("Checking error: \n\n" + ex.Message + "\n\n\n");
}
}
As you can see I tried get Headers in an array for after use this array for give headers to an string, But it isnt possible using this code. I'm C# newbie for that Im requesting some help here.
very thanks!
Upvotes: 1
Views: 1312
Reputation: 38528
The way to serialize all headers to a single string would be to do something like this:
var headers = client.GetMessageHeaders (i);
using (var stream = new MemoryStream ()) {
headers.WriteTo (stream);
var bytes = memory.ToArray ();
var latin1 = Encoding.GetEncoding (28591);
string header = latin1.GetString (bytes, 0, bytes.length);
}
Keep in mind, however, that each header may contain undeclared 8-bit "text" data which might not be in iso-8859-1 or even UTF-8 (it could be in any encoding). Not only that, but each header containing 8-bit "text" data might even be using a different encoding.
Trying to represent this as a string is a fool's errand.
Far better to represent it as a Stream or an array of bytes.
If all you want to do is print the headers to the console, you could do something like this instead:
using (var client = new Pop3Client())
{
try
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("pop3.host.com", 110, false);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate("[email protected]", "password");
for (int i = 0; i < client.Count; i++)
{
header = client.GetMessageHeaders(i);
header.WriteTo (Console.OpenStandardOutput ());
}
client.Disconnect(true);
}
catch (Exception ex)
{
Console.WriteLine("Checking error: \n\n" + ex.Message + "\n\n\n");
}
}
Or, if you'd rather see the decoded header values:
using (var client = new Pop3Client())
{
try
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("pop3.host.com", 110, false);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate("[email protected]", "password");
for (int i = 0; i < client.Count; i++)
{
var headers = client.GetMessageHeaders(i);
foreach (var header in headers)
Console.WriteLine ("{0}: {1}", header.Field, header.Value);
}
client.Disconnect(true);
}
catch (Exception ex)
{
Console.WriteLine("Checking error: \n\n" + ex.Message + "\n\n\n");
}
}
Upvotes: 3