Note that there are some explanatory texts on larger screens.

plurals
  1. POC# + Append DataTable row to html email template
    text
    copied!<p>I have a DataTable of records which I need to append as information to my Email template htm. Currently what I am doing here works fine but that is if I only have 1 row of record. How can I go about appending the htm template such that I can have multiple postings in the email</p> <p>e.g Sample Email Screen <strong>(Assuming my DataTable returns 3 rows of record)</strong>:</p> <pre><code>Dear Sir, your daily car posting results: Image Toyota Cambry $10000 Image Honda GT $10000 Image Nissan Sunny $10000 </code></pre> <p><strong>Loop DataTable row:</strong></p> <pre><code> for (int i = 0; i &lt; dt.Rows.Count; i++) { DataRow dr = dt.Rows[i]; primaryImage = dr["PrimaryImage"].ToString(); email = dr["Email"].ToString(); postTitle = dr["Model"].ToString(); model = dr["Model"].ToString(); askingPrice = dr["AskingPrice"].ToString(); var mail = new Email(); mail.IsBodyHtml = true; mail.MailAddresses = email; mail.MailSubject = "Test"; mail.HtmFileName = "Email.htm"; var dict = new Dictionary&lt;string, string&gt; { {"&lt;%PrimaryImage%&gt;", primaryImage }, {"&lt;%PostTitle%&gt;", postTitle}, {"&lt;%Model%&gt;", model}, {"&lt;%AskingPrice%", askingPrice} }; mail.Dict = dict; MailMessage mailMessage; mailMessage = mail.CreateMailMessage(); Email.Send(mailMessage, 3, 3000, true); } } </code></pre> <p><strong>Create Mail Message:</strong></p> <pre><code>public MailMessage CreateMailMessage() { MailMessage mail = new MailMessage(); mail.IsBodyHtml = IsBodyHtml; mail.From = new MailAddress("xxx@yahoo.com", "xxx"); mail.Bcc.Add(MailAddresses); mail.Subject = MailSubject; string body = ""; string filePath = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["TEMPLATES"] + "/"); if (File.Exists(filePath + HtmFileName)) { FileStream f = new FileStream(filePath + HtmFileName, FileMode.Open); StreamReader sr = new StreamReader(f); body = sr.ReadToEnd(); foreach (var pair in Dict) { body = body.Replace(pair.Key, pair.Value); } f.Close(); } mail.Body = body; mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure; return mail; } </code></pre> <p><strong>Portion of the Email.htm template:</strong></p> <pre><code>&lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div style="border: thin solid #E1E1E1; background-color: #F0F0F0; margin-bottom: 10px; padding-top: 3px; padding-bottom: 3px; padding-left:5px;"&gt; Postings &lt;/div&gt; &lt;div&gt; &lt;a&gt;&lt;img src="&lt;%PrimaryImage%&gt;"/&gt;&lt;/a&gt; &lt;br/&gt;&lt;br/&gt; Sell Post Title: &lt;%PostTitle%&gt; &lt;br/&gt;&lt;br/&gt; Model: &lt;%Model%&gt; &lt;br/&gt;&lt;br/&gt; Asking Price: &lt;%AskingPrice%&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload