Here is a small sendemail transcript function and explanation for Encompass
private void SaveEmailTranscript(EmailTemplate template, List<EmailAttachment> atts)
{
StringBuilder stringBuilder = new StringBuilder();
List<string> values = new List<string>();
values.AddRange((IEnumerable<string>) template.ToAddresses.GetAddresses(template));
values.AddRange((IEnumerable<string>) template.CCAddresses.GetAddresses(template));
values.AddRange((IEnumerable<string>) template.BCCAddresses.GetAddresses(template));
stringBuilder.AppendLine(“From: ” + EncompassApplication.CurrentUser.FullName + “<” + EncompassApplication.CurrentUser.Email + “>”);
stringBuilder.AppendLine(“Sent: ” + DateTime.Now.ToString(“MM/dd/yyyy hh:mm:ss tt”));
stringBuilder.AppendLine(“To: ” + string.Join(“; “, (IEnumerable<string>) values));
stringBuilder.AppendLine(“Subject: ” + template.Subject.Merge());
string str = “”;
foreach (EmailAttachment att in atts)
str = str + att.Name + “;”;
stringBuilder.AppendLine(“Attachments: ” + str);
stringBuilder.AppendLine();
stringBuilder.AppendLine(template.Body.Merge().ConvertToPlainText() ?? “”);
string path = Environment.CurrentDirectory + “\\” + Guid.NewGuid().ToString() + “.txt”;
System.IO.File.WriteAllText(path, stringBuilder.ToString());
bool imageAttachments = Session.DefaultInstance.ConfigurationManager.GetImageAttachmentSettings().UseImageAttachments;
Loan currentLoan = EncompassApplication.CurrentLoan;
EllieMae.Encompass.BusinessObjects.Loans.Attachment attachment = imageAttachments ? currentLoan.Attachments.AddImage(path) : currentLoan.Attachments.Add(path);
attachment.Title = template.TemplateName;
if (template.TranscriptDocs != null && template.TranscriptDocs.Count > 0)
{
if (!string.IsNullOrEmpty(template.TranscriptDocs[0].AttName))
attachment.Title = template.TranscriptDocs[0].AttName;
this.MoveToFolder(EncompassApplication.CurrentLoan, attachment.Title, template.TranscriptDocs[0].Key);
}
if (!System.IO.File.Exists(path))
return;
System.IO.File.Delete(path);
}
Here is explanation
The provided C# code defines a `SaveEmailTranscript` method that appears to be responsible for saving an email transcript to a text file and attaching it to a loan document. Let’s break down what this method does step by step:
1. **StringBuilder Initialization**: It starts by initializing a `StringBuilder` named `stringBuilder`. This object is used to build the content that will be saved in the text file.
2. **Recipient Addresses**: It collects recipient addresses from the `template` object’s `ToAddresses`, `CCAddresses`, and `BCCAddresses` properties and stores them in a `List<string>` called `values`.
3. **Constructing Email Header**:
– It appends the sender’s information (current user’s name and email).
– It appends the current date and time as the “Sent” timestamp.
– It appends the recipient addresses, separated by semicolons.
– It appends the email subject from the `template`.
– It creates a string `str` to hold a list of attachment names.
4. **Attachment Names**:
– It iterates through a list of `EmailAttachment` objects (`atts`) and concatenates their names into the `str` string, separated by semicolons.
5. **Newlines and Email Body**:
– It appends several newline characters to format the email header.
– It appends the email body from the `template`, converts it to plain text (if not null), or appends an empty string if the body is null.
6. **File Path Generation**: It generates a unique file path for saving the transcript as a text file. The file path is based on the current directory and a new GUID (Globally Unique Identifier).
7. **Saving to Text File**: It uses `System.IO.File.WriteAllText` to save the content in the `stringBuilder` to the text file at the specified path.
8. **Attachment to Loan Document**:
– It determines whether to use image attachments based on a configuration setting.
– It retrieves the current loan object (`currentLoan`) from the EncompassApplication.
– It adds the saved text file as an attachment to the loan document. If image attachments are enabled, it adds it as an image attachment; otherwise, as a regular attachment.
– It sets the title of the attachment to the `template.TemplateName`.
9. **Handling TranscriptDocs (Conditional)**:
– If the `template.TranscriptDocs` collection is not null and contains items, it checks if the first item has a non-empty `AttName`.
– If there’s a non-empty `AttName`, it sets the attachment’s title to that value.
– It then calls a `MoveToFolder` method, presumably to move the attachment to a specific folder within the loan.
10. **Cleanup**:
– It checks if the saved file still exists (it may have been deleted or moved during processing), and if it exists, it deletes it.
In summary, this method builds a text representation of an email transcript, saves it to a text file, attaches it to a loan document, and handles specific scenarios related to the attachment’s title and folder location. It also performs cleanup by deleting the saved file if it still exists.