View Single Post
Old 18-12-2007, 18:17   #4
0rph3n
Senior Member
 
L'Avatar di 0rph3n
 
Iscritto dal: Apr 2005
Città: Resana - TV
Messaggi: 960
queste sono i metodi interessati, il client invoca il metodo public void Send(int nCodSync, String nomeMittente, String indirizzoMittente)
come potrete notare è codice scritto no velocemente...un po' di più, ed è il frutto di aggiunte e sopraaggiunte (il classico modo dell'azienda in cui lavoro)

Codice:
public void Send(int nCodSync, String nomeMittente, String indirizzoMittente)
{
	Message message = new Message();

	message.SenderName = nomeMittente;
	message.SenderAddress = indirizzoMittente;

	OleDbConnection conn = null;
	OleDbCommand cmd = null;

	int nCodPratica = -1;

	try
	{
		conn = new OleDbConnection(this.connectionString);
		conn.Open();
	}
	catch (Exception ex)
	{
		throw new ApplicationException("Si è verificato un problema nell'accesso al DataBase.\n\n" + ex.Message);
	}

	try
	{
		cmd = conn.CreateCommand();

		String query = "SELECT cNomeFile, cPathDestinazione, nCodMessaggio, nCodPratica FROM ATTIVITA_SYNC WHERE nCodSync = " + nCodSync.ToString();
		cmd.CommandText = query;
		OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
		System.Data.DataTable table = new System.Data.DataTable();
		adapter.Fill(table);

		int nCodMessaggio = -1;
		nCodPratica = -1;

		if (table.Rows.Count > 0)
		{
			if (!table.Rows[0].IsNull("nCodMessaggio"))
			{
				nCodMessaggio = System.Convert.ToInt32(table.Rows[0]["nCodMessaggio"]);
			}
			else
			{
				throw new ApplicationException("Campo nCodMessaggio nella tabella ATTIVITA_SYNC non valorizzato.");
			}

			if (!table.Rows[0].IsNull("nCodPratica"))
			{
				nCodPratica = System.Convert.ToInt32(table.Rows[0]["nCodPratica"]);
			}
			else
			{
				throw new ApplicationException("Campo nCodPratica nella tabella ATTIVITA_SYNC non valorizzato.");
			}

			if (!table.Rows[0].IsNull("cNomeFile") && !table.Rows[0].IsNull("cPathDestinazione"))
			{
				message.AttachmentFilePath = table.Rows[0]["cPathDestinazione"].ToString() + table.Rows[0]["cNomeFile"].ToString() + ".pdf";
			}
			else
			{
				throw new ApplicationException("Campo cNomeFile o cPathDestinazione nella tabella ATTIVITA_SYNC non valorizzato.");
			}
		}
		else
		{
			throw new ApplicationException("Non è stato trovato alcun record con nCodSync = " + nCodSync.ToString() + " nella tabella ATTIVITA_SYNC.");
		}
		table.Dispose();
		table = null;

		query = "SELECT cDesOggetto, cDesCorpo FROM EMAIL_MESSAGGI WHERE bCancellato = 0 AND nCodMessaggio = " + nCodMessaggio.ToString();
		cmd.CommandText = query;
		table = new System.Data.DataTable();
		adapter.Fill(table);

		if (table.Rows.Count > 0)
		{
			if (!table.Rows[0].IsNull("cDesOggetto"))
			{
				message.Subject = table.Rows[0]["cDesOggetto"].ToString();
			}
			else
			{
				throw new ApplicationException("Il campo cDesOggetto della tabella EMAIL_MESSAGGI per il record con nCodMessaggio = " + nCodMessaggio.ToString() + " non è valorizzato.");
			}

			if (!table.Rows[0].IsNull("cDesCorpo"))
			{
				message.Body = table.Rows[0]["cDesCorpo"].ToString();
			}
			else
			{
				throw new ApplicationException("Il campo cDesCorpo della tabella EMAIL_MESSAGGI per il record con nCodMessaggio = " + nCodMessaggio.ToString() + " non è valorizzato.");
			}
		}
		else
		{
			throw new ApplicationException("Non è stato trovato alcun record con nCodMessaggio = " + nCodMessaggio.ToString() + " nella tabella EMAIL_MESSAGGI.");
		}
		table.Dispose();
		table = null;

		query = "SELECT cNome, cEmail FROM EMAIL_INDIRIZZI WHERE nCodIndirizzo = 1";
		cmd.CommandText = query;
		table = new System.Data.DataTable();
		adapter.Fill(table);

		if (table.Rows.Count > 0)
		{
			if (!table.Rows[0].IsNull("cNome"))
			{
				message.RecipientName = table.Rows[0]["cNome"].ToString();
			}
			else
			{
				throw new ApplicationException("Il campo cNome della tabella EMAIL_INDIRIZZI per il record con nCodIndirizzo = 1 non è valorizzato.");
			}

			if (!table.Rows[0].IsNull("cEmail"))
			{
				message.RecipientAddress = table.Rows[0]["cEmail"].ToString();
			}
			else
			{
				throw new ApplicationException("Il campo cEmail della tabella EMAIL_INDIRIZZI per il record con nCodIndirizzo = 1 non è valorizzato.");
			}
		}
		else
		{
			throw new ApplicationException("Non è stato trovato alcun record con nCodIndirizzo = 1 nella tabella EMAIL_INDIRIZZI.");
		}
		table.Dispose();
		table = null;
	}
	catch (Exception ex)
	{
		throw new ApplicationException("Si è verificato un'errore durante il recupero dei dati.\n\n" + ex.Message);
	}
	finally
	{
		if (cmd != null)
			cmd.Dispose();

		if (conn != null)
		{
			conn.Close();
			conn.Dispose();
		}
	}
		
	this.Send(message, nCodPratica);
}

