-4

可能重复:
通过 C# ASP.NET 发送 html 电子邮件

任何人都可以为我提供发送包含html格式正文的电子邮件的示例代码吗?我对这个概念完全陌生,并且努力做到这一点。

4

1 回答 1

1

试试这个

    public void SendErrorMail(string commaSeparatedEmails, string errorDate, string pageName, string errorMessage, string errorSource, string errorInnerException, string errorData, string errorTarget, string errorStack)
    {
        try
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("yourgmailid@gmail.com", "yourPass");
            SmtpServer.EnableSsl = true;
            mail.From = new MailAddress("yourmail@gmail.com");
            mail.To.Add(functionToGetValueFromDB());

            mail.Bcc.Add("oyurbccmail@gmail.com");
            mail.Subject = "Error Details";
            string mailtable =
                "<html><table style= 'width: 352px' id='Mail' runat='server'>" +
                                          "<tr><td style='width: 100px; height: 25px'>" +
                                                  "  </td>" +
                                              "  <td style='width: 100px; height: 25px'>" +
                                                 "   </td> " +
                                          "  </tr>" +
                                           " <tr>" +
                                             "   <td style='width: 100px; height: 25px'>" +
                                                   " Error Date</td> " +
                                               " <td style='width: 100px; height: 25px'> " +
                                                  errorDate + "</td> " +
                                           " </tr> " +
                                           "  <tr>" +
                                                "<td style='width: 100px; height: 25px'>" +
                                                   "Error Page </td>" +
                                               " <td style='width: 100px; height: 25px'>" +
                                                  pageName + "</td>" +
                                           " </tr>" +
                                           "  <tr>" +
                                                "<td style='width: 100px; height: 25px'>" +
                                                   "Error Message </td>" +
                                               " <td style='width: 100px; height: 25px'>" +
                                                  errorMessage + "</td>" +
                                           " </tr>" +
                                           " <tr>" +
                                               " <td style='width: 100px; height: 25px'>" +
                                                  " Error Source</td>" +
                                              "  <td style='width: 100px; height: 25px'>" +
                                                  errorSource + "</td>" +
                                           " </tr>" +

                                            " <tr>" +
                                               " <td style='width: 100px; height: 25px'>" +
                                                  " Error InnerException</td>" +
                                              "  <td style='width: 100px; height: 25px'>" +
                                                  errorInnerException + "</td>" +
                                           " </tr>" +

                                            " <tr>" +
                                               " <td style='width: 100px; height: 25px'>" +
                                                  " Error Data</td>" +
                                              "  <td style='width: 100px; height: 25px'>" +
                                                  errorData + "</td>" +
                                           " </tr>" +

                                            " <tr>" +
                                               " <td style='width: 100px; height: 25px'>" +
                                                  " Error Target</td>" +
                                              "  <td style='width: 100px; height: 25px'>" +
                                                  errorTarget + "</td>" +
                                           " </tr>" +

                                            " <tr>" +
                                               " <td style='width: 100px; height: 25px'>" +
                                                  " Error Stack  </td>" +
                                              "  <td style='width: 100px; height: 25px'>" +
                                                  errorStack + "</td>" +
                                           " </tr>" +

                                            "<tr>" +
                                               " <td style='width: 100px; height: 24px'>" +
                                               " </td>" +
                                                "<td style='width: 100px; height: 24px'>" +
                                                  "  </td>" +
                                           " </tr>" +
                                      "  </table></html>";
            mail.IsBodyHtml = true;
            mail.Body = mailtable;
            SmtpServer.Send(mail);
        }
        catch (Exception ex)
        {
        }
    }

从数据库中获取价值的函数

public string functionToGetValueFromDB()
{
 //your sp codes here


 return to_mail;
}
于 2012-11-27T11:28:29.357 回答