If you get an “Invalid Party object type 9” error when you’re trying to send an email, either from code or from a workflow, it is because you have set the From Party to a team.
For instance:
//Defining Activity Parties
Entity toParty = new Entity(ActivityParty.EntityLogicalName);
Entity fromParty = new Entity(ActivityParty.EntityLogicalName);
//set partyid
toParty["partyid"] = new EntityReference(Contact.EntityLogicalName, contactGuid.Id);
fromParty["partyid"] = new EntityReference(Team.EntityLogicalName, consumerTeam.Id);
//create email entity
Entity email = new Entity(Email.EntityLogicalName);
email["from"] = new Entity[] { fromParty };
email["to"] = new Entity[] { toParty };
email["subject"] = "Account Login Information";
email["description"] = PopulateBody(userName, password);
email["directioncode"] = true;
email["regardingobjectid"] = new EntityReference(Contact.EntityLogicalName, contactGuid.Id);
Guid emailId = Create(email);
//Sending email
SendEmailRequest reqSendEmail = new SendEmailRequest();
reqSendEmail.EmailId = emailID; //ID of created mail
reqSendEmail.TrackingToken = "";
reqSendEmail.IssueSend = true;
SendEmailResponse res = (SendEmailResponse)Execute(reqSendEmail);
Or it could be that you set
fromParty["partyid"] = opportunity.OwnerId;
It’s completely valid for an Opportunity to have a Team as owner, but an email will only accept a SystemUser or a Queue as From Party.
So in the cases where the Opportunity owner is a SystemUser, the email will be sent. If the owner is a Team, it will generate the error above.
In order to send the mail, you must specify either a SystemUser or a Queue as From Party:
fromParty["partyid"] = new EntityReference(SystemUser.EntityLogicalName, fromUser.SystemUserId.Value);
or
fromParty["partyid"] = new EntityReference(Queue.EntityLogicalName, fromQueue.QueueId.Value);