EmailUtils.cs 10 KB

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