MailClient.GetMailHeader Method


Receives a specified email headers from mail server.

[Visual Basic]
Public Function GetMailHeader( _
    info As MailInfo
) As Byte()

Public Async Function GetMailHeaderAsync( _
    info As MailInfo
) As Task (Of Byte())
[C#]
public byte[] GetMailHeader(
    MailInfo info
);

public async Task<byte[]> GetMailHeaderAsync(
    MailInfo info
);
[C++]
public: array<unsigned char^> GetMailHeader(
    MailInfo^ info
);
[JScript]
public function GetMailHeader( 
    info: MailInfo
) : Byte [];

Parameters

info
The MailInfo instance to retrieve header.

Return Value

A byte array presenting the email header.

Remarks

This method abstracts message header without downloading the whole message body. With this feature, you can build an application such as POP3 & IMAP4 Checker to preview email.

Example

[Visual Basic, C#, C++] The following example demonstrates how to preview email subject with EAGetMail POP3 & IMAP Component, but it doesn't demonstrates the events and mail parsing usage. To get the full samples of EAGetMail, please refer to Samples section.

[VB - Preview Email from POP3/IMAP4/MS Exchange Server]
Imports System
Imports System.Globalization
Imports System.IO
Imports System.Text
Imports EAGetMail

Public Class TestClass

    Public Sub PreviewMail(server As String, user As String, password As String, useSsl As Boolean)

        Try
            ' To receive email from IMAP4 server, please change
            ' ServerProtocol.Pop3 to ServerProtocol.Imap4 in MailServer constructor

            ' To receive email with Exchange Web Service, please change
            ' ServerProtocol.Pop3 to ServerProtocol.ExchangeEWS to MailServer constructor

            ' To receive email with Exchange WebDAV, please change
            ' ServerProtocol.Pop3 to ServerProtocol.ExchangeWebDAV to MailServer constructor

            ' Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007
            ' Or later version, POP3/IMAP4 service Is disabled by default. If you don't want to use POP3/IMAP4
            ' to download email from Exchange Server, you can use Exchange Web Service (Exchange 2007-2019 Or
            ' later version) Or WebDAV (Exchange 2000/2003) protocol.

            ' Most modern email server require SSL/TLS connection, 
            ' set useSsl to true Is recommended.
            Dim oServer As New MailServer(server, user, password, useSsl,
                        ServerAuthType.AuthLogin, ServerProtocol.Pop3)

            ' POP3 port Is 110, POP3 SSL port Is 995
            ' IMAP4 port Is 143,  IMAP4 SSL port Is 993.
            ' EWS/WebDAV, please ignore Port property.
            If oServer.Protocol = ServerProtocol.Pop3 Then
                oServer.Port = If(useSsl, 995, 110)
            ElseIf oServer.Protocol = ServerProtocol.Imap4 Then
                oServer.Port = If(useSsl, 993, 143)
            End If

            Console.WriteLine("Connecting server ...")
            Dim oClient As New MailClient("TryIt")
            oClient.Connect(oServer)

            Console.WriteLine("Retrieving email list ...")
            Dim infos() As MailInfo = oClient.GetMailInfos()

            Console.WriteLine("Total {0} email(s)", infos.Length)

            For i As Integer = 0 To infos.Length - 1
                Console.WriteLine("Checking {0}/{1} ...", i + 1, infos.Length)
                Dim info As MailInfo = infos(i)

                Console.WriteLine("UIDL: {0}", info.UIDL)
                Console.WriteLine("Index: {0}", info.Index)
                Console.WriteLine("Size: {0}", info.Size)
                ' IMAP4MailFlags is supported by IMAP only.
                Console.WriteLine("Flags: {0}", info.IMAP4MailFlags)
                ' POP3 protocol doesn't support read flag.
                Console.WriteLine("Read: {0}", info.Read)
                Console.WriteLine("Deleted: {0}", info.Deleted)
              
                Console.WriteLine("Downloading header {0}/{1} ...", i + 1, infos.Length)
                Dim oMail As New Mail("TryIt")
                oMail.Load(oClient.GetMailHeader(info))

                Console.WriteLine("From: {0}", oMail.From.ToString())
                Console.WriteLine("Subject: {0}", oMail.Subject)
            Next

            Console.WriteLine("Disconnecting ...")
            ' Delete method just mark the email as deleted, 
            ' Quit method expunge the emails from server permanently.
            oClient.Quit()

            Console.WriteLine("Completed!")
        Catch ep As Exception
            Console.WriteLine("Error: {0}", ep.Message)
        End Try

    End Sub
End Class


[C# - Preview Email from POP3/IMAP4/MS Exchange Server] using System; using System.IO; using System.Globalization; using System.Text; using EAGetMail; class TestClass { public void PreivewMail(string server, string user, string password, bool useSsl) { try { // To receive email from IMAP4 server, please change // ServerProtocol.Pop3 to ServerProtocol.Imap4 in MailServer constructor // To receive email with Exchange Web Service, please change // ServerProtocol.Pop3 to ServerProtocol.ExchangeEWS to MailServer constructor // To receive email with Exchange WebDAV, please change // ServerProtocol.Pop3 to ServerProtocol.ExchangeWebDAV to MailServer constructor // Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007 // or later version, POP3/IMAP4 service is disabled by default. If you don't want to use POP3/IMAP4 // to download email from Exchange Server, you can use Exchange Web Service (Exchange 2007-2019 or // later version) or WebDAV (Exchange 2000/2003) protocol. // Most modern email server require SSL/TLS connection, // set useSsl to true is recommended. MailServer oServer = new MailServer(server, user, password, useSsl, ServerAuthType.AuthLogin, ServerProtocol.Pop3); // POP3 port is 110, POP3 SSL port is 995 // IMAP4 port is 143, IMAP4 SSL port is 993. // EWS/WebDAV, please ignore Port property. if (oServer.Protocol == ServerProtocol.Pop3) { oServer.Port = (useSsl) ? 995 : 110; } else if (oServer.Protocol == ServerProtocol.Imap4) { oServer.Port = (useSsl) ? 993 : 143; } Console.WriteLine("Connecting server ..."); MailClient oClient = new MailClient("TryIt"); oClient.Connect(oServer); Console.WriteLine("Retreiving email list ..."); MailInfo[] infos = oClient.GetMailInfos(); Console.WriteLine("Total {0} email(s)", infos.Length); for (int i = 0; i < infos.Length; i++) { Console.WriteLine("Checking {0}/{1} ...", i+1, infos.Length); MailInfo info = infos[i]; Console.WriteLine("UIDL: {0}", info.UIDL); Console.WriteLine("Index: {0}", info.Index); Console.WriteLine("Size: {0}", info.Size); // IMAP4MailFlags is supported by IMAP4 protocol only. Console.WriteLine("Flags: {0}", info.IMAP4MailFlags); // POP3 protocol doesn't support read flag Console.WriteLine("Read: {0}", info.Read); Console.WriteLine("Deleted: {0}", info.Deleted); Console.WriteLine("Downloading headers {0}/{1} ...", i + 1, infos.Length); Mail oMail = new Mail("TryIt"); oMail.Load(oClient.GetMailHeader(info)); Console.WriteLine("From: {0}", oMail.From.ToString()); Console.WriteLine("Subject: {0}", oMail.Subject); } Console.WriteLine("Disconnecting ..."); // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. oClient.Quit(); Console.WriteLine("Completed!"); } catch (Exception ep) { Console.WriteLine("Error: {0}", ep.Message); } } }
[C++/CLI - Preview Email from POP3/IMAP4/MS Exchange Server] using namespace System; using namespace System::Globalization; using namespace System::IO; using namespace EAGetMail; //add EAGetMail namespace void PreviewMail(String ^server, String ^user, String ^password, bool useSsl) { try { // To receive email from IMAP4 server, please change // ServerProtocol.Pop3 to ServerProtocol.Imap4 in MailServer constructor // To receive email with Exchange Web Service, please change // ServerProtocol.Pop3 to ServerProtocol.ExchangeEWS to MailServer constructor // To receive email with Exchange WebDAV, please change // ServerProtocol.Pop3 to ServerProtocol.ExchangeWebDAV to MailServer constructor // Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007 // or later version, POP3/IMAP4 service is disabled by default. If you don't want to use POP3/IMAP4 // to download email from Exchange Server, you can use Exchange Web Service (Exchange 2007-2019 or // later version) or WebDAV (Exchange 2000/2003) protocol. // Most modern email server require SSL/TLS connection, // set useSsl to true is recommended. MailServer ^oServer = gcnew MailServer(server, user, password, useSsl, ServerAuthType::AuthLogin, ServerProtocol::Pop3); // POP3 port is 110, POP3 SSL port is 995 // IMAP4 port is 143, IMAP4 SSL port is 993. // EWS/WebDAV, please ignore Port property. if (oServer->Protocol == ServerProtocol::Pop3) { oServer->Port = (useSsl) ? 995 : 110; } else if (oServer->Protocol == ServerProtocol::Imap4) { oServer->Port = (useSsl) ? 993 : 143; } Console::WriteLine("Connecting server ..."); MailClient ^oClient = gcnew MailClient("TryIt"); oClient->Connect(oServer); Console::WriteLine("Retreiving email list ..."); array<MailInfo^>^infos = oClient->GetMailInfos(); Console::WriteLine("Total {0} email(s)", infos->Length); for (int i = 0; i < infos->Length; i++) { Console::WriteLine("Checking {0}/{1}", i + 1, infos->Length); MailInfo ^info = infos[i]; Console::WriteLine("UIDL: {0}", info->UIDL); Console::WriteLine("Index: {0}", info->Index); Console::WriteLine("Size: {0}", info->Size); // IMAP4MailFlags is supported by IMAP4 protocol only Console::WriteLine("Flags: {0}", info->IMAP4MailFlags); // POP3 doesn't support read flag Console::WriteLine("Read: {0}", info->Read); Console::WriteLine("Deleted: {0}", info->Deleted); Console::WriteLine("Downloading headers {0}/{1}", i + 1, infos->Length); Mail ^oMail = gcnew Mail("TryIt"); oMail->Load(oClient->GetMailHeader(info)); Console::WriteLine("From: {0}", oMail->From->ToString()); Console::WriteLine("Subject: {0}", oMail->Subject); } Console::WriteLine("Disconnecting ..."); // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. oClient->Quit(); Console::WriteLine("Completed!"); } catch (Exception ^ep) { Console::WriteLine("Error: {0}", ep->Message); } }

See Also

MailClient.GetMail Method
Mail Class