public void Send(Message message, int nCodPratica)
{
	MailMessage mailMessage = new MailMessage();
	
	if (message.SenderAddress != null && !message.SenderAddress.Equals(string.Empty))
		mailMessage.From = message.SenderAddress;
	else
	{
		throw new ApplicationException("L'indirizzo email del mittente non è stato specificato.");
	}

	if (message.RecipientAddress != null && !message.RecipientAddress.Equals(string.Empty))
	{
		if (message.RecipientName != null && !message.RecipientName.Equals(string.Empty))
			mailMessage.To = "\"" + message.RecipientName + "\" <" + message.RecipientAddress + ">";
		else
			mailMessage.To = message.RecipientAddress;
	}
	else
	{
		throw new ApplicationException("L'indirizzo email del destinatario non è stato specificato.");
	}

	mailMessage.Subject = message.Subject;
	mailMessage.Body = message.Body;

	switch (message.Format)
	{
		case EMailFormat.Text:
			mailMessage.BodyFormat = System.Web.Mail.MailFormat.Text;
			break;
		case EMailFormat.HTML:
			mailMessage.BodyFormat = System.Web.Mail.MailFormat.Html;
			break;
	}

	switch (message.Encoding)
	{
		case EMailEncoding.ASCII:
			mailMessage.BodyEncoding = System.Text.Encoding.ASCII;
			break;
		case EMailEncoding.BigEndianUnicode:
			mailMessage.BodyEncoding = System.Text.Encoding.BigEndianUnicode;
			break;
		case EMailEncoding.Default:
			mailMessage.BodyEncoding = System.Text.Encoding.Default;
			break;
		case EMailEncoding.Unicode:
			mailMessage.BodyEncoding = System.Text.Encoding.Unicode;
			break;
		case EMailEncoding.UTF7:
			mailMessage.BodyEncoding = System.Text.Encoding.UTF7;
			break;
		case EMailEncoding.UTF8:
			mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
			break;
	}

	if (message.AttachmentFilePath != null && !message.AttachmentFilePath.Equals(string.Empty))
	{
		if (System.IO.File.Exists(message.AttachmentFilePath))
		{
			System.Web.Mail.MailEncoding attachmentEncoding = System.Web.Mail.MailEncoding.Base64;
			switch (message.AttachmentEncoding)
			{
				case AttachmentFileEncoding.Base64:
					attachmentEncoding = System.Web.Mail.MailEncoding.Base64;
					break;
				case AttachmentFileEncoding.UUEncode:
					attachmentEncoding = System.Web.Mail.MailEncoding.UUEncode;
					break;
			}
			mailMessage.Attachments.Add(new MailAttachment(message.AttachmentFilePath, attachmentEncoding));
		}
		else
		{
			throw new ApplicationException("File da allegare non trovato.");
		}
	}

	if (this.UserName != null && !this.UserName.Equals(string.Empty))
	{
		// Imposta l'autenticazione di base
		mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
		// Imposta l'username con il quale connettersi al server smtp
		mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", this.UserName);
		// Imposta la password con la quale connettersi al server smtp
		mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", this.Password);
	}

	if (this.SMTPServer != null && !this.SMTPServer.Equals(string.Empty))
	{
		SmtpMail.SmtpServer = this.SMTPServer;
	}
	else
	{
		throw new ApplicationException("Indirizzo o nome del server SMTP non impostato.");
	}

	// Prova a spedire la mail
	try
	{
		SmtpMail.Send(mailMessage);
	}
	catch (Exception ex)
	{
		throw new ApplicationException("Si è verificato un'errore durante l'invio del messaggio.\n\n" + ex.Message);
	}

	this.NotifyEmailDelivery(nCodPratica, message.RecipientName);
}

