Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/XrmMockup365/Requests/SendEmailRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,15 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E
if (activityParty.Contains("partyid"))
{
var partyRef = activityParty.GetAttributeValue<EntityReference>("partyid");
if (db.GetEntityOrNull(partyRef) is null)
var partyEntity = db.GetEntityOrNull(partyRef);
if (partyEntity is null)
{
throw new FaultException($"{partyRef.LogicalName} with Id = {partyRef.Id} does not exist");
}
if (string.IsNullOrEmpty(partyEntity.GetAttributeValue<string>("emailaddress1")))
{
throw new FaultException($"{partyRef.LogicalName} with Id = {partyRef.Id} does not have an email address");
}
}
else if (!activityParty.Contains("addressused"))
{
Expand All @@ -122,7 +127,6 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E
throw new FaultException("The email must have at least one recipient before it can be sent");
}


email["statecode"] = new OptionSetValue(EMAIL_STATE_COMPLETED);
email["statuscode"] = new OptionSetValue(request.IssueSend ? EMAIL_STATUS_PENDING_SEND : EMAIL_STATUS_SENT);

Expand Down
37 changes: 37 additions & 0 deletions tests/XrmMockup365Test/TestSendEmail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,5 +356,42 @@ public void TestSendEmailRequestFailsWhenRecipientInvalid()

Assert.Throws<FaultException>(() => orgAdminUIService.Execute(sendEmailRequest));
}

[Fact]
public void TestSendEmailRequestFailsWhenRecipientHasNoEmailAddress1()
{
var contact = new Contact
{
FirstName = "Test"
};
contact.Id = orgAdminUIService.Create(contact);

var email = new Email
{
From = new ActivityParty[]
{
new ActivityParty
{
PartyId = crm.AdminUser
}
},
To = new ActivityParty[]
{
new ActivityParty
{
PartyId = contact.ToEntityReference()
}
},
Subject = "Test Email",
};
email.Id = orgAdminUIService.Create(email);

var sendEmailRequest = new SendEmailRequest
{
EmailId = email.Id
};

Assert.Throws<FaultException>(() => orgAdminUIService.Execute(sendEmailRequest));
}
}
}
Loading