Download unseen emails from Gmail using IMAP video

In this video you’ll learn how to:

  • Create new application that references Mail.dll IMAP component.
  • Configure Gmail to allow IMAP access.
  • Connect and login to Gmail using SSL secured channel.
  • Search and download unseen emails.
  • Parse downloaded email messages.
  • and finally access most common email properties like subject, sender, attachments.

Remember to enable IMAP in Gmail (your authentication options are app-passwords and OAuth2.0).

Enjoy:

Receive iCalendar meeting request

Mail.dll .NET email component makes receiving iCalendar meeting request fairly easy.

IMail object exposes Appointments collection that contains all appointments that were found while parsing an email.

You can use both IMAP or POP3 protocol to download emails from the server.

Here’s the simple sample showing how to process iCalendar appointments:

// C#

IMail email = new MailBuilder().CreateFromEml(client.GetMessageByUID(uid));

foreach (Appointment appointment in email.Appointments)
{
    if (appointment.Method == Method.Request)
    {
        // appointment was created
        string summary = appointment.Event.Summary;
        DateTime? start = appointment.Event.Start;
        DateTime? end = appointment.Event.End;
        string location = appointment.Event.Location;

        Console.WriteLine("{0} at {1} ({2}-{3})", summary, location, start, end);

        foreach (Participant participant in appointment.Event.Participants)
        {
            Console.WriteLine("Common name: " + participant.Cn);
            Console.WriteLine("Email: " + participant.Email);
            Console.WriteLine("Participation status: " + participant.Status);
        }
    }
    else if (appointment.Method == Method.Cancel)
    {
        // appointment was canceled
        Console.WriteLine("Event was cancelled: " + appointment.Event.UID);
        
    }
    else if (appointment.Method == Method.Reply)
    {
        // someone replied to the request
        foreach (Participant participant in appointment.Event.Participants)
        {
            if (participant.Status == ParticipationStatus.Accepted)
                Console.WriteLine("Event was accepted by: " + participant.Email);
            else if (participant.Status == ParticipationStatus.Declined)
                Console.WriteLine("Event was declined by: " + participant.Email);
        }

    }
}

' VB.NET

Dim email As IMail = New MailBuilder().CreateFromEml(client.GetMessageByUID(uid))

For Each appointment As Appointment In email.Appointments
	If appointment.Method = Method.Request Then
		' appointment was created
		Dim summary As String = appointment.[Event].Summary
		Dim start As System.Nullable(Of DateTime) = appointment.[Event].Start
		Dim [end] As System.Nullable(Of DateTime) = appointment.[Event].[End]
		Dim location As String = appointment.[Event].Location

		Console.WriteLine("{0} at {1} ({2}-{3})", summary, location, start, [end])

		For Each participant As Participant In appointment.[Event].Participants
			Console.WriteLine("Common name: " + participant.Cn)
			Console.WriteLine("Email: " + participant.Email)
			Console.WriteLine("Participation status: " + participant.Status)
		Next
	ElseIf appointment.Method = Method.Cancel Then
		' appointment was canceled

		Console.WriteLine("Event was cancelled: " + appointment.[Event].UID)
	ElseIf appointment.Method = Method.Reply Then
		' someone replied to the request
		For Each participant As Participant In appointment.[Event].Participants
			If participant.Status = ParticipationStatus.Accepted Then
				Console.WriteLine("Event was accepted by: " + participant.Email)
			ElseIf participant.Status = ParticipationStatus.Declined Then
				Console.WriteLine("Event was declined by: " + participant.Email)
			End If

		Next
	End If
Next

Create barcode in WinForms video

In this video you’ll learn how to:

  • Download and install Barcode.dll barcode component.
  • Create new WinForms application that references Barcode.dll barcode component.
  • Darg & drop barcode windows control
  • Configure barcode windows control.

Enjoy:

Archive email in Gmail

First remember to enable IMAP in Gmail.

To archive message in Gmail you need to use DeleteMessageByUID method. Using INBOX folder is crucial for Archiving to work. If you use DeleteMessageByUID when other folder/label is selected, Gmail will not archive the message, but rather remove currently selected label.

After this operation messages will be still available in “All Mail” IMAP folder.

// C#

using(Imap imap = new Imap())
{
	imap.ConnectSSL("imap.gmail.com");
	imap.Login("user", "password");

	// Find all emails we want to delete
	imap.SelectInbox();
	List<long> uids = imap.Search(
		Expression.Subject("email to archive"));

	imap.DeleteMessageByUID(uids);

	imap.Close();
}
' VB.NET

Using imap As New Imap()
	imap.ConnectSSL("imap.gmail.com")
	imap.Login("user@gmail.com", "password")

	' Find all emails we want to delete
	imap.SelectInbox()
	Dim uids As List(Of Long) = imap.Search(_
		Expression.Subject("email to archive"))

	imap.DeleteMessageByUID(uids)

	imap.Close()
End Using

Send iCalendar meeting requests for different timezone

Usually you define time of an event in relation to current time zone. If you want to send such event to recipient in different time zone you should use UTC. This way you can be sure that you both meet at correct time.

First add all necessary namespaces:

// C# version

using System;
using Limilabs.Mail;
using Limilabs.Mail.Appointments;
using Fluent = Limilabs.Mail.Fluent;
using Limilabs.Client.SMTP;
' VB.NET version

Imports System;
Imports Limilabs.Mail;
Imports Limilabs.Mail.Appointments;
Imports Fluent = Limilabs.Mail.Fluent;
Imports Limilabs.Client.SMTP;

The easiest way to achieve this is to create specify time using local DateTime instance and invoke ToUniversalTime method, which translates it to UTC timezone. This also handles daylight saving time differences.

You can also use overloaded DateTime constructor that uses UTC timezone: DateTime(2012, 08, 17, 12, 00, 00, DateTimeKind.Utc).

// C#

DateTime start = new DateTime(2012, 08, 17, 12, 00, 00).ToUniversalTime();

Appointment appointment = new Appointment();
Event e = appointment.AddEvent();
e.Start = start;
e.End = start + TimeSpan.FromMinutes(30);
e.Summary = "At noon";

e.SetOrganizer(new Person("Alice", "alice@example.org"));

e.AddParticipant(
new Participant("Bob", "bob@example.org", ParticipationRole.Required, true));
e.AddParticipant(
new Participant("Tom", "tom@example.org", ParticipationRole.Optional, true));
e.AddParticipant(
new Participant("Alice", "alice@example.org", ParticipationRole.Required, true));

Alarm alarm = e.AddAlarm();
alarm.BeforeStart(TimeSpan.FromMinutes(15));

IMail email = Fluent.Mail
.Text("Status meeting at noon.")
.Subject("Status meeting")
.From("alice@example.org")
.To("bob@example.org")
.To("tom@example.org")
.AddAppointment(appointment)
.Create();

using (Smtp client = new Smtp())
{
client.ConnectSSL("smtp.example.org");
client.UseBestLogin("user", "password");
client.SendMessage(email);
client.Close();
}
' VB.NET

Dim start As DateTime = New DateTime(2012, 8, 17, 12, 0, 0).ToUniversalTime()

Dim appointment As New Appointment()
Dim e As [Event] = appointment.AddEvent()
e.Start = start
e.[End] = start + TimeSpan.FromMinutes(30)
e.Summary = "At noon"

e.SetOrganizer(New Person("Alice", "alice@example.org"))

e.AddParticipant( _
New Participant("Bob", "bob@example.org", ParticipationRole.Required, True))
e.AddParticipant( _
New Participant("Tom", "tom@example.org", ParticipationRole.[Optional], True))
e.AddParticipant( _
New Participant("Alice", "alice@example.org", ParticipationRole.Required, True))

Dim alarm As Alarm = e.AddAlarm()
alarm.BeforeStart(TimeSpan.FromMinutes(15))

Dim email As IMail = Fluent.Mail.Text("Status meeting at noon.") _
.Subject("Status meeting") _
.From("alice@example.org") _
.[To]("bob@example.org") _
.[To]("tom@example.org") _
.AddAppointment(appointment) _
.Create()

Using client As New Smtp()
client.ConnectSSL("smtp.example.org")
client.UseBestLogin("user", "password")
client.SendMessage(email)
client.Close()
End Using

Things get a bit more complicated if you want to define recurring event in different timezone. You can not use UTC time zone in such case.