Skip to main content

Notifications

Announcements

No record found.

Customer Service forum

Unable to send an Email With Attachment using custom Workflow

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

}

}

}

}

Error Details:

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

<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="">schemas.microsoft.com/.../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>

  • Hareesh Ganoji Profile Picture
    Hareesh Ganoji 25 on at
    RE: Unable to send an Email With Attachment using custom Workflow

    Thank you Omar. Issue got resolved.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to send an Email With Attachment using custom Workflow

    The activitymimeattachment doesn't have "objectid" and you are trying to populate one. Look at the documentation here https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/activitymimeattachment?view=dynamics-ce-odata-9

    It might be objectid_email is what you are looking for? 

    Also if the above doesn't work, try to populate the body_binary field in the email attachment entity, I suspect it is related but no harm in trying...

    Thanks

    Omar

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

#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