web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Forward Email with attachment

(1) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • sardar ahmed Profile Picture
    520 on at

    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.

  • Suggested answer
    Shiva.B Profile Picture
    150 on at

    Hi Samuel,

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

  • Community Member Profile Picture
    on at

    Hi Sardar,

    Thank you for the response!

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

  • Community Member Profile Picture
    on at

    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
    on at

    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);

        }

     }

    }

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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);

                                       }

                                   }

  • Suggested answer
    Shaik Profile Picture
    on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans