Hello,
I am writing an plugin to send an email to a user. Does anyone know how I could do this? I have seen articles referencing ActivityParty, but I was not able to add the correct reference to use this class.
Any ideas?
Thanks,
Devin
*This post is locked for comments
Aileen,
i having error on this line :
SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail); //error line
reference :
SendEmailRequest reqSendEmail = new SendEmailRequest();
reqSendEmail.EmailId = _emailId;//ID of created mail
reqSendEmail.TrackingToken = "";
reqSendEmail.IssueSend = true;
SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);
ANSWER:
I was able to get the email to send. One has go into the User profile in CRM, and click on "APPROVE EMAIL"
This needs to be part of the documentation. It should be more obvious that the email mailbox needs to be approved before emails can be sent via code. Workflows send emails fine, but anything externally will not send unless you have approved the email address!
Call to method:
sendEmail(new Guid("7891B00E-ED80-E211-B2EA-02BFAC1A6434"), owner.Key, "systemuser", "Test Email", "Test Description", owner.Value);
Method:
private void sendEmail(Guid From, Guid To, string ToEntitySchemaName, string Subject, string Description, List<string> urls)
{
var description = new StringBuilder();
description.AppendLine("Hello,");
description.AppendLine("Text");
description.AppendLine("<ul>");
foreach (var address in urls) {
description.AppendFormat("<li>{0}</li>", string.Format(HyperLinkFormat, address, "Link to things"));
}
description.AppendLine("</ul>");
try
{
// Create 'From' activity party for the email
ActivityParty fromParty = new ActivityParty
{
PartyId = new EntityReference(SystemUser.EntityLogicalName, From)
};
// Create 'To' activity party for the email
ActivityParty toParty = new ActivityParty
{
PartyId = new EntityReference(ToEntitySchemaName, To)
};
// Create an e-mail message
Email email = new Email
{
To = new ActivityParty[] { toParty },
From = new ActivityParty[] { fromParty },
Subject = Subject,
Description = description.ToString(),
DirectionCode = true
};
Guid _emailId = _service.Create(email);
SendEmailRequest sendEmailreq = new SendEmailRequest
{
EmailId = _emailId,
TrackingToken = "",
IssueSend = true
};
SendEmailResponse sendEmailresp = (SendEmailResponse)_service.Execute(sendEmailreq);
if (sendEmailresp != null)
{
//Console.WriteLine("Email record created successfully");
//Console.ReadKey();
}
}
catch (Exception ex)
{
//Console.Write("Errorex.Message);
//Console.ReadKey();
throw new InvalidPluginExecutionException(ex.Message);
}
}
Devin,
Can you copy paste your code here?
It seems you copy paste a systemuser guid but use entity type other than allowed entities (user and queue)
Hello,
I have implemented the code that was found in the SDK to send an email. However I am getting an error upon debugging the code.
"the specified sender type is not supported"
I am passing a guid of a systemuser in CRM.
You can use the SendEmailRequest class. The partyId and type are mandatory.
There's sample code available within CRM SDK. You can reference the below link
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,151 Super User 2024 Season 2
Martin Dráb 229,993 Most Valuable Professional
nmaenpaa 101,156