2012-11-27 59 views
-3

可能重複:
send html email via C# ASP.NET如何發送預定的電子郵件?

誰能爲我提供的示例代碼發送包含體以HTML格式的電子郵件?我對這個概念是完全陌生的,並努力做到這一點。

+0

這個問題已經被問,並回答了很多次的堆棧溢出,例如http://stackoverflow.com/questions/1329922/send-html-email-via-c-sharp-asp-net – jeroenh

+1

如果您想按計劃發送,請編寫程序發送一個,然後使用Windows任務計劃程序在需要的時間間隔內調用它。 – Bridge

+0

我可以安排在SQL調度。但問題是調度後,我需要一個腳本來執行,我需要作爲C#腳本。我需要一個工作的例子。請幫忙。 – user1613212

回答

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("[email protected]", "yourPass"); 
      SmtpServer.EnableSsl = true; 
      mail.From = new MailAddress("[email protected]"); 
      mail.To.Add(functionToGetValueFromDB()); 

      mail.Bcc.Add("[email protected]"); 
      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) 
     { 
     } 
    } 

函數來獲取值從DB

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


return to_mail; 
} 
+0

你可以幫我綁定sp的「to_email」,這樣我應該怎麼寫html內容。請幫助。 – user1613212

+0

只需通過名爲'commaSeparatedEmails'的參數傳遞to_mail,根據您的要求刪除不需要的參數和不需要的主體。 – sajanyamaha

+0

對不安的朋友感到難過。我還需要從sp的html內容中獲取值。所以我應該在哪裏通過存儲過程。 – user1613212