Sending an email via CRM Online

Back to Blog Listing

CRM has its own functionality for sending and tracking emails. Sometimes it is required to use that functionality from within custom code as emails are limited in the entity values they can access via the CRM interface.

To create an email in CRM is fairly simple:

Dim serProxy As OrganizationServiceProxy
Using serProxy
    Dim ser As IOrganizationService = CType(serProxy, IOrganizationService)
    Dim em As New Email
    em.Subject = "Invoice: "
    em.Description = [emailContent]
   Dim emailID As Guid = ser.Create(em)
End Using

To add recipients and a from address must be done by creating ActivityParty objects. The from and to parameters of an email are an array of these objects, allowing one to many of each to be added. These ActivityParty objects can, as when using the CRM interface, be of an account or contact type.

Dim em As New Email
Dim emFrom(1) As ActivityParty
Dim eFrom As New ActivityParty
eFrom.PartyId = New EntityReference(Contact.EntityLogicalName, New Guid("7FBC77C9-785B-E311-BA57-D89D67639E24"))
emFrom(0) = eFrom
em.From = emFrom
Dim emTo(1) As ActivityParty
Dim eTo As New ActivityParty
eTo.PartyId = New EntityReference(Account.EntityLogicalName, inv.CustomerId.Id)

emTo(0) = eTo

em.To = emTo
Dim emailID As Guid = ser.Create(em)

The regarding object can be an entity of near any type, you must only specify the type when adding the entity reference.

Dim em As New Email
em.RegardingObjectId = New EntityReference(Invoice.EntityLogicalName, New Guid(InvoiceGUID))
Dim emailID As Guid = ser.Create(em)


 Once the email has been successfully created then it can be sent via the CRM using the following code.

Dim sendRequest As New SendEmailRequest
sendRequest.EmailId = emailID
sendRequest.IssueSend = True


About the Author

James CTwo is our blogging alter ego for the Dynamics CRM and Web experts here at C2. We have a passion for anything CRM, Web Design or Social Media. We love writing about it! Find us on YouTube & Twitter.

comments powered by Disqus

Our White Papers

Discover the content that matters to you on business transformation. Explore how you can drive customer engagement, empower your team, optimise your businesses and revolutionise your products with the next digital revolution. For more information visit our white paper library.


C2 Newsletter

Our newsletter is sent on a quarterly basis, offering detailed insights into all aspects of business transformation with Microsoft cloud technologies. We also promote upcoming events and special offers throughout the year. Sign up to get involved today.

Dynamics 365 Office 365 Power BI Silver Partner