private void NotifyEmailDelivery(int nCodPratica, String nomeDestinatario)
{
	OleDbConnection conn = null;
	OleDbTransaction transaction = null;
	OleDbCommand cmd = null;
	String query = String.Empty;
	try
	{
		conn = new OleDbConnection(this.connectionString);
		conn.Open();
	}
	catch (Exception ex)
	{
		throw new ApplicationException("Si è verificato un problema nell'accesso al DataBase.\n\n" + ex.Message);
	}

	try
	{
		transaction = conn.BeginTransaction();
	}
	catch (Exception ex)
	{
		throw new ApplicationException("Si è verificato un problema nell'inizializzazione della transazione.\n\n" + ex.Message);
	}

	try
	{
		cmd = conn.CreateCommand();
		cmd.Transaction = transaction;

		query = "UPDATE PRATICA_DATI SET bInvioDittaIncaricata = 1 WHERE nCodPratica = " + nCodPratica.ToString();
		cmd.CommandText = query;
		cmd.ExecuteNonQuery();

		int totInterventi = -1;
		query = "SELECT COUNT(nCodPratica) AS totInterventi FROM PRATICA_INTERVENTI WHERE nCodPratica = " + nCodPratica.ToString() + " AND bCancellazione = 0 AND bIdentificativo = 0";
		cmd.CommandText = query;
		totInterventi = System.Convert.ToInt32(cmd.ExecuteScalar());

		if (totInterventi == 0)
		{
			query = "INSERT INTO PRATICA_INTERVENTI (nCodIntervento, nCodPratica, cDesPersonale, dDataTrasmissione, bCancellazione, bIdentificativo)" +
				" SELECT CASE WHEN MAX(nCodIntervento) IS NULL THEN 1 ELSE MAX(nCodIntervento) + 1 END," +
				nCodPratica + "," +
				" '" + nomeDestinatario + "'," +
				" GETDATE()," +
				" 0," +
				" 0" +
				" FROM PRATICA_INTERVENTI";
		}
		else
		{
			query = "SELECT dDataTrasmissione FROM PRATICA_INTERVENTI WHERE nCodPratica = " + nCodPratica.ToString() + " AND bCancellazione = 0 AND bIdentificativo = 0 AND cDesPersonale = '" + nomeDestinatario + "'";
			cmd.CommandText = query;
			OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
			System.Data.DataTable tabellaDataTrasmissione = new System.Data.DataTable();
			adapter.Fill(tabellaDataTrasmissione);

			if (tabellaDataTrasmissione.Rows.Count == 0 || tabellaDataTrasmissione.Rows[0].IsNull("dDataTrasmissione"))
			{
				query = "UPDATE PRATICA_INTERVENTI SET cDesPersonale = '" + nomeDestinatario + "', dDataTrasmissione = GETDATE(), dOraInizio = NULL, dOraFine = NULL, bCancellazione = 0" +
						" WHERE nCodIntervento = (SELECT TOP 1 nCodIntervento FROM PRATICA_INTERVENTI WHERE nCodPratica = " + nCodPratica.ToString() + " AND bCancellazione = 0 AND bIdentificativo = 0)";
			}
			else
			{
				DateTime dataTrasmissione;
				dataTrasmissione = System.Convert.ToDateTime(tabellaDataTrasmissione.Rows[0]["dDataTrasmissione"]);
				if (dataTrasmissione.ToString("dd/mm/yyyy").Equals("01/01/1900"))
				{
					query = "UPDATE PRATICA_INTERVENTI SET cDesPersonale = '" + nomeDestinatario + "', dDataTrasmissione = GETDATE(), dOraInizio = NULL, dOraFine = NULL, bCancellazione = 0" +
							" WHERE nCodIntervento = (SELECT TOP 1 nCodIntervento FROM PRATICA_INTERVENTI WHERE nCodPratica = " + nCodPratica.ToString() + " AND bCancellazione = 0 AND bIdentificativo = 0)";
				}
				else
				{
					query = "UPDATE PRATICA_INTERVENTI SET cDesPersonale = '" + nomeDestinatario + "', dOraInizio = NULL, dOraFine = NULL, bCancellazione = 0" +
							" WHERE nCodIntervento = (SELECT TOP 1 nCodIntervento FROM PRATICA_INTERVENTI WHERE nCodPratica = " + nCodPratica.ToString() + " AND bCancellazione = 0 AND bIdentificativo = 0)";
				}
			}
			tabellaDataTrasmissione.Dispose();
			tabellaDataTrasmissione = null;
		}

		cmd.CommandText = query;
		cmd.ExecuteNonQuery();

		transaction.Commit();
	}
	catch (Exception ex)
	{
		if (transaction != null)
			transaction.Rollback();

		throw new ApplicationException("Si è verificato un'errore durante l'aggiornamento dei dati sul database.\n\n" + ex.Message);
	}
	finally
	{
		if (cmd != null)
			cmd.Dispose();

		if (conn != null)
		{
			conn.Close();
			conn.Dispose();
		}
	}
}
comunque questo codice, nonostante la qualità funziona.

...vado a mangiare va...
0rph3n è offline   Rispondi citando il messaggio o parte di esso