Skip to content

Instantly share code, notes, and snippets.

@ZhenDeng
Last active May 20, 2019 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZhenDeng/565c973f3ed7b125a2528016aadaf28a to your computer and use it in GitHub Desktop.
Save ZhenDeng/565c973f3ed7b125a2528016aadaf28a to your computer and use it in GitHub Desktop.
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("<div style='padding: 0; margin: 0; height: 100%; background:#fbfaf7;'>");
stringBuilder.Append("<table width='100%' height='100%' align='center' cellspacing='0' cellpadding='0' bgcolor='#fbfaf7'>");
stringBuilder.Append("<tbody>");
stringBuilder.Append("<tr><td style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545'>&nbsp;</td></tr>");
stringBuilder.Append("<tr>");
stringBuilder.Append("<td style='color:#343731;'><table width='600px' bgcolor='#ffffff' cellspacing='0' cellpadding='0' align='center' border='0' style='border:1px solid #232323;text-align:center'><tbody><tr><td style='padding:10px;color:#343731'>");
stringBuilder.Append("<img src='https://via.placeholder.com/150' width='80px' style='margin:0 auto;display:block'>");
stringBuilder.Append("<h1 style='font-weight:400;text-transform:uppercase'>Trojan Trading Company PTY LTD</h1><h4 style='font-weight:400;text-transform:uppercase'>Australia</h4>");
stringBuilder.Append("</td></tr></tbody></table></td></tr>");
stringBuilder.Append("<tr><td width='600' style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545'>");
stringBuilder.Append("<table cellspacing='0' cellpadding='0' width='600' align='center' bgcolor='#ffffff' border='0' style='border-right:1px solid #d3d3d3;border-left:1px solid #d3d3d3'>");
stringBuilder.Append("<tbody><tr><td align='center' style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545'>&nbsp;</td></tr>");
stringBuilder.Append("<tr><td style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545'>");
stringBuilder.Append("<h3 style='margin:15px 20px 10px 20px;font-size:1.1em;color:#454545;text-align:center'>Your Order #" + orderId + " for Customer " + currentUser.Account + "</h3>");
stringBuilder.Append("</td></tr>");
stringBuilder.Append("<tr><td style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545'><table width='90%' cellspacing='0' cellpadding='0' align='center' bgcolor='#ffffff' border='0'>");
stringBuilder.Append("<tbody><tr>");
stringBuilder.Append("<td style='padding:1em 0.25em;border-bottom:1px solid #c4c4c4'></td>");
stringBuilder.Append("<td style='padding:1em 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><strong style ='font-size:10px'>WLP ex.GST</strong></td>");
stringBuilder.Append("<td style='padding:1em 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><strong style='font-size:10px'>Buy Price ex.GST</strong></td>");
stringBuilder.Append("<td style='padding:1em 0.25em;border-bottom:1px solid #c4c4c4;text-align:center'><strong style ='font-size:10px'>Order Qty</strong></td>");
stringBuilder.Append("<td style='padding:1em 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><strong style='font-size:10px'>Line Amount ex.GST</strong></td></tr>");
foreach (var item in cart.ShoppingItems)
{
stringBuilder.Append("<tr><td style='padding:0 0.25em;border-bottom:1px solid #c4c4c4'><h4 style='margin:0'>#" + item.Product.Id + " " + item.Product.Name + "</h4></td>");
stringBuilder.Append("<td style='padding:0 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><p style='font:12px/1.5 Arial,Helvetica,sans-serif;margin:0 0 0 0'>$" + item.Product.OriginalPrice + "</p></td>");
if (currentUser.Role.ToLower() == "agent")
{
stringBuilder.Append("<td style='padding:0 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><strong>$" + String.Format("{0:0.00}", item.Product.AgentPrice) + "</strong></td>");
}
else if (currentUser.Role.ToLower() == "wholesaler")
{
stringBuilder.Append("<td style='padding:0 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><strong>$" + String.Format("{0:0.00}", item.Product.WholesalerPrice) + "</strong></td>");
}
stringBuilder.Append("<td style='padding:0 0.25em;border-bottom:1px solid #c4c4c4;text-align:center'>" + item.Amount + "</td>");
if (currentUser.Role.ToLower() == "agent")
{
stringBuilder.Append("<td style='padding:0 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><span><strong>$" + String.Format("{0:0.00}", item.Product.AgentPrice * item.Amount) + "</strong></span></td></tr>");
}
else if (currentUser.Role.ToLower() == "wholesaler")
{
stringBuilder.Append("<td style='padding:0 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><span><strong>$" + String.Format("{0:0.00}", item.Product.WholesalerPrice * item.Amount) + "</strong></span></td></tr>");
}
}
stringBuilder.Append("</tbody>");
stringBuilder.Append("<tfoot>");
stringBuilder.Append("<tr><td colspan='1'>&nbsp;</td><td colspan='3' width='100' style='padding:0.5em 0.25em;border-bottom:1px solid #c4c4c4'>Payment Method</td>");
stringBuilder.Append("<td width='100' style='padding:0.5em 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><strong><span>" + order.ClientMessage + "</span></strong></td></tr>");
stringBuilder.Append("<tr><td colspan='1'>&nbsp;</td><td colspan='3' width='100' style='padding:0.5em 0.25em;border-bottom:1px solid #c4c4c4'>You Will Pay Excl.GST</td>");
stringBuilder.Append("<td width='100' style='padding:0.5em 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><strong>$<span>" + String.Format("{0:0.00}", priceExclGst) + "</span></strong></td></tr>");
stringBuilder.Append("<tr><td colspan='1'>&nbsp;</td><td colspan='3' width='100' style='padding:0.5em 0.25em;border-bottom:1px solid #c4c4c4'>GST</td>");
stringBuilder.Append("<td width='100' style='padding:0.5em 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><strong>$<span>" + String.Format("{0:0.00}", gst) + "</span></strong></td></tr>");
stringBuilder.Append("<tr><td colspan='1'>&nbsp;</td><td colspan='3' width='100' style='padding:0.5em 0.25em;border-bottom:1px solid #c4c4c4'>You will pay Inc.GST</td>");
stringBuilder.Append("<td width='100' style='padding:0.5em 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><strong>$<span>" + String.Format("{0:0.00}", priceIncGst) + "</span></strong></td></tr>");
stringBuilder.Append("<tr><td colspan='1'>&nbsp;</td><td colspan='3' width='100' style='padding:0.5em 0.25em;border-bottom:1px solid #c4c4c4'>Total Discount Earned</td>");
stringBuilder.Append("<td width='100' style='padding:0.5em 0.25em;border-bottom:1px solid #c4c4c4;text-align:right'><strong>$<span>" + String.Format("{0:0.00}", discount) + "</span></strong></td></tr>");
stringBuilder.Append("</tfoot></table></td></tr>");
stringBuilder.Append("<tr><td style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545'><table width='90%'><tbody>");
stringBuilder.Append("<tr><td valign='top' width='50%' style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545'><h3 style='margin:15px 20px 10px 20px;font-size:1.1em;color:#454545;text-align:left'>Shipping Address</h3>");
stringBuilder.Append("<p style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545;margin:12px 20px 10px 20px;margin-bottom:0'>" + currentUser.Account + "<br>" + currentUser.ShippingCustomerName + "<br>");
stringBuilder.Append(currentUser.ShippingStreetNumber + " " + currentUser.ShippingAddressLine + "<br> " + currentUser.ShippingSuburb + ", " + currentUser.ShippingState + ", " + currentUser.ShippingPostCode + "<br>");
stringBuilder.Append("<strong>Email:</strong><a href='" + currentUser.Email + "' target='_blank'>" + currentUser.Email + "</a><br><strong>Phone:</strong>" + currentUser.Phone + "</p></td>");
stringBuilder.Append("<td valign='top' width='50%' style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545'><h3 style='margin:15px 20px 10px 20px;font-size:1.1em;color:#454545;text-align:left'>Billing Address</h3>");
stringBuilder.Append("<p style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545;margin:12px 20px 10px 20px;margin-bottom:0'>" + currentUser.Account + "<br>" + currentUser.BillingCustomerName + "<br>");
stringBuilder.Append(currentUser.BillingStreetNumber + " " + currentUser.BillingAddressLine + "<br> " + currentUser.BillingSuburb + ", " + currentUser.BillingState + ", " + currentUser.BillingPostCode + "<br>");
stringBuilder.Append("<strong>Email:</strong><a href='" + currentUser.Email + "' target='_blank'>" + currentUser.Email + "</a><br><strong>Phone:</strong>" + currentUser.Phone + "</p></td>");
stringBuilder.Append("</tr></tbody></table></td></tr>");
stringBuilder.Append("<tr><td style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545'>&nbsp;</td></tr>");
stringBuilder.Append("<tr><td style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545;background:#f6f4ef;text-align:center;border-bottom:1px solid #d3d3d3'><p style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#45659d;margin:12px 20px 10px 20px;text-decoration:none'><a style='color:#454545;text-decoration:none' target='_blank'><strong>https://XXXXXXXX</strong></a></p></td></tr>");
stringBuilder.Append("</tbody></table></td></tr>");
stringBuilder.Append("<tr><td style='font:12px/1.5 Arial,Helvetica,sans-serif;color:#454545'>&nbsp;</td></tr>");
stringBuilder.Append("</tbody></table></div>");
string emailBody = stringBuilder.ToString();
SendEmail("testprojectemail2019@gmail.com", "testprojectemail2019@gmail.com", "test", emailBody, "", "", true);
public bool SendEmail(string from, string to, string subject, string message, string cc = null, string bcc = null, bool isHTML = false, List<Attachment> attachments = null, string fromAddressDisplayName = "", string sendingApplication = "Email Service Application")
{
bool isSent = false;
try
{
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
//MailMessage emailMessage = new MailMessage(from, to);
MailMessage emailMessage = new MailMessage();
emailMessage.From = new MailAddress(from, fromAddressDisplayName);
// to address, it can be more than one
if (to != null)
{
List<string> toList = to.Trim().Split(';', ',').ToList();
foreach (string toAddress in toList)
{
if (!string.IsNullOrWhiteSpace(toAddress))
emailMessage.To.Add(new MailAddress(toAddress));
}
}
if (emailMessage.To.Count <= 0)
{
// there is no to email address specified
return isSent;
}
emailMessage.Subject = subject;
emailMessage.Body = message;
emailMessage.IsBodyHtml = isHTML;
// adding attachments
foreach (Attachment attachment in attachments ?? new List<Attachment>())
{
emailMessage.Attachments.Add(attachment);
}
// add cc list
if (cc != null)
{
List<string> ccList = cc.Trim().Split(';', ',').ToList();
foreach (string ccAddress in ccList)
{
if (!string.IsNullOrWhiteSpace(ccAddress))
emailMessage.CC.Add(ccAddress.Trim());
}
}
// add bcc list
if (bcc != null)
{
List<string> bccList = bcc.Trim().Split(';', ',').ToList();
foreach (string bccAddress in bccList)
{
if (!string.IsNullOrWhiteSpace(bccAddress))
emailMessage.Bcc.Add(bccAddress.Trim());
}
}
client.Send(emailMessage);
isSent = true;
}
catch (SmtpException smtpEx)
{
//DDAppLogger.Add("EmailService", "SendEmail", "", string.Format("SMTP error for email: {0}, Error message: {1}", to.Address, smtpEx.ToString()), -1, "SYSTEM", "UNKNOWN", true);
// if failed to email, write an entry in system event log
string log = "Error in Sending Email";
string sEvent = string.Format("Unable to send email. Subject : {0}, To Address : {1}. Details of Error: {2}", subject, to, smtpEx.ToString());
RegisterEventLog(sEvent, log, EventLogEntryType.Error, sendingApplication);
//throw smtpEx;
}
catch (Exception ex)
{
//DDAppLogger.Add("EmailService", "SendEmail", "", string.Format("Error for email: {0}, Error message: {1}", to.Address, ex.ToString()), -1, "SYSTEM", "UNKNOWN", true);
// if failed to email, write an entry in system event log
string log = "Error in Sending Email";
string sEvent = string.Format("Unable to send email. Subject : {0}, To Address : {1}. Details of Error: {2}\n\n Email Message: {3}", subject, to, ex.ToString(), message);
RegisterEventLog(sEvent, log, EventLogEntryType.Error, sendingApplication);
//throw ex;
}
return isSent;
}
public void RegisterEventLog(string strEvent, string log, EventLogEntryType logType, string source = "Email Service Application")
{
// Application source in system event log
try
{
if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, log);
}
EventLog.WriteEntry(source, strEvent, logType);
}
catch (Exception ex)
{
//DDAppLogger.Add("EmailService", "RegisterEventLog", "RegisterEventLog", ex.ToString(), -1, "UNKNOWN", "SYSTEM", true);
//throw ex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment