Email formatting in code behind
I need to send an email to a customer that has just completed a transaction but I am having some difficulties in drafting out the format in code behind, this is my code, apparently it's appearing as one straight line of text in the body of my mail, how can I format it in such a way it appears something like, any help is appreciated, thanks :
Thank you for shopping with us. Your purchase details are as follow:
Product ID Model Number Model Name Unit Cost Quantity ItemTotal
357 P1 AK47 Rifle(free 30 * 7.62) $2.99 1 2.99
362 XT3893 Mirage stealth tank $500000.99 1 500000.99
Total:$500002.99
This is my code:
MembershipUserCollection users = Membership.GetAllUsers();
MembershipUser u = users[UserName];
MailMessage mail = new MailMessage();
mail.To.Add(u.Email);
mail.From = new MailAddress("me@hotmail.com");
mail.Subject = "Purchase invoice" + newOrder.OrderID;
string bodyText = "";
double unitQtyPrice = 0;
double totalPrice = 0;
foreach (var cItem in cartList)
{
Math.Round(cItem.UnitCost, 2);
bodyText = bodyText + '\n'
+ cItem.ProductID.ToString() + '\t'
+ cItem.ModelName + '\t'
+ cItem.ModelNumber + '\t'
+ cItem.UnitCost.ToString() + '\t'
+ cItem.Quantity.ToString() + '\t';
开发者_开发知识库 unitQtyPrice = cItem.Quantity * (double)cItem.UnitCost;
totalPrice += unitQtyPrice;
}
Math.Round(totalPrice, 2);
mail.Body = "Thank you for shopping with us. Your purchase details are as follow:"
+ '\n' + "ProductID:" + '\t' + "ModelName" + '\t' + "ModelNumber" + '\t' + "UnitPrice" + '\t' + "Quantity"
+ '\n' + bodyText + '\n' + '\n' + '\t' + '\t' + "Total Price:" + totalPrice ;
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.live.com", 587);
client.EnableSsl = true; //ssl must be enabled for hotmail
NetworkCredential credentials = new NetworkCredential("me@hotmail.com", "xxxx");
client.Credentials = credentials;
try
{
client.Send(mail);
}
catch (Exception exp)
{
throw new Exception("ERROR: Fail to send email - " + exp.Message.ToString(), exp);
}
You have it set to HTML, which is whitespace-insensitive. If you want it to be sent as plaintext with line breaks and all, don't set IsBodyHtml
.
Alternatively, you can include actual HTML markup to lay out your email, which may be a better solution.
Just write you mail.Body part in an HTML language...
and set
mail.IsBodyHtml = true;
精彩评论