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.Mail;
using System.IO;
namespace AnOnyMaiL
{
public partial class frmAnOnyMaiL : Form
{
string strPathAllegato;
public frmAnOnyMaiL()
{
InitializeComponent();
}
private void cmdChiudi_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void cmdCancella_Click(object sender, EventArgs e)
{
Reset();
}
private void cmdInvia_Click(object sender, EventArgs e)
{
// Configura
MailAddress strDa
= new MailAddress
(txtDa.
Text, txtAliasDa.
Text);
MailAddress strA
= new MailAddress
(txtA.
Text, txtAliasA.
Text);
MailMessage eMsg
= new MailMessage
(strDa, strA
);
eMsg.Subject = txtOggetto.Text;
eMsg.Body = txtTesto.Text;
// Allega file se esiste
if (strPathAllegato != null)
{
Attachment allegato
= new Attachment
(strPathAllegato
);
eMsg.Attachments.Add(allegato);
}
SmtpClient eClient
= new SmtpClient
(txtSmtp.
Text);
try
{
// Invia E.mail
eClient.Send(eMsg);
MessageBox.Show("E.mail inviata con successo. ", "AnOnyMaiL", MessageBoxButtons.OK, MessageBoxIcon.Information);
strPathAllegato = null;
lblAllegati.Text = "Allegati: 0";
}
catch (Exception exError)
{
MessageBox.Show(exError.ToString());
}
}
private void cmdAllega_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
// Controlla che l'estensione del file sia .txt o .zip
if ((openFileDialog1.FileName.EndsWith(".txt")) || openFileDialog1.FileName.EndsWith(".zip"))
{
// Se l'estensione è .txt o .zip memorizza il percorso dell'allegato
strPathAllegato = Path.GetFullPath(openFileDialog1.FileName);
// Aggiorna la label
lblAllegati.Text = "Allegati: 1";
}
else
{
MessageBox.Show("Puoi allegare solo un file .zip o .txt ", "AnOnyMaiL", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void Reset()
{
// Resetta tutte le TextBox e nulla l'allegato
txtDa.Text = "";
txtAliasDa.Text = "";
txtA.Text = "";
txtAliasA.Text = "";
txtSmtp.Text = "";
txtOggetto.Text = "";
txtTesto.Text = "";
lblAllegati.Text = "Allegati: 0";
strPathAllegato = null;
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
// Se è minimizzato, ripristinalo
if (FormWindowState.Minimized == WindowState)
{
Show();
WindowState = FormWindowState.Normal;
}
}
private void frmAnOnyMaiL_Resize(object sender, EventArgs e)
{
// Se il resize minimizza, nascondi
if (FormWindowState.Minimized == WindowState)
{
Hide();
}
}
}
}