Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Sending Emails Via workflows in MS CRM 2016 onPremises

Posted on by Microsoft Employee

Good Day,

I have CRM email router set up to exchange 2010.

I once the email enters CRM i am supposed to create another email with a different body and link to the previous incoming email and forward this to another email address.

My challenge is in attachments,an email enters CRM with attachments but once I forward as outgoing email it then loses all the attachments.

I need to be able to forward this emails together with all the attachments how can I achieve this?

and again any signature contain a drawing is displayed as html on the new email how can I display this as it is like in the original incoming email?

I have followed a few guidelines online but they did not do much because I copied the code as it is but really no attachments are sent see below address here

the links suggest I use a workflow activity but this workflow activity is not working as expected maybe I have my code wrong.

See code below.

Thanks

using System;

namespace MoveAttachmentsToCase
{
     public class MoveAttachmentsToCase :CodeActivity
    {
        //define output variable

        [Input("SourceEmail")]

        [ReferenceTarget("email")]

        public InArgument<EntityReference> SourceEmail { get; set; }

     
         protected override void Execute(CodeActivityContext executionContext)
         {    //Create the service object
           IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
           IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
           IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            // Get the e-mail reference object
            // Get the target entity from the context

            // Get the target entity from the context

             Entity  AccountID= (Entity)service.Retrieve("email", context.PrimaryEntityId, new ColumnSet(new string[] { "activityid" }));

            AddAttachmentToEmailRecord(service, AccountID.Id, SourceEmail.Get<EntityReference>(executionContext));

      }

          private void AddAttachmentToEmailRecord(IOrganizationService service, Guid SourceAccountID, EntityReference SourceEmailID)

          {

                //create email object

            Entity _ResultEntity = service.Retrieve("email", SourceEmailID.Id, new ColumnSet(true));

            QueryExpression _QueryNotes = new QueryExpression("annotation");

           _QueryNotes.ColumnSet = new ColumnSet(new string[] { "subject", "mimetype", "filename", "documentbody" });

           _QueryNotes.Criteria = new FilterExpression();

           _QueryNotes.Criteria.FilterOperator = LogicalOperator.And;

           _QueryNotes.Criteria.AddCondition(new ConditionExpression("objectid", ConditionOperator.Equal, SourceAccountID));

         EntityCollection _MimeCollection = service.RetrieveMultiple(_QueryNotes);

            if (_MimeCollection.Entities.Count > 0)

                    {  //we need to fetch first attachment

                     Entity _NotesAttachment = _MimeCollection.Entities.First();

                  //Create email attachment

                    Entity _EmailAttachment = new Entity("activitymimeattachment");

                  if (_NotesAttachment.Contains("subject"))

                  _EmailAttachment["subject"] = _NotesAttachment.GetAttributeValue<string>("subject");

                  _EmailAttachment["objectid"] = new EntityReference("email", _ResultEntity.Id);

                  _EmailAttachment["objecttypecode"] = "email";

                  if (_NotesAttachment.Contains("filename"))

                   _EmailAttachment["filename"] = _NotesAttachment.GetAttributeValue<string>("filename");

                   if (_NotesAttachment.Contains("documentbody"))

                  _EmailAttachment["body"] = _NotesAttachment.GetAttributeValue<string>("documentbody");

                   if (_NotesAttachment.Contains("mimetype"))

                  _EmailAttachment["mimetype"] = _NotesAttachment.GetAttributeValue<string>("mimetype");

                   service.Create(_EmailAttachment);

          }

      

         }
     }
}

*This post is locked for comments

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Sending Emails Via workflows in MS CRM 2016 onPremises

    Hi,

     not sure about the rest of the code, but you should be querying activitymimeattachment, not annotation here:

    QueryExpression _QueryNotes = new QueryExpression("annotation");

    annotations are "notes"

    activitymimeattachments are email attachments, and that's what you need to copy

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans