EmailUtils.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using InABox.Reports;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net.Mail;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Controls;
  14. using System.Windows;
  15. using System.Windows.Forms;
  16. using System.Drawing;
  17. using InABox.WPF;
  18. using MessageBox = System.Windows.Forms.MessageBox;
  19. using Comal.Classes;
  20. using TextBox = System.Windows.Controls.TextBox;
  21. namespace PRSDesktop
  22. {
  23. public class EmailUtils
  24. {
  25. /// <summary>
  26. /// Creates and opens an email with the default email app - selected by the user.
  27. /// This method is for emails with a PDF attachment. Provide the file name and data.
  28. /// Optionally provide from, subject and body.
  29. /// If from is not provided, an attempt will be made to find the User's email address - if empty it will throw an error (cannot be empty)
  30. /// </summary>
  31. /// <param name="attachmentname"></param>
  32. /// <param name="attachmentdata"></param>
  33. /// <param name="from"></param>
  34. /// <param name="subject"></param>
  35. /// <param name="body"></param>
  36. public static void CreateEMLFile(string attachmentname, byte[] attachmentdata, string from = "", string subject = "", string body = "", string to = "")
  37. {
  38. var message = CreateMessage(from, subject, body, to);
  39. message = AddAttachment(message, attachmentname, attachmentdata);
  40. OpenEmail(message, attachmentname);
  41. }
  42. /// <summary>
  43. /// Creates and opens an email with the default email app - selected by the user.
  44. /// This method is for emails with no attachments.
  45. /// Optionally provide from, subject and body.
  46. /// If from is not provided, an attempt will be made to find the User's email address - if empty it will throw an error (cannot be empty)
  47. /// </summary>
  48. /// <param name="from"></param>
  49. /// <param name="subject"></param>
  50. /// <param name="body"></param>
  51. public static void CreateEMLFile(string from = "", string subject = "", string body = "")
  52. {
  53. var message = CreateMessage(from, subject, body);
  54. OpenEmail(message, "Message from " + from);
  55. }
  56. private static void OpenEmail(MailMessage message, string name)
  57. {
  58. var filename = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(name, ".eml"));
  59. using (var filestream = File.Open(filename, FileMode.Create))
  60. {
  61. var binaryWriter = new BinaryWriter(filestream);
  62. //Write the Unsent header to the file so the mail client knows this mail must be presented in "New message" mode
  63. binaryWriter.Write(Encoding.UTF8.GetBytes("X-Unsent: 1" + Environment.NewLine));
  64. var assembly = typeof(SmtpClient).Assembly;
  65. var mailWriterType = assembly.GetType("System.Net.Mail.MailWriter")!;
  66. // Get reflection info for MailWriter contructor
  67. var mailWriterConstructor =
  68. mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(Stream), typeof(bool) }, null)!;
  69. // Construct MailWriter object with our FileStream
  70. var mailWriter = mailWriterConstructor.Invoke(new object[] { filestream, true });
  71. // Get reflection info for Send() method on MailMessage
  72. var sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic)!;
  73. sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true, true }, null);
  74. // Finally get reflection info for Close() method on our MailWriter
  75. var closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic)!;
  76. // Call close method
  77. closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { }, null);
  78. }
  79. // Open the file with the default associated application registered on the local machine
  80. Process.Start(new ProcessStartInfo(filename) { UseShellExecute = true });
  81. }
  82. private static MailMessage CreateMessage(string from, string subject, string body, string to = "")
  83. {
  84. if (string.IsNullOrWhiteSpace(to))
  85. to = "example@outlook.com.au";
  86. if (string.IsNullOrWhiteSpace(from))
  87. from = GetAddressFromUser();
  88. if (string.IsNullOrWhiteSpace(subject))
  89. subject = "Enter subject";
  90. if (string.IsNullOrWhiteSpace(body))
  91. body = "Enter message";
  92. var message = new MailMessage(from, to, subject, body);
  93. message.IsBodyHtml = false;
  94. return message;
  95. }
  96. private static string GetAddressFromUser()
  97. {
  98. CoreTable table = new Client<User>().Query(new Filter<User>(x => x.ID).IsEqualTo(ClientFactory.UserGuid)
  99. , new Columns<User>(x => x.EmailAddress));
  100. User user = table.Rows.FirstOrDefault().ToObject<User>();
  101. if (!string.IsNullOrWhiteSpace(user.EmailAddress))
  102. return user.EmailAddress;
  103. else
  104. MessageBox.Show("Current User Email Address is blank - please fill in (Human Resources -> User Accounts -> Choose your User -> Email Settings -> Email Address", "Error");
  105. return "";
  106. }
  107. private static MailMessage AddAttachment(MailMessage message, string attachmentname, byte[] attachmentdata)
  108. {
  109. var attachment = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(attachmentname, ".pdf"));
  110. File.WriteAllBytes(attachment, attachmentdata);
  111. message.Attachments.Add(new Attachment(attachment));
  112. return message;
  113. }
  114. public static IEnumerable<ReportExportDefinition> CreateTemplateDefinitions(DataModel model)
  115. {
  116. var templates = new Client<DataModelTemplate>().Query(new Filter<DataModelTemplate>(x => x.Model).IsEqualTo(model.Name)
  117. .And(x => x.Visible).IsEqualTo(true));
  118. if (templates.Rows.Any())
  119. {
  120. List<ReportExportDefinition> list = new List<ReportExportDefinition>();
  121. foreach (CoreRow row in templates.Rows)
  122. {
  123. Action<DataModel, byte[]> action = new Action<DataModel, byte[]>((model, data) =>
  124. {
  125. DoEmailAction(model, data, row.Get<DataModelTemplate, string>(x => x.Name));
  126. });
  127. list.Add(
  128. new ReportExportDefinition(
  129. "Email Report",
  130. ImageUtils.CreatePreviewWindowButtonContent(row.Get<DataModelTemplate, string>(x => x.Name),PRSDesktop.Resources.emailreport),
  131. ReportExportType.PDF,
  132. action));
  133. }
  134. return list;
  135. }
  136. else
  137. return new List<ReportExportDefinition>()
  138. {
  139. new ReportExportDefinition(
  140. "Email Report",
  141. ImageUtils.CreatePreviewWindowButtonContent("Email",PRSDesktop.Resources.emailreport),
  142. ReportExportType.PDF,
  143. DoEmailReport)
  144. };
  145. }
  146. private static void DoEmailAction(DataModel model, byte[] data, string templateName)
  147. {
  148. var template = new Client<DataModelTemplate>().Query(new Filter<DataModelTemplate>(x => x.Name).IsEqualTo(templateName)).Rows.FirstOrDefault();
  149. ParseTemplateAndCreateEmail(template, model, data);
  150. }
  151. private static void ParseTemplateAndCreateEmail(CoreRow row, DataModel model, byte[] data)
  152. {
  153. var to = DataModelUtils.ParseTemplate(model, row.Get<DataModelTemplate, string>(x => x.To)).Replace("\n", "").Replace("\r", "");
  154. var Subject = DataModelUtils.ParseTemplate(model, row.Get<DataModelTemplate, string>(x => x.Subject)).Replace("\n", "").Replace("\r", "");
  155. var attachmentName = DataModelUtils.ParseTemplate(model, row.Get<DataModelTemplate, string>(x => x.AttachmentName)).Replace("\n", "").Replace("\r", "");
  156. var body = DataModelUtils.ParseTemplate(model, row.Get<DataModelTemplate, string>(x => x.Template));
  157. if (string.IsNullOrWhiteSpace(attachmentName))
  158. attachmentName = model.Name;
  159. EmailUtils.CreateEMLFile(attachmentName, data, App.EmployeeEmail, Subject, body, to);
  160. }
  161. public static void DoEmailReport(DataModel model, byte[] data)
  162. {
  163. string attachmentName = DetermineName(model);
  164. EmailUtils.CreateEMLFile(attachmentName, data, App.EmployeeEmail, "Emailing report for " + attachmentName);
  165. }
  166. private static string DetermineName(DataModel model)
  167. {
  168. string title = model.Name;
  169. if (model.AsDictionary.ContainsKey(typeof(Requisition)))
  170. {
  171. CoreTable table = model.AsDictionary[typeof(Requisition)];
  172. title = title + " - " + table.Rows.FirstOrDefault().Get<Requisition, string>(x => x.Title);
  173. }
  174. else if (model.AsDictionary.ContainsKey(typeof(PurchaseOrder)))
  175. {
  176. title = "Purchase Order ";
  177. CoreTable table = model.AsDictionary[typeof(PurchaseOrder)];
  178. if (table.Rows.Count == 1)
  179. title = title + table.Rows.FirstOrDefault().Get<PurchaseOrder, string>(x => x.PONumber);
  180. else if (table.Rows.Count > 1)
  181. {
  182. foreach (CoreRow row in table.Rows)
  183. {
  184. title = title + row.Get<PurchaseOrder, string>(x => x.PONumber) + ", ";
  185. }
  186. title = title.Substring(0, title.Length - 2);
  187. }
  188. }
  189. return title;
  190. }
  191. }
  192. }