Posts Tagged ‘Mail.dll’

Send vCard business card

Saturday, March 10th, 2012

Mail.dll .NET email component makes sending vCard business cards easy.

MailBuilder class contains AddVCard method, that can be used to add vCard business card as a attachment to your email.
You can use VCard class to create VCARD business card. It provides easy to use API to add phone, email and address information.

Here’s the simple sample showing how to send email with VCard business card:

// C#

// Create VCard business card

VCard vCard = new VCard();

vCard.FullName = "John Doe";
vCard.Name = new VCardName("John", "Doe");
vCard.Organization = new VCardOrganization("Example");
vCard.Title = "CEO";
vCard.Url = "http://www.example.com";

VCardEmail workEmail = new VCardEmail("john.doe@example.com");
workEmail.MarkWork();
workEmail.MarkPreferred();
vCard.Emails.Add(workEmail);

VCardEmail homeEmail = new VCardEmail("john.doe@gmail.com");
homeEmail.MarkHome();
vCard.Emails.Add(homeEmail);

VCardAddress workAddress = new VCardAddress("", "", "501 E. Middlefield Rd.", "Mountain View", "CA", "94043", "U.S.A.");
workAddress.MarkWork();
vCard.Addresses.Add(workAddress);

VCardAddress homeAddress = new VCardAddress("", "", "6544 Battleford Drive", "Raleigh", "NC", "27613-3502", "U.S.A.");
homeAddress.MarkHome();
vCard.Addresses.Add(homeAddress);

VCardPhone homePhone = new VCardPhone("+1-919-676-9515");
homePhone.MarkHome();
vCard.Phones.Add(homePhone);

VCardPhone workPhone = new VCardPhone();
workPhone.AsUri("tel:+1-919-676-9564");
workPhone.MarkWork();
workPhone.MarkFax();
workPhone.MarkVoice();
vCard.Phones.Add(workPhone);

// Create email message

MailBuilder builder = new MailBuilder();
builder.From.Add(new MailBox("john.doe@example.com"));
builder.To.Add(new MailBox("bob@example.com"));
builder.Text = "Business card";
builder.Text = "Here's my business card.";
builder.AddVCard(vCard);
IMail email = builder.Create();

// Send email message using SMTP protocol

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

' Create VCard business card

Dim vCard As New VCard()

vCard.FullName = "John Doe"
vCard.Name = New VCardName("John", "Doe")
vCard.Organization = New VCardOrganization("Example")
vCard.Title = "CEO"
vCard.Url = "http://www.example.com"

Dim workEmail As New VCardEmail("john.doe@example.com")
workEmail.MarkWork()
workEmail.MarkPreferred()
vCard.Emails.Add(workEmail)

Dim homeEmail As New VCardEmail("john.doe@gmail.com")
homeEmail.MarkHome()
vCard.Emails.Add(homeEmail)

Dim workAddress As New VCardAddress("", "", "501 E. Middlefield Rd.", "Mountain View", "CA", "94043", _
	"U.S.A.")
workAddress.MarkWork()
vCard.Addresses.Add(workAddress)

Dim homeAddress As New VCardAddress("", "", "6544 Battleford Drive", "Raleigh", "NC", "27613-3502", _
	"U.S.A.")
homeAddress.MarkHome()
vCard.Addresses.Add(homeAddress)

Dim homePhone As New VCardPhone("+1-919-676-9515")
homePhone.MarkHome()
vCard.Phones.Add(homePhone)

Dim workPhone As New VCardPhone()
workPhone.AsUri("tel:+1-919-676-9564")
workPhone.MarkWork()
workPhone.MarkFax()
workPhone.MarkVoice()
vCard.Phones.Add(workPhone)

' Create email message

Dim builder As New MailBuilder()
builder.From.Add(New MailBox("john.doe@example.com"))
builder.[To].Add(New MailBox("bob@example.com"))
builder.Text = "Business card"
builder.Text = "Here's my business card."
builder.AddVCard(vCard)
Dim email As IMail = builder.Create()

' Send email message using SMTP protocol

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

Such email can be parsed and VCard extracted.

Receive vCard business card

Saturday, March 3rd, 2012

Mail.dll .NET email component makes receiving vCard business cards easy.

IMail object exposes VCards collection that contains all vCard business cards that were found while parsing an email.

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

Here’s the simple sample showing how to process VCard business cards:

// C#

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

foreach (VCard vCard in email.VCards)
{

    Console.WriteLine("first name: " + vCard.Name.FirstName);
    Console.WriteLine("last name: " + vCard.Name.LastName);
    Console.WriteLine("full name: " + vCard.FullName);

    Console.WriteLine("email: " + vCard.Email);
    Console.WriteLine("work phone: " + vCard.WorkPhone);
    Console.WriteLine("home phone: " + vCard.HomePhone);

    if (vCard.WorkAddress != null)
    {
        Console.WriteLine(vCard.WorkAddress.PostOfficeBox);
        Console.WriteLine(vCard.WorkAddress.ApartmentNumber);
        Console.WriteLine(vCard.WorkAddress.Street);
        Console.WriteLine(vCard.WorkAddress.City);
        Console.WriteLine(vCard.WorkAddress.Region);
        Console.WriteLine(vCard.WorkAddress.PostalCode);
        Console.WriteLine(vCard.WorkAddress.Country);
    }
    if (vCard.HomeAddress != null)
    {
        //...
    }

    foreach (VCardPhone phone in vCard.Phones)
    {
        Console.WriteLine("phone: " + phone.Value);
    }
    foreach (VCardAddress address in vCard.Addresses)
    {
        Console.WriteLine("street: " + address.Street);
    }
    foreach (VCardEmail mail in vCard.Emails)
    {
        Console.WriteLine("email: " + mail.Value);
    }
}
' VB.NET

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

For Each vCard As VCard In email.VCards

	Console.WriteLine("first name: " + vCard.Name.FirstName)
	Console.WriteLine("last name: " + vCard.Name.LastName)
	Console.WriteLine("full name: " + vCard.FullName)

	Console.WriteLine("email: " + vCard.Email)
	Console.WriteLine("work phone: " + vCard.WorkPhone)
	Console.WriteLine("home phone: " + vCard.HomePhone)

	If vCard.WorkAddress IsNot Nothing Then
		Console.WriteLine(vCard.WorkAddress.PostOfficeBox)
		Console.WriteLine(vCard.WorkAddress.ApartmentNumber)
		Console.WriteLine(vCard.WorkAddress.Street)
		Console.WriteLine(vCard.WorkAddress.City)
		Console.WriteLine(vCard.WorkAddress.Region)
		Console.WriteLine(vCard.WorkAddress.PostalCode)
		Console.WriteLine(vCard.WorkAddress.Country)
	End If
			'...
	If vCard.HomeAddress IsNot Nothing Then
	End If

	For Each phone As VCardPhone In vCard.Phones
		Console.WriteLine("phone: " + phone.Value)
	Next
	For Each address As VCardAddress In vCard.Addresses
		Console.WriteLine("street: " + address.Street)
	Next
	For Each mail As VCardEmail In vCard.Emails
		Console.WriteLine("email: " + mail.Value)
	Next
Next

You can learn here how to send email with VCard.

Download unseen emails from Gmail using IMAP video

Wednesday, February 1st, 2012

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.

Enjoy:

Receive iCalendar meeting request

Monday, January 30th, 2012

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 email 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

Archive email in Gmail

Friday, January 6th, 2012

First remember to enable IMAP in Gmail.

You need to use DeleteMessageByUID method. Messages will be still available in All Mail 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