using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.IO;
using BazaZamowien.Classes;
using BazaZamowien.zamowieniaDataSetTableAdapters;

namespace BazaZamowien
{
    public partial class MailForm : Form
    {
        
        public MailForm()
        {
            InitializeComponent();            
        }

        private void WyślijButton_Click(object sender, EventArgs e)
        {
            //walidacja maili  

            Regex r = new Regex("^[a-zA-Z0-9_\\.\\-]+@[a-zA-Z0-9\\-]+\\.[a-zA-Z0-9\\-\\.]+$");

            if (!r.IsMatch(odEmailTextBox.Text))
            {
                MessageBox.Show("Niepoprawny adres mailowy nadawcy");
                return;
            }

            if (!r.IsMatch(doEmailTextBox.Text))
            {
                MessageBox.Show("Nieporawny adres mailowy odbiorcy");
                return;
            }

            if (!r.IsMatch(doWiadomosciTextBox.Text))
            {
                MessageBox.Show("Nieporawny adres mailowy 'do wiadomości'");
                return;
            }

            this.Cursor = Cursors.WaitCursor;

            MailAddress adresOD = new MailAddress(odEmailTextBox.Text, odTextBox.Text);
            MailAddress adresDO = new MailAddress(doEmailTextBox.Text, doComboBox.Text);
            MailMessage message = new MailMessage(adresOD, adresDO);

            message.CC.Add(doWiadomosciTextBox.Text);
            message.Subject = tematTextBox.Text;
            message.Body = trescTextBox.Text;
                       
            SmtpClient klientSMTP = new SmtpClient("poczta.ct.com.pl");
            klientSMTP.Timeout = 1000000;
            //klientSMTP.EnableSsl = false;
            //klientSMTP.Credentials = new System.Net.NetworkCredential(User.getUser().Email, User.getUser().Password);
            //MessageBox.Show(User.getUser().Email + User.getUser().Password); 

            try
            {
                klientSMTP.Send(message);
                MessageBox.Show("Wiadomość została wysłana");
                this.Close();
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.Message);
            }

            this.Cursor = Cursors.Default;
        }

        private void doComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (doComboBox.SelectedItem != null)
            {
                doEmailTextBox.Text = ((BoundItem)doComboBox.SelectedItem).StringValue;
            }
        }       

    }
}