Argentini.Halide.H3Email.BuildHtmlEmail C# (CSharp) Method

BuildHtmlEmail() public static method

Build an HTML e-mail body from a container's form field objects.

Works with the following controls: TextBox, RadioButtonList, CheckBoxList, DropDownList, ListBox, HiddenField. Checkboxlists return comma-separated lists of values.

using Argentini.Halide; ... string emailBody = H3Email.BuildHtmlEmail("ContainerDiv1"); string result = H3Email.Send( "[email protected]", "[email protected],[email protected]", "Message subject here", emailBody, H3Email.MailFormat.Html);
public static BuildHtmlEmail ( Control containerControl ) : String
containerControl System.Web.UI.Control Container control whose children are to be evaluated and used.
return String
        public static String BuildHtmlEmail(Control containerControl)
        {
            StringBuilder mailBody = new StringBuilder("<html><body>\r\n<table border=\"0\" cellpadding=\"7\" cellspacing=\"0\" width=\"670\">\r\n");

            foreach (Control c in containerControl.Controls)
            {
                try
                {
                    switch (c.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.TextBox":

                            TextBox tb = (TextBox)c;
                            mailBody.Append("<tr><td align=\"left\" valign=\"top\"><strong>" + c.ID.Replace("_", " ") + "</strong></td><td align=\"left\" valign=\"top\">" + tb.Text + "</td></tr>\r\n");
                            break;

                        case "System.Web.UI.WebControls.RadioButtonList":

                            RadioButtonList rbl = (RadioButtonList)c;
                            mailBody.Append("<tr><td align=\"left\" valign=\"top\"><strong>" + c.ID.Replace("_", " ") + "</strong></td><td align=\"left\" valign=\"top\">" + rbl.SelectedValue + "</td></tr>\r\n");
                            break;

                        case "System.Web.UI.WebControls.CheckBoxList":

                            CheckBoxList cbl = (CheckBoxList)c;

                            mailBody.Append("<tr><td align=\"left\" valign=\"top\"><strong>" + c.ID.Replace("_", " ") + "</strong></td><td align=\"left\" valign=\"top\">");

                            Boolean multiple = false;

                            for (int x = 0; x < cbl.Items.Count; x++)
                            {
                                ListItem li = cbl.Items[x];

                                if (li.Selected)
                                {
                                    mailBody.Append((multiple ? "," : "") + li.Value);
                                    multiple = true;
                                }
                            }

                            mailBody.Append("</td></tr>\r\n");

                            break;

                        case "System.Web.UI.WebControls.DropDownList":

                            DropDownList ddl = (DropDownList)c;
                            mailBody.Append("<tr><td align=\"left\" valign=\"top\"><strong>" + c.ID.Replace("_", " ") + "</strong></td><td align=\"left\" valign=\"top\">" + ddl.SelectedValue + "</td></tr>\r\n");
                            break;

                        case "System.Web.UI.WebControls.ListBox":

                            ListBox lb = (ListBox)c;

                            mailBody.Append("<tr><td align=\"left\" valign=\"top\"><strong>" + c.ID.Replace("_", " ") + "</strong></td><td align=\"left\" valign=\"top\">");

                            Boolean multiple2 = false;

                            for (int x = 0; x < lb.Items.Count; x++)
                            {
                                ListItem li = lb.Items[x];

                                if (li.Selected)
                                {
                                    mailBody.Append((multiple2 ? "," : "") + li.Value);
                                    multiple2 = true;
                                }
                            }

                            mailBody.Append("</td></tr>\r\n");

                            break;

                        case "System.Web.UI.WebControls.HiddenField":

                            HiddenField hf = (HiddenField)c;
                            mailBody.Append("<tr><td align=\"left\" valign=\"top\"><strong>" + c.ID.Replace("_", " ") + "</strong></td><td align=\"left\" valign=\"top\">" + hf.Value + "</td></tr>\r\n");
                            break;
                    }
                }

                catch
                { }
            }

            mailBody.Append("</table>\r\n</body></html>");

            return mailBody.ToString();
        }