Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Unable to send an Email With Attachment using custom Workflow

(0) ShareShare
ReportReport
Posted on by 25

Hi,

Our requirement is emails that are received from the customer have to create a case for this we used Rule item and if the email that are contains attachments that has to be create a note to the related case after that we create an email using workflow and attached notes to email before it is sending to the customer representative everything worked out fine till attaching the files from notes to email after attaching the notes to email. I am getting an exception like“Unexpected Error ” at this line of code “SendEmailResponse res = (SendEmailResponse)service.Execute(SendEmail)” after spending a one day we realized that the attachments which are made from the note entity to email using the custom code are restricting us to Execute the SendEmailReponse and the thing is that attachments which are attached from notes to email are not corrupted after removing the attachments that are attached using the custom code we able to send the mail manually by click the send button on the email created by the workflow.please help me out to get rid of this. Below is the code I am using to attach the note to email.

namespace AttatchingNoteToEmail

{

    public class EmailAttatchment : CodeActivity

    {

        [RequiredArgument]

        [Input("TargetEmail")]

        [ReferenceTarget("email")]

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

         protected override void Execute(CodeActivityContext executionContext)

            {

 

            try

            {

 

 

                IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

                IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();

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

                ITracingService trace = executionContext.GetExtension<ITracingService>();

                EntityReference targetEmail = TargetEmail.Get<EntityReference>(executionContext);

                Entity = service.Retrieve(targetEmail.LogicalName, targetEmail.Id, new ColumnSet(true));

                EntityReference Case = .GetAttributeValue<EntityReference>("regardingobjectid");

                Guid CaseId = Case.Id;

 

                string fetchXML =

                             "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";

                fetchXML += "  <entity name='annotation'>";

                fetchXML += "    <attribute name='subject' />";

                fetchXML += "    <attribute name='filename' />";

                fetchXML += "    <attribute name='documentbody' />";

                fetchXML += "    <attribute name='mimetype' />";

                fetchXML += "    <attribute name='annotationid' />";

                fetchXML += "    <order attribute='subject' descending='false' />";

                fetchXML += "    <link-entity name='incident' from='incidentid' to='objectid' alias='ac'>";

                fetchXML += "      <filter type='and'>";

                fetchXML += "        <condition attribute='incidentid' operator='eq' value='" + CaseId + "' />";

                fetchXML += "      </filter>";

                fetchXML += "    </link-entity>";

                fetchXML += "  </entity>";

                fetchXML += "</fetch>";

                FetchExpression fetchQuery = new FetchExpression(fetchXML);

                EntityCollection _MimeCollection = service.RetrieveMultiple(fetchQuery);

                if (_MimeCollection.Entities.Count > 0) {

                    foreach (var notes in _MimeCollection.Entities)

                    {

                        Entity _EmailAttachment = new Entity("activitymimeattachment");

                        if (notes.Contains("subject"))

                        {

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

                        }

                        if (notes.Contains("filename"))

                        {

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

                        }

                        if (notes.Contains("documentbody"))

                        {

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

                         }

                        if (notes.Contains("mimetype"))

                        {

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

                        }

                        _EmailAttachment["objectid"] = new EntityReference(targetEmail.LogicalName, targetEmail.Id);

                        _EmailAttachment["objecttypecode"] = "email";

                        service.Create(_EmailAttachment);

                    }

                 }

                SendEmailRequest SendEmail = new SendEmailRequest();

                SendEmail.EmailId = targetEmail.Id;

                SendEmail.TrackingToken = "";

                SendEmail.IssueSend = true;

                SendEmailResponse res = (SendEmailResponse)service.Execute(SendEmail);

 

            }

            catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }

        }

               

     }

   }

1106.pastedimage1566799242018v1.png

Error Details:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

<s:Body><s:Fault><faultcode>s:Client</faultcode>

<faultstring xmlns:xml="">www.w3.org/.../namespace" xml:lang="en-US">An unexpected error occurred.</faultstring>

<detail><OrganizationServiceFault xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">

<ErrorCode>-2147220970</ErrorCode><ErrorDetails />

<Message>An unexpected error occurred.</Message>

<Timestamp>2019-08-26T05:56:03.4954553Z</Timestamp>

<InnerFault><ErrorCode>-2147220970</ErrorCode>

<ErrorDetails /><Message>System.FormatException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #556ED4D6</Message>

<Timestamp>2019-08-26T05:56:03.4954553Z</Timestamp>

<InnerFault xmlns:i="">www.w3.org/.../XMLSchema-instance" i:nil="true" />

<TraceText xmlns:i="">www.w3.org/.../XMLSchema-instance" i:nil="true" /></InnerFault>

<TraceText xmlns:i="">www.w3.org/.../XMLSchema-instance" i:nil="true" />

</OrganizationServiceFault>

</detail>

</s:Fault>

</s:Body>

</s:Envelope>

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Dynamics 365 general forum

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans