代码隐藏中的电子邮件格式

本文关键字:电子邮件 格式 隐藏 代码 | 更新日期: 2023-09-27 17:59:43

我需要给一个刚刚完成交易的客户发送一封电子邮件,但我在代码后面起草格式时遇到了一些困难,这是我的代码,显然它在我的邮件正文中显示为一条直线文本,我该如何将其格式化,使其看起来像这样,非常感谢您的帮助,谢谢:

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

代码隐藏中的电子邮件格式

您已经将其设置为HTML,这是不区分空白的。如果您希望它以带换行符的明文形式发送,请不要设置IsBodyHtml

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

只需给您写邮件即可。HTML语言中的正文部分。。。

并设置

mail.IsBodyHtml = true;