using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
using System.IO;
using System.Collections;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SmtpClient klient = null;
        MailMessage wiadomosc = null;
        ArrayList tablicaZalacznikow = new ArrayList();
        try
        {
            wiadomosc = new MailMessage(TextBox1.Text, TextBox2.Text);
            wiadomosc.Subject = TextBox3.Text;
            wiadomosc.Body = TextBox4.Text;
            klient = new SmtpClient(TextBox5.Text);
            klient.Credentials = CredentialCache.DefaultNetworkCredentials;
            
            for (int i = 0; i<ListBox1.Items.Count; i++)
            {
                Attachment zalocznik = new Attachment(Server.MapPath("~/") + ListBox1.Items[i].ToString());
                wiadomosc.Attachments.Add(zalocznik);
                tablicaZalacznikow.Add(zalocznik);

            }
            Label1.Text = "Wiadomość wysłana";
            klient.Send(wiadomosc);
            for (int j = 0; j < tablicaZalacznikow.Count; j++)
                ((Attachment)tablicaZalacznikow[j]).Dispose();
            for (int i = 0; i < ListBox1.Items.Count; i++)
                File.Delete(Server.MapPath("~/") + ListBox1.Items[i].ToString());
            ListBox1.Items.Clear();
       
        }
        catch(Exception ex)
        {
            Label1.Text = "Nie można wysłać wiadomości (" + ex.Message + ")";
        }

           
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string nazwaPliku = FileUpload1.FileName;
            string scierzkaNaSerwerze = Server.MapPath("~/")+ nazwaPliku;
            FileUpload1.SaveAs(scierzkaNaSerwerze);
            ListBox1.Items.Add(nazwaPliku);
            Label2.Text = "Załącznik pobrany";
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        ListBox1.Items.Clear();
    }
}
