using System;
using System.Net.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ExWeb
{
    public partial class Mod_Sendmail : Mod
    {
        public static string Help_Description =
            "Anzeige einer Datei in einem iframe";

        public static string[][] Help_Attributes = new string[][]
        {
            // Attribut, Wert, DefaultWert, Optional, Hilfe, Beispiel
            new string[] {"MailServer", "string", "","","Name des Mailservers", "mail.clubcomputer.at" },
            new string[] {"User","string", "","","Username (bei ClubComputer immer Mailboxadresse)", "franz.fiala@clubcomputer.at"},
            new string[] {"Pass","string","","","Passwort",""},
            new string[] {"To","string","","","Mailadresse des Empfängers","vorname.zuname@domain.at"},
            new string[] {"ToCc","string","","opt","Mailadresse der Kopie-Empfänger (durch Beistrich getrennt)",""},
            new string[] {"ToBcc","string","","opt","Mailadresse der Blindkopie-Empfänger (durch Beistrich getrennt)",""},
            new string[] {"Subject","string","","opt","Betreff",""},
            new string[] {"Message","string","","opt","Nachricht", ""},
            new string[] {"Button","string","","opt","Text auf Versendebutton", ""},
        };
        string TextToSend = "";
        string AktuelleSeite = "";
        Label lMessage;
        Panel pDialog;
        public string strMailServer = "";
        public string MailServer
        {
            set
            {
                strMailServer = value;
            }
        }
        public string strUser = "";
        public string User
        {
            set
            {
                strUser = value;
            }
        }
        public string strPass = "";
        public string Pass
        {
            set
            {
                strPass = value;
            }
        }
        public string strFrom = "";
        public string From
        {
            set
            {
                strFrom = value;
            }
        }
        public string strTo = "";
        public string To
        {
            set
            {
                strTo = value;
            }
        }
        public string strToBcc = "";
        public string ToBcc
        {
            set
            {
                strToBcc = value;
            }
        }
        public string strToCc = "";
        public string ToCc
        {
            set
            {
                strToCc = value;
            }
        }
        public string strSubject = "";
        public string Subject
        {
            set
            {
                strSubject = value;
            }
        }
        public string strMessage = "";
        public string Message
        {
            set
            {
                strMessage = value;
            }
        }
        public string strButton = "";
        public string Button
        {
            set
            {
                strButton = value;
            }
        }

        void ShowControls(ControlCollection c)
        {
            // Response.Write("<br/>" + c.Count.ToString() + "<br/>");
            for (int i = 0; i < c.Count; i++)
            {
                // Response.Write(i + ":" + c[i].GetType().ToString() + "<br/>");
                switch (c[i].GetType().ToString())
                {
                    case "System.Web.UI.WebControls.Label":
                        Label l = (Label)c[i];
                        if (l.ID == "Label_Message")
                            lMessage = l;
                        break;
                    case "System.Web.UI.WebControls.Panel":
                        Panel p = (Panel)c[i];
                        if (p.ID == "Panel_MailDialog")
                            pDialog = p;
                        break;
                    case "System.Web.UI.WebControls.TextBox":
                        System.Web.UI.WebControls.TextBox tb = (System.Web.UI.WebControls.TextBox)c[i];
                        TextToSend += "<b>" + tb.ID + ": " + tb.Text + "</b><br/>";
                        break;
                    case "System.Web.UI.WebControls.DropDownList":
                        System.Web.UI.WebControls.DropDownList ddl = (System.Web.UI.WebControls.DropDownList)c[i];
                        for (int j = 0; j < ddl.Items.Count; j++)
                        {
                            if (ddl.Items[j].Selected)
                            {
                                TextToSend += "<b>" + ddl.ID + ": " + ddl.SelectedValue + "</b><br/>";
                                break;
                            }
                        }
                        break;
                    case "System.Web.UI.WebControls.CheckBox":
                        System.Web.UI.WebControls.CheckBox cb = (System.Web.UI.WebControls.CheckBox)c[i];
                        TextToSend += "<b>" + cb.ID + ": " + cb.Checked + "</b><br/>";
                        break;
                }
                if (c[i].HasControls()) ShowControls(c[i].Controls);
            }
        }
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            SetPanelLayout(this.Controls);
        }
        protected new void Page_Load(object sender, EventArgs e)
        {
            Page.LoadComplete += new EventHandler(Page_LoadComplete);

            AktuelleSeite = Request.AppRelativeCurrentExecutionFilePath;
            ShowControls(this.Page.Controls);
            Button_Send.Text = strButton;
            if (!IsPostBack)
            {
                LoadNumbers();
            }
        }
        protected void LoadNumbers()
        {
            Random r1 = new Random();
            Random r2 = new Random();
            int z1 = r1.Next(9) + 1;
            int z2 = r1.Next(9) + 1;
            Label_ErsteZahl.Text = z1.ToString();
            Label_ZweiteZahl.Text = z2.ToString();
        }
        protected void Button_Send_Click(object sender, EventArgs e)
        {
            int z1= int.Parse(Label_ErsteZahl.Text);
            int z2= int.Parse(Label_ZweiteZahl.Text);
            int Ergebnis = -1;
            if (int.TryParse(TextBox_Ergebnis.Text, out Ergebnis))
            {
                if (Ergebnis == z1 + z2)
                    MailHelper.SendMailMessage(strMailServer, strUser, strPass, strMessage, strFrom, strTo, strToBcc, strToCc, strSubject + " " + AktuelleSeite, TextToSend, lMessage, pDialog);
            }
            else
            {
                LoadNumbers();
            }
        }
    }
    public class MailHelper
    {
        /// <summary>
        /// Sends an mail message
        /// </summary>
        /// <param name="from">Sender address</param>
        /// <param name="to">Recepient address</param>
        /// <param name="bcc">Bcc recepient</param>
        /// <param name="cc">Cc recepient</param>
        /// <param name="subject">Subject of mail message</param>
        /// <param name="body">Body of mail message</param>
        public static void SendMailMessage(string MailServer, string User, string Pass, string Response, string from, string to, string bcc, string cc, string subject, string body, Label l, Panel p)
        {
            MailMessage mMailMessage = new MailMessage();

            mMailMessage.From = new MailAddress(from);
            mMailMessage.To.Add(new MailAddress(to));
            if ((bcc != null) && (bcc != string.Empty))
            {
                mMailMessage.Bcc.Add(new MailAddress(bcc));
            }

            if ((cc != null) && (cc != string.Empty))
            {
                mMailMessage.CC.Add(new MailAddress(cc));
            }       // Set the subject of the mail message
            mMailMessage.Subject = subject;
            mMailMessage.Body = body;
            mMailMessage.IsBodyHtml = true;
            mMailMessage.Priority = MailPriority.Normal;

            SmtpClient mSmtpClient = new SmtpClient(MailServer);
            try
            {
                mSmtpClient.Credentials = new System.Net.NetworkCredential(User, Pass);
                mSmtpClient.Send(mMailMessage);
                l.Text = Response;
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                string errorMessage = string.Empty;
                while (ex2 != null)
                {
                    errorMessage += ex2.ToString();
                    ex2 = ex2.InnerException;
                }

                l.Text = errorMessage;
            }
            p.Visible = false;
        }
    }
}