Using the DeliverIncomingEmail Message

On March 31, 2010, in Development, Dynamics CRM, Email, by Mitch Milam

Every now and again, you find an absolute gem within the CRM SDK.  And I’m not talking about a diamond-in-the-rough, I’m talking about a bright, shiny, put it in a ring kind.

The DeliverIncomingEmail message is one of those gems.  Let’s take a look:

 

What does it do?

First and foremost, it takes information that exists within an email message that you retrieved from somewhere and creates a CRM Email Activity. This process could be a custom application you wrote, or in my case, a Simple Email Router.  Regardless of where the email comes from, this message will do all of the heavy lifting required to create the Email Activity.

 

Usage:

DeliverIncomingEmailRequest request = new DeliverIncomingEmailRequest
{
    Subject = subject,
    From = from,
    To = to,
    Cc = cc,
    Bcc = bcc,
    MessageId = Guid.NewGuid().ToString(),
    ReceivedOn = CrmTypes.CreateCrmDateTimeFromUser(timeReceived),
    Body = body,
    SubmittedBy = to,
    Importance = "Normal",
    Attachments = new BusinessEntityCollection()
};

request.Attachments.EntityName = EntityName.activitymimeattachment.ToString();
request.Attachments.BusinessEntities.Add(new activitymimeattachment());

 

How It Works

You complete the parameters of the message with information from the inbound email; things like To, From, Subject, Body, etc. the use the CrmService.Execute method to deliver the mail.  That’s it.

 

Caveats

MessageId is a Guid that can be either the ID of the message or a new Guid you supply.

Attachments are required, even if you don’t have attachments.  In the example above, we have created a BusinessEntityCollection to hold our attachments, but never actually added any.  This will keep the Message from failing to process.

To, Cc, Bcc can contain multiple email addresses that are separated by a comma ( , ).

 

So why is this such a big deal?

Did you notice that we are only supplying the email addresses of the people involved in the conversation?  When CRM processes this message, it will perform an automatic lookup and attempt to associate people with those email addresses.

This is actually one of the few messages within the CRM SDK that doesn’t require an ID to perform a lookup or an association and saves you, the developer, a tremendous amount of time and effort.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>