Create Email Message with Attachment from a Note

Recently I saw a request of how to create an email message with an attachment that exists from a Note, and how to do this via a Plugin. I remembered doing something like that in an old project, so I thought that I would share the logic behind this.

The first thing is to decide how this will be called. This logic can be called from an Action or Plugin, but the logic will reside in backend code. How to initiate the process is up to you. Once we initiate the process, the first thing to do is to retrieve the notes from the existing entity that contains the note documents. In order to retrieve the notes, we need to implement the following logic:

        private EntityCollection RetrieveAnnotations(string entityName, Guid entityId)
        {
            QueryExpression query = new QueryExpression(Annotation.EntityLogicalName)
            {
                ColumnSet = new ColumnSet(true),
                Criteria =
                {
                    Conditions =
                    {
                        new ConditionExpression("objectid", ConditionOperator.Equal, entityId),
                        new ConditionExpression("objecttypecode", ConditionOperator.Equal, entityId)
                    }
                }
            };

            EntityCollection results = service.RetrieveMultiple(query);
            return results;
        }

 This logic will retrieve all of the annotations related to a particular entity record. I did not add another condition for IsDocument, but you can add it if required. I will show that in the end. The next step is to create to more functions. The first function is to create an email message, and the second in to add the attachment to the email message that I created. Let's take a look at each one of these functions separately.

The Create Email Message function receives 4 Parameters: From (Type Entity), To (Type Entity), Subject (Type String) and Email Body (Type String), and returns the Guid of the Email Message that was created. The source is shown below, and can be modified to fit your exact needs:

        private Guid CreateEmailMessage(Entity from, Entity to, string subject, string description)
        {
            Guid emailid = Guid.Empty;
            Entity email = new Entity("email");

            EntityCollection fromParty = new EntityCollection() { EntityName = "activityparty" };
            fromParty.Entities.Add(from);
            email.Attributes["from"] = fromParty;

            EntityCollection toParty = new EntityCollection() { EntityName = "activityparty" };
            toParty.Entities.Add(to);
            email.Attributes["to"] = toParty;

            email.Attributes["subject"] = subject;
            email.Attributes["description"] = description;

            try
            {
                emailid = service.Create(email);
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                string message = "An error occurred in the CreateEmailMessage function";
                throw new InvalidPluginExecutionException(message);
            }
            return emailid;
        }

Once we create the email message we can add the attachment to the message. The information for creating the attachment is retrieved from the notes, so there is no real changes required. We will see how everything fits together at the end.

        private Guid AddAttachmentsToEmail(Guid emailId, string fileName, string documentBody, string mimeType)
        {
            Entity attachment = new Entity("activitymimeattachment");
            attachment["subject"] = "Attachment to Email";
            attachment["filename"] = fileName;
            attachment["mimetype"] = mimeType;
            attachment["body"] = documentBody;

            attachment["objectid"] = new Entity("email", emailId);
            attachment["objecttypecode"] = "email";

            try
            {
                Guid attachmentId = service.Create(attachment);
                return attachmentId;
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                string message = "An error occurred in the AddAttachmentsToEmail function";
                throw new InvalidPluginExecutionException(message);
            }
        }

 Now that the Email message is created, and the attachment is added to the email message, we need to just Send the Email Message. The function only needs the email Id of the message, but can be modified as needed:

        private void SendEmail(Guid emailId)
        {
            SendEmailRequest request = new SendEmailRequest();
            request.EmailId = emailId;
            request.TrackingToken = "";
            request.IssueSend = true;

            try
            {
                SendEmailResponse response = (SendEmailResponse)service.Execute(request);
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                string message = "An error occurred in the SendEmail function.");
                throw new InvalidPluginExecutionException(message);
            }
        }

Finally, we can put everything together in the CreateEmailLogic function which will retrieve the notes by calling the RetrieveAnnotations function, loop through the collection of the notes, check if they are a document, create the email message, add attachments and send the email. This is the way the entry point function looks like:

        private void CreateEmailLogic(string customEntityName, Guid entityRecordId)
        {
            EntityCollection notes = RetrieveAnnotations(customEntityName, entityRecordId);
            if (notes.Entities.Count > 0)
            {
                foreach (Entity note in notes.Entities)
                {
                    bool isDocument = Convert.ToBoolean(note["isdocument"]);

                    if (isDocument)
                    {
                        // Should check these attributes exist in Note
                        string documentBody = note["documentbody"].ToString();
                        string mimeType = note["mimetype"].ToString();
                        string fileName = note["filename"].ToString();


                        // Need to get information to generate email message
                        Guid emailId = CreateEmailMessage(from, to, subject, emailMessage);
                        AddAttachmentsToEmail(emailId, fileName, documentBody, mimeType);
                        SendEmail(emailId);
                    }
                }
            }
        }

There are many possible variations to  the above logic and functions, but with the above you can accomodate the requirements that you need.