-4

Possible Duplicate:
send html email via C# ASP.NET

Can anyone provide me with sample code for sending email which contains body in html format? I am totally new to this concept and struggling to do so.

Community
  • 1
  • 1
user1613212
  • 93
  • 1
  • 8
  • this question has been asked and answered many times on Stack overflow, e.g. http://stackoverflow.com/questions/1329922/send-html-email-via-c-sharp-asp-net – jeroenh Nov 27 '12 at 11:14
  • 1
    If you want to send on a schedule, write the program to send one, and use Windows task scheduler to call it at the interval you require. – Bridge Nov 27 '12 at 11:17
  • i can schedule in sql scheduler. but the problem is after scheduling i need a script to be executed in which i need as c# script. i need a working example of that. Please help. – user1613212 Nov 27 '12 at 11:20

1 Answers1

1

Try this

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

function To Get Value From DB

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


 return to_mail;
}
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44
  • can u help me to bind the "to_email" from sp so that how should i write the html content. pls help. – user1613212 Nov 27 '12 at 11:32
  • just pass to_mail through parameter called 'commaSeparatedEmails',Remove the unwanted parameters and unwanted body as per your requirement. – sajanyamaha Nov 27 '12 at 11:37
  • sorry for disturbing friend. i also need to get the value inside the html content from sp. so where should i pass the stored procedure here. – user1613212 Nov 27 '12 at 12:09
  • 1
    Add "mail.To.Add(functionToGetValueFromDB());" and also add the below function.Write your sp code there. – sajanyamaha Nov 27 '12 at 12:19