HTML电子邮件格式问题

HTML电子邮件格式问题

问题描述:

你好,

我有一种将HTML格式的电子邮件发送给收件人的方法.我一直在尝试以适当的方式发送它,但是我得到的只是与正文格式相同的书面内容.它没有填充字段.代码是这样的:

Hello,

I have a method that sends email in a HTML format to the recipient. I have been trying to send it in a proper way but all I get is the same written things which I wrote on the body format. It doesn''t fill the fields. The code is like that:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.IsBodyHtml = true;
                string body = "<html><body>";

                body += @"<table border='1' cellspacing='0' cellpadding='0'>
                          <tr>
                            <td width='160' height='160'><a href='{0}'><img src='{1}' width='160' height='160' border='0px' /></a></td>
                            <td style='padding-left:10px'><table width='432' border='0' cellspacing='0' cellpadding='0'>
                              <tr>
                                <td><a style='font-size:18px; font-weight:bold'>ASIN: {2}</a></td>
                              </tr>
                              <tr>
                                <td><a style='color:#000; font-size:18px; font-weight:bold' href='{0}'>link</a></td>
                              </tr>
                              <tr>
                                <td>Review average has been changed from {3} to {4}.</td>
                              </tr>
                              <tr>
                                <td>Total review has been changed from {5} to {6}.</td>
                              </tr>
                              <tr>
                                <td>Price: {7}</td>
                              </tr>
                              <tr>
                                <td>Marketplace: {8}</td>
                              </tr>
                              <tr>
                                <td height='21'>Manufacturer: {9}</td>
                              </tr>
                            </table></td>
                          </tr>
                        </table>";

                message.Body = String.Format(body, detailpageurl, imageurl, asin,  String.Format("{0:0.#}", old_rating),  String.Format("{0:0.#}", rating), oldtotalreview, total_review, price, mercname, manufacturer);
                body += "</body></html>";
                message.Body = body;
                message.Subject = "Product Review";


我收到的电子邮件是:


And the email that I have received is :

ASIN: {2}
link

Review average has been changed from {3} to {4}.
Total review has been changed from {5} to {6}.
Price: {7}
Marketplace: {8}
Manufacturer: {9}



有人可以帮我找到问题吗?



can someone help me find the problem?

您的编码有误.
您正在将格式化的文本分配给message.Body.
执行分配后,body变量仍包含未格式化的文本.但是稍后,您再次将未格式化的body变量分配给message.Body

如果您更换它,
There is an error in your coding.
You are assigning the formatted text to message.Body.
After the assignment is executed the body variable still contains the unformatted text. But later on you are again assigning the unformatted body variable to the message.Body

If you replace this,
message.Body = String.Format(body, detailpageurl, imageurl, asin, String.Format("{0:0.#}", old_rating), String.Format("{0:0.#}", rating), oldtotalreview, total_review, price, mercname, manufacturer); 



与此,



with this,

body = String.Format(body, detailpageurl, imageurl, asin, String.Format("{0:0.#}", old_rating), String.Format("{0:0.#}", rating), oldtotalreview, total_review, price, mercname, manufacturer); 


它应该可以正常工作.


It should work fine.