The following example demonstrates referencing various headers of an email message. One important item to note, is that the headers must be checked if they are null, before referencing any of their properties.
For a continuation of this example, see Referencing Headers Part II.
[C#]
using System; using aspNetPOP3; using aspNetMime; namespace cstest { class Class1 { [STAThread] static void Main(string[] args) { //create a new pop3 object POP3 pop = new POP3("127.0.0.1","dave@blah.com", "mypassword" ); //connect to the POP3 server pop.Connect(); //retrieve the first message MimeMessage msg = pop.GetMessage(1); //Close the POP3 Connection pop.Disconnect(); //write out some of the headers if( msg.Subject != null ) Console.WriteLine( msg.Subject.Value ); if( msg.To != null ) msg.To.ToString(); if( msg.From != null ) Console.WriteLine( msg.From.EmailAddress ); if( msg.ContentType != null ) Console.WriteLine( msg.ContentType.Value ); //reference an X-Header if( msg.Headers[ "X-Mailer" ] != null ) Console.WriteLine( msg.Headers[ "X-Mailer" ].Value ); //done Console.WriteLine( "done."); Console.ReadLine( ); } } }
[VB.NET]
Imports aspNetPOP3 Imports aspNetMime Module Module1 Sub Main() 'create a new pop3 object Dim pop As New POP3("127.0.0.1", "dave@blah.com", "mypassword") 'connect to the POP3 server pop.Connect() 'retrieve the first message Dim msg As MimeMessage = pop.GetMessage(1) 'Close the POP3 Connection pop.Disconnect() 'write out some of the headers If Not (msg.Subject Is Nothing) Then Console.WriteLine(msg.Subject.Value) End If If Not (msg.To Is Nothing) Then msg.To.ToString() End If If Not (msg.From Is Nothing) Then Console.WriteLine(msg.From.EmailAddress) End If If Not (msg.ContentType Is Nothing) Then Console.WriteLine(msg.ContentType.Value) End If 'reference an X-Header If Not (msg.Headers("X-Mailer") Is Nothing) Then Console.WriteLine(msg.Headers("X-Mailer").Value) End If 'done Console.WriteLine("done.") Console.ReadLine() End Sub End Module
Copyright 2003 - Contact: Webmaster Last Updated: Friday, May 10, 2013