Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
AnOnyMaiL - Form1.cs

Form1.cs

Caricato da:
Scarica il programma completo

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net.Mail;
  9. using System.IO;
  10.  
  11. namespace AnOnyMaiL
  12. {
  13.     public partial class frmAnOnyMaiL : Form
  14.     {
  15.         string strPathAllegato;
  16.         public frmAnOnyMaiL()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void cmdChiudi_Click(object sender, EventArgs e)
  22.         {
  23.             Application.Exit();
  24.         }
  25.  
  26.         private void cmdCancella_Click(object sender, EventArgs e)
  27.         {
  28.             Reset();
  29.         }
  30.  
  31.         private void cmdInvia_Click(object sender, EventArgs e)
  32.         {
  33.             // Configura
  34.             MailAddress strDa = new MailAddress(txtDa.Text, txtAliasDa.Text);
  35.             MailAddress strA = new MailAddress(txtA.Text, txtAliasA.Text);
  36.             MailMessage eMsg = new MailMessage(strDa, strA);
  37.             eMsg.Subject = txtOggetto.Text;
  38.             eMsg.Body = txtTesto.Text;
  39.  
  40.             // Allega file se esiste
  41.             if (strPathAllegato != null)
  42.             {
  43.                 Attachment allegato = new Attachment(strPathAllegato);
  44.                 eMsg.Attachments.Add(allegato);
  45.             }
  46.  
  47.  
  48.             SmtpClient eClient = new SmtpClient(txtSmtp.Text);
  49.             try
  50.             {
  51.                 // Invia E.mail
  52.                 eClient.Send(eMsg);
  53.                 MessageBox.Show("E.mail inviata con successo.    ", "AnOnyMaiL", MessageBoxButtons.OK, MessageBoxIcon.Information);
  54.                 strPathAllegato = null;
  55.                 lblAllegati.Text = "Allegati: 0";
  56.             }
  57.             catch (Exception exError)
  58.             {
  59.                 MessageBox.Show(exError.ToString());
  60.             }
  61.         }
  62.  
  63.         private void cmdAllega_Click(object sender, EventArgs e)
  64.         {
  65.             openFileDialog1.ShowDialog();
  66.         }
  67.  
  68.         private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
  69.         {
  70.             // Controlla che l'estensione del file sia .txt o .zip
  71.             if ((openFileDialog1.FileName.EndsWith(".txt")) || openFileDialog1.FileName.EndsWith(".zip"))
  72.             {
  73.                 // Se l'estensione è .txt o .zip memorizza il percorso dell'allegato
  74.                 strPathAllegato = Path.GetFullPath(openFileDialog1.FileName);
  75.                
  76.                 // Aggiorna la label
  77.                 lblAllegati.Text = "Allegati: 1";
  78.             }
  79.             else
  80.             {
  81.                 MessageBox.Show("Puoi allegare solo un file .zip o .txt   ", "AnOnyMaiL", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  82.             }
  83.         }
  84.  
  85.         private void Reset()
  86.         {
  87.             // Resetta tutte le TextBox e nulla l'allegato
  88.             txtDa.Text = "";
  89.             txtAliasDa.Text = "";
  90.             txtA.Text = "";
  91.             txtAliasA.Text = "";
  92.             txtSmtp.Text = "";
  93.             txtOggetto.Text = "";
  94.             txtTesto.Text = "";
  95.             lblAllegati.Text = "Allegati: 0";
  96.             strPathAllegato = null;
  97.         }
  98.  
  99.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  100.         {
  101.             // Se è minimizzato, ripristinalo
  102.             if (FormWindowState.Minimized == WindowState)
  103.             {
  104.                 Show();
  105.                 WindowState = FormWindowState.Normal;
  106.             }
  107.         }
  108.  
  109.         private void frmAnOnyMaiL_Resize(object sender, EventArgs e)
  110.         {
  111.             // Se il resize minimizza, nascondi
  112.             if (FormWindowState.Minimized == WindowState)
  113.             {
  114.                 Hide();
  115.             }
  116.         }
  117.     }
  118. }