Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Forward Email with attachment

Posted on by Microsoft Employee

I have a workflow that triggers when an email is received. Then a new email is created and sent out, but the attachments are missed out. Is there a way to configure CRM to dynamically add received attachments to the newly created email? Any code or steps will be much appreciated.

*This post is locked for comments

  • Suggested answer
    Shaik Profile Picture
    Shaik on at
    RE: Forward Email with attachment

    Hi Samuel_T,

    There is readily available workflow tool kit from Andrri, you can make use of it or try to use the concept of what Andrii is doing, below is the blog and source code of it

    butenko.pro/.../ultimate-workflow-toolkit-v1-3-0-0

    github.com/.../v1.2.0.0

    Thanks,

    Shaik

    If you find this as helpful Mark as Answer

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Forward Email with attachment

    You can use the following two functions to retrieve email attachments and append email attachments:

    Note that you first have to create the new email message. Your steps are:

    1. Create New Email Message

    2. Retrieve Email Attachments

    3. Attach Email Attachments from Received Message to Target Message

           private EntityCollection RetrieveEmailAttachments(Guid emailid)

           {

               EntityCollection entities = new EntityCollection();

               ColumnSet columns = new ColumnSet("activitymimeattachmentid", "objectid", "objecttypecode", "subject", "filename", "filesize", "attachmentid");

               QueryExpression query = new QueryExpression

               {

                   ColumnSet = columns,

                   EntityName = "activitymimeattachment",

                   Criteria =

                   {

                       Conditions =

                       {

                           new ConditionExpression("objectid", ConditionOperator.Equal, emailid)

                       }

                   }

               };

               try

               {

                   entities = service.RetrieveMultiple(query);

               }

               catch (FaultException<OrganizationServiceFault> ex)

               {

                   throw new InvalidPluginExecutionException(ex.Message);

               }

               return entities;

           }

    After you get the attachments collection, add the following code to append the attachments to the email message that you created. This is used linked attachments, so no new attachment records are created.

    EntityCollection attachments = RetrieveEmailAttachments(receivedEmailId);

    // target email id below is the email id of the newly created email message to be created.

                                   if (attachments.Entities.Count > 0)

                                   {

                                       foreach (Entity attachment in attachments.Entities)

                                       {

                                           string attachmentSubject = attachment.Contains("subject") ? attachment["subject"].ToString() : string.Empty;

                                           string attachmentFilename = attachment.Contains("filename") ? attachment["filename"].ToString() : string.Empty;

                                           Entity linkedAttachment = new Entity("activitymimeattachment");

                                           linkedAttachment.Attributes["objectid"] = new EntityReference("email", targetEmailId);

                                           linkedAttachment.Attributes["objecttypecode"] = "email";

                                           if (!string.IsNullOrEmpty(attachmentSubject))

                                               linkedAttachment.Attributes["subject"] = attachmentSubject;

                                           if (!string.IsNullOrEmpty(attachmentFilename))

                                               linkedAttachment.Attributes["filename"] = attachmentFilename;

                                           linkedAttachment.Attributes["attachmentid"] = new EntityReference("attachment", ((EntityReference)attachment.Attributes["attachmentid"]).Id);

                                           service.Create(linkedAttachment);

                                       }

                                   }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Forward Email with attachment

    Any help correcting this sample plugin to use the received received description, subject and all attachments to send a new email to another email address:

    public class EMailPlugin : IPlugin

    {

     public void Execute(IServiceProvider serviceProvider)

     {

        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

        {

           IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

           IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

           Entity account = (Entity)context.InputParameters["Target"];

           Entity email = new Entity("email");

           Entity fromParty = new Entity("activityparty");

           Entity toParty = new Entity("activityparty");

           toParty["partyid"] = new EntityReference("systemuser", context.UserId);

           fromParty["partyid"] = new EntityReference("systemuser", context.UserId);

           email["from"] = new Entity[] { fromParty };

           email["to"] = new Entity[] { toParty };

           email["subject"] = "email subject - " + DateTime.Now.ToString();

           email["description"] = "email description";

           email["regardingobjectid"] = new EntityReference("account", account.Id);

           Guid emailId = service.Create(email);

           SendEmailRequest sendEmailreq = new SendEmailRequest

           {

              EmailId = emailId,

              TrackingToken = "",

              IssueSend = true

           };

           SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);

        }

     }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Forward Email with attachment

    Hi Shiva,

    Thank you for taking the time to respond!

    Any chance you could share with me a sample code of either plugin or custom work flow that I could use.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Forward Email with attachment

    Hi Sardar,

    Thank you for the response!

    If possible, please share with me the sample code as I could not find it?

  • Suggested answer
    Shiva.B Profile Picture
    Shiva.B 150 on at
    RE: Forward Email with attachment

    Hi Samuel,

    You can use plugin or custom work flow to get attachment id and assign that attachment to newly created or replay email

  • sardar ahmed Profile Picture
    sardar ahmed 520 on at
    RE: Forward Email with attachment

    Hello, Samuel

    You need to write a custom workflow for the above,

    1. Take the input parameter of type email, pass the received email to this parameter.

    2. Fetch the attachments from the received email.

    3. Create the new email.

    4. Attach the retrieved attachments

    5. Send email.

    You can easily get the samples to retrieve attachments, sending emails in MS CRM SDK.

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans