|
| 1 | +using Newtonsoft.Json.Linq; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.DocIO.DLS; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Net.Mail; |
| 7 | + |
| 8 | +namespace Create_and_send_email_messages |
| 9 | +{ |
| 10 | + class Program |
| 11 | + { |
| 12 | + static void Main(string[] args) |
| 13 | + { |
| 14 | + //Creates new Word document instance for Word processing |
| 15 | + using (WordDocument template = new WordDocument()) |
| 16 | + { |
| 17 | + //Opens the Word template document |
| 18 | + Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Template.docx")); |
| 19 | + template.Open(docStream, FormatType.Docx); |
| 20 | + docStream.Dispose(); |
| 21 | + |
| 22 | + //Gets the recipient details as DataTable |
| 23 | + List<object> recipients = GetRecipients(); |
| 24 | + foreach (var dataRecord in recipients) |
| 25 | + { |
| 26 | + //Clones the template document for creating new document for each record in the data source |
| 27 | + WordDocument document = template.Clone(); |
| 28 | + |
| 29 | + //Performs the mail merge |
| 30 | + document.MailMerge.Execute(new List<object>() { dataRecord as Dictionary<string, object> }); |
| 31 | + |
| 32 | + //Save the HTML file as string |
| 33 | + docStream = new MemoryStream(); |
| 34 | + document.SaveOptions.HtmlExportOmitXmlDeclaration = true; |
| 35 | + document.Save(docStream, FormatType.Html); |
| 36 | + //Releases the resources occupied by WordDocument instance |
| 37 | + document.Dispose(); |
| 38 | + docStream.Position = 0; |
| 39 | + StreamReader reader = new StreamReader(docStream); |
| 40 | + string mailBody = reader.ReadToEnd(); |
| 41 | + docStream.Dispose(); |
| 42 | + if (mailBody.StartsWith("<!DOCTYPE")) |
| 43 | + mailBody = mailBody.Remove(0, 97); |
| 44 | + //Sends the email message |
| 45 | + //Update the required e-mail id here |
| 46 | + SendEMail("[email protected]", "[email protected]", "You order #" + (dataRecord as Dictionary<string, object>)["OrderID"].ToString() + " has been shipped", mailBody); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + #region Helper methods |
| 51 | + private static void SendEMail(string from, string recipients, string subject, string body) |
| 52 | + { |
| 53 | + //Creates the email message |
| 54 | + var emailMessage = new MailMessage(from, recipients); |
| 55 | + //Adds the subject for email |
| 56 | + emailMessage.Subject = subject; |
| 57 | + //Sets the HTML string as email body |
| 58 | + emailMessage.IsBodyHtml = true; |
| 59 | + emailMessage.Body = body; |
| 60 | + //Sends the email with prepared message |
| 61 | + using (var client = new SmtpClient()) |
| 62 | + { |
| 63 | + //Update your SMTP Server address here |
| 64 | + client.Host = "smtp.live.com"; |
| 65 | + client.UseDefaultCredentials = false; |
| 66 | + //Update your email credentials here |
| 67 | + client.Credentials = new System.Net.NetworkCredential(from, "password"); |
| 68 | + client.Port = 587; |
| 69 | + client.EnableSsl = true; |
| 70 | + client.Send(emailMessage); |
| 71 | + } |
| 72 | + } |
| 73 | + /// <summary> |
| 74 | + /// Gets the data to perform mail merge. |
| 75 | + /// </summary> |
| 76 | + /// <returns></returns> |
| 77 | + private static List<object> GetRecipients() |
| 78 | + { |
| 79 | + //Reads the JSON object from JSON file. |
| 80 | + JObject jsonObject = JObject.Parse(File.ReadAllText(@"../../../CustomerDetails.json")); |
| 81 | + //Converts JSON object to Dictionary. |
| 82 | + IDictionary<string, object> data = GetData(jsonObject); |
| 83 | + return data["Customers"] as List<object>; |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Gets data from JSON object. |
| 88 | + /// </summary> |
| 89 | + /// <param name="jsonObject">JSON object.</param> |
| 90 | + /// <returns>Dictionary of data.</returns> |
| 91 | + private static IDictionary<string, object> GetData(JObject jsonObject) |
| 92 | + { |
| 93 | + Dictionary<string, object> dictionary = new Dictionary<string, object>(); |
| 94 | + foreach (var item in jsonObject) |
| 95 | + { |
| 96 | + object keyValue = null; |
| 97 | + if (item.Value is JArray) |
| 98 | + keyValue = GetData((JArray)item.Value); |
| 99 | + else if (item.Value is JToken) |
| 100 | + keyValue = ((JToken)item.Value).ToObject<string>(); |
| 101 | + dictionary.Add(item.Key, keyValue); |
| 102 | + } |
| 103 | + return dictionary; |
| 104 | + } |
| 105 | + /// <summary> |
| 106 | + /// Gets array of items from JSON array. |
| 107 | + /// </summary> |
| 108 | + /// <param name="jArray">JSON array.</param> |
| 109 | + /// <returns>List of objects.</returns> |
| 110 | + private static List<object> GetData(JArray jArray) |
| 111 | + { |
| 112 | + List<object> jArrayItems = new List<object>(); |
| 113 | + foreach (var item in jArray) |
| 114 | + { |
| 115 | + object keyValue = null; |
| 116 | + if (item is JObject) |
| 117 | + keyValue = GetData((JObject)item); |
| 118 | + jArrayItems.Add(keyValue); |
| 119 | + } |
| 120 | + return jArrayItems; |
| 121 | + } |
| 122 | + #endregion |
| 123 | + } |
| 124 | +} |
0 commit comments