代码后面的电子邮件格式

时间:2011-06-21 17:06:19

标签: c# .net asp.net

我需要向刚刚完成交易的客户发送电子邮件,但我在编写背后的代码格式时遇到一些困难,这是我的代码,显然它正在出现在正文中的一行文字中我的邮件,我怎么能以这样的方式格式化,看起来像是什么,感谢任何帮助,谢谢:

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

这是我的代码:

                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);
                }

2 个答案:

答案 0 :(得分:4)

您将其设置为HTML,这对空格不敏感。如果您希望将其作为带有换行符的明文发送,请不要设置IsBodyHtml

或者,您可以包含实际的HTML标记来布置您的电子邮件,这可能是更好的解决方案。

答案 1 :(得分:1)

只需给你发邮件。用HTML语言写一部分......

并设置

mail.IsBodyHtml = true;