如何在asp.net中的sp_send_dbmail中使用HTML格式

时间:2015-02-03 19:29:19

标签: c# asp.net sp-send-dbmail

我正在尝试向某些用户发送电子邮件,但我想使用html格式。我失去了所有的新线条,一切都显示为一条线。这是我目前的代码:

   string body = "Hello " + fullName + Environment.NewLine + ",";
                body += "Your registration  has been approved. Here is your user name and password " + Environment.NewLine ;
                body += "User Name: " + usrname + Environment.NewLine;
                body += "Password: " + pass + Environment.NewLine;
                body += "Start Date: " + startDate + Environment.NewLine;
                body += "End Date: " + EndDate + Environment.NewLine;

                body += "Thanks";
                string toAddr = email.ToString();
                string mailMsg = body;

                string subject = "Registration Approved";
                string profile = "myProf";

                SqlConnection execMail = new SqlConnection("Data Source=XXXXXXXXX;Initial Catalog=msdb;Persist Security Info=True;User ID=xxxxxxxxxx;Password=xxxxxxxxxxx");
                execMail.Open();

                SqlCommand exec_cmd = new SqlCommand("sp_send_dbmail");
                exec_cmd.CommandType = CommandType.StoredProcedure;
                exec_cmd.Connection = execMail;

                exec_cmd.Parameters.AddWithValue("profile_name", profile);
                exec_cmd.Parameters.AddWithValue("recipients", toAddr);
                // exec_cmd.Parameters.AddWithValue("copy_recipients", ccAddr);
                // exec_cmd.Parameters.AddWithValue("blind_copy_recipients", bccAddr);
                exec_cmd.Parameters.AddWithValue("body", mailMsg);
                exec_cmd.Parameters.AddWithValue("subject", subject);

                exec_cmd.ExecuteNonQuery();

                exec_cmd.Dispose();
                execMail.Close();
                con.Close();
                con.Dispose();

1 个答案:

答案 0 :(得分:1)

你需要在你的字符串体中放置html标签,例如:

string body = "<h1>Hello" + fullName + Environment.NewLine + "</h1>";
                body += "<p>Your registration  has been approved. Here is your user name and password " + Environment.NewLine + "</p>";
                body += "<h3>User Name: " + usrname + Environment.NewLine+"</h3>";
                body += "<h3>Password: " + pass + Environment.NewLine+"</h3>";
                body += "<h3>Start Date: " + startDate + Environment.NewLine+"</h3>";
                body += "<h3>End Date: " + EndDate + Environment.NewLine+"</h3>";

                body += "<p>Thanks</p>";

然后,在参数中,您需要添加:

exec_cmd.Parameters.AddWithValue("body_format", "HTML");

默认值为TEXT