Here is a code snippet of send email for one of the encompass plugin
public void SendEmail(EmailTemplate template)
{
try
{
// ISSUE: reference to a compiler-generated field
if (Main.\u003C\u003Eo__38.\u003C\u003Ep__0 == null)
{
// ISSUE: reference to a compiler-generated field
Main.\u003C\u003Eo__38.\u003C\u003Ep__0 = CallSite<Func<CallSite, object, MailItem>>.Create(Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof (MailItem), typeof (Main)));
}
MailItem message = Main.\u003C\u003Eo__38.\u003C\u003Ep__0.Target((CallSite) Main.\u003C\u003Eo__38.\u003C\u003Ep__0, ((_Application) Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid(“0006F03A-0000-0000-C000-000000000046”)))).CreateItem(OlItemType.olMailItem));
if (template.FromAddresses != null && template.FromAddresses.Count > 0)
message.SentOnBehalfOfName = template.FromAddresses.GetAddresses(template)[0];
message.To = template.ToAddresses.GetAddresses(template).Join();
message.CC = template.CCAddresses.GetAddresses(template).Join();
message.BCC = template.BCCAddresses.GetAddresses(template).Join();
// ISSUE: reference to a compiler-generated method
message.PropertyAccessor.SetProperty(“http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-EmailTemplate”, (object) template.TemplateName);
string empty = string.Empty;
string str = template.Body;
Tuple<List<string>, string> tuple = ImageExtract.Process(str);
if (tuple != null)
{
str = message.SetImages(tuple.Item2, tuple.Item1);
if (Main._EmailSettings.AdvAuditLog)
Helpers.UpdateErrorLogField(EncompassApplication.CurrentLoan, “Success: Images added from template”, template.TemplateName);
}
if (!string.IsNullOrEmpty(message.To) || !string.IsNullOrEmpty(message.CC) || !string.IsNullOrEmpty(message.BCC))
{
message.Subject = template.Subject.Merge();
if (Main._EmailSettings.AdvAuditLog)
Helpers.UpdateErrorLogField(EncompassApplication.CurrentLoan, “Success: Subject Line field merge completed.”, template.TemplateName);
List<EmailAttachment> attachmentsUniversal = template.GetAttachmentsUniversal();
foreach (EmailAttachment emailAttachment in attachmentsUniversal)
{
// ISSUE: variable of a compiler-generated type
OlAttachmentType Type = OlAttachmentType.olByValue;
// ISSUE: reference to a compiler-generated method
message.Attachments.Add((object) emailAttachment.Path, (object) Type, (object) 1, (object) (emailAttachment.Name ?? “”));
if (Main._EmailSettings.AdvAuditLog)
Helpers.UpdateErrorLogField(EncompassApplication.CurrentLoan, “Success: Attachments added”, template.TemplateName);
}
if (!template.DoNotUseSig)
{
// ISSUE: reference to a compiler-generated method
message.Display(Type.Missing);
message.HTMLBody = str.Merge() + message.HTMLBody;
}
else
message.HTMLBody = str.Merge();
if (Main._EmailSettings.AdvAuditLog)
Helpers.UpdateErrorLogField(EncompassApplication.CurrentLoan, “Success: Body text field merge completed”, template.TemplateName);
if (template.AllowEdits)
{
// ISSUE: reference to a compiler-generated method
message.Display(Type.Missing);
}
else
{
// ISSUE: reference to a compiler-generated method
message.Send();
}
if (Main._EmailSettings.AdvAuditLog)
Helpers.UpdateErrorLogField(EncompassApplication.CurrentLoan, “Success: Email sent or Openned via Outlook”, template.TemplateName);
this.UpdateLogsAndTranscripts(template, attachmentsUniversal);
EncompassApplication.CurrentLoan.LogLoanRequest(nameof (SendEmail));
}
else
Helpers.UpdateErrorLogField(EncompassApplication.CurrentLoan, template.TemplateName + ” Email Template will not send as the email does not have any recipients.”, template.TemplateName);
}
catch (Exception ex)
{
Helpers.UpdateErrorLogField(EncompassApplication.CurrentLoan, string.Format(“Error sending {0} automated email\n\n{1}”, (object) template.TemplateName, (object) ex), template.TemplateName);
}
}
ok, here is some explanation
This C# code defines a `SendEmail` method. It’s responsible for sending emails based on an `EmailTemplate` provided as an argument. Let’s break down the functionality of this method step by step:
1. **Try-Catch Block**: The entire method is wrapped in a try-catch block, which is used to handle any exceptions that may occur during the execution of the code. If an exception is caught, it logs an error message with details about the exception.
2. **Creating a MailItem Object**:
– The code creates a `MailItem` object named `message`. This object represents an email message.
3. **Setting Sender and Recipients**:
– It sets the “SentOnBehalfOfName” property of the `message` if there are “FromAddresses” defined in the `template`.
– It sets the “To,” “CC,” and “BCC” fields of the `message` based on the recipient addresses defined in the `template`.
4. **Custom Property**: It sets a custom property on the `message` using the `PropertyAccessor.SetProperty` method. This custom property seems to store the template name.
5. **Processing and Embedding Images**:
– It processes the email body of the `template` to extract and embed images if any are found. The processed body is stored in the `str` variable.
– If images are processed, it updates an error log field (likely for auditing purposes).
6. **Setting Subject Line**: It sets the subject of the email message based on the `template`.
7. **Attaching Files**:
– It retrieves a list of email attachments from the `template`.
– It iterates through the list and attaches each file to the email message.
– If attachments are added, it updates an error log field.
8. **Handling Email Body**:
– If the template does not specify to not use a signature, it displays the email message (likely to allow the user to edit it) and appends the email body (`str`) to the HTMLBody of the message.
– If the template specifies not to use a signature, it sets the HTMLBody directly.
9. **Sending or Displaying the Email**:
– If there are recipients specified (To, CC, or BCC), it either displays the email message (if template.AllowEdits is true) or sends the email.
– If the email is sent, it updates an error log field indicating success.
10. **Updating Logs and Transcripts**: The method calls `UpdateLogsAndTranscripts` to perform some additional actions related to logs and transcripts.
11. **Logging**: If there are no recipients specified, it logs an error indicating that the email won’t be sent because there are no recipients.
12. **Exception Handling**: Any exceptions that occur during the process are caught, and an error log is updated with details of the exception.
In summary, this method is responsible for creating and sending emails based on an `EmailTemplate`. It handles various aspects of email creation, including sender, recipients, subject, attachments, and the email body. It also handles exceptions and logs relevant information for auditing purposes.