/// <summary> /// Sends email based on the properties stored in InputParameters /// </summary> /// <param name="success">true if successfull, false if not</param> /// <param name="Parameters">Holds the needed properties</param> /// <param name="messages">Appends these messages into the email body</param> public static void Send(bool success, InputParameters Parameters, StringBuilder messages) { string message = ""; try { string HostServer = Parameters.EmailHostServer; int HostPort = Parameters.EmailHostPort; string from_email = Parameters.EmailFrom; string from_password = Parameters.EmailFromPassword; string subject = Parameters.EmailSubject + " | " + (Parameters.UsedForDownloading.Equals("TRUE") ? "Downloading" : "Uploading") + " : " + (success ? "Success" : "Failed / Error"); string recipient = Parameters.EmailRecipient; StringBuilder body = new StringBuilder((Parameters.UsedForDownloading.Equals("TRUE") ? "Downloading" : "Uploading") + " Process. "); body.AppendLine(" "); body.Append(messages); SmtpClient client = new SmtpClient { Host = HostServer, Port = HostPort, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, Credentials = new NetworkCredential(from_email, from_password), Timeout = 20000 }; // construct mail message and send it MailMessage mm = new MailMessage(from_email, recipient, subject, body.ToString()); client.Send(mm); message = "Email Sent to " + recipient + " with subject " + subject; Console.WriteLine(message); EventLogging.WriteEntry(message, EventLogEntryType.SuccessAudit); } catch (Exception e) { message = "Could not send email\n\n" + e.ToString(); Console.WriteLine(message); EventLogging.WriteEntry(message, EventLogEntryType.Error); } }
/// <summary> /// Performs the syncronization (uploading and downloading) of directories (local and ftp) based on InputParameters set /// </summary> /// <param name="Parameters">Holds the properties needed for the synchronization</param> /// <returns>0 if successfull, 1 if error</returns> public int SynchronizeDirectories(InputParameters Parameters) { // Setup Event Log EventLogging.AppEventLog.Source = Parameters.AppLogSourceName; EventLogging.AppLoggingEventId = Parameters.AppLoggingEventId; EventLogging.AppLoggingCategory = Parameters.AppLoggingCategory; // Check if Downloading or Uploading bool IsDownloading = Parameters.UsedForDownloading.Equals("TRUE") ? true : false; // Check if to delete file afterwards bool DeleteFiles = Parameters.DeleteFiles.Equals("TRUE") ? true : false; string ActionVerb = IsDownloading ? "Downloading" : "Uploading"; SynchronizationMode SyncMode = IsDownloading ? SynchronizationMode.Local : SynchronizationMode.Remote; // Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = Parameters.HostName, UserName = Parameters.UserName, SshHostKeyFingerprint = Parameters.SshHostKeyFingerprint, SshPrivateKeyPath = Parameters.SshPrivateKeyPath }; StringBuilder messages = new StringBuilder(); string message = ""; try { using (Session session = new Session()) { string FTPFolder = Parameters.FTPDownloadFolder; string LocalPath = Parameters.LocalDestinationPath; bool FoundFilesToSync = false; EventLogging.WriteEntry("Before Connecting for " + ActionVerb + " - " + sessionOptions.HostName + " : " + sessionOptions.UserName, EventLogEntryType.Information); // Connect session.Open(sessionOptions); message = "Connected for " + ActionVerb + " from " + (IsDownloading ? "FTP Folder " + FTPFolder + " to Local Folder " + LocalPath : "Local Folder " + LocalPath + " to FTP Folder " + FTPFolder); EventLogging.WriteEntry(message, EventLogEntryType.Information); // Will continuously report progress of synchronization session.FileTransferred += FileTransferred; // Synchronize Directories SynchronizationResult synchronizationResult = session.SynchronizeDirectories(SyncMode, LocalPath, FTPFolder, DeleteFiles); // Throw on any error synchronizationResult.Check(); // Check if downloaded anything and delete remote file foreach (TransferEventArgs Download in synchronizationResult.Downloads) { FoundFilesToSync = true; // log successfull message message = "Success in " + ActionVerb + " file '" + Download.FileName + "' to Local Folder " + Download.Destination; EventLogging.WriteEntry(message, EventLogEntryType.SuccessAudit); // remember messages for email sending messages.AppendLine(message); // delete remote file if (DeleteFiles) { session.RemoveFiles(Download.FileName); message = "Success in deleting remote file '" + Download.FileName + "'"; EventLogging.WriteEntry(message, EventLogEntryType.SuccessAudit); messages.AppendLine(message); } } // Check if uploaded anything and delete local file foreach (TransferEventArgs Upload in synchronizationResult.Uploads) { FoundFilesToSync = true; // log successfull message message = "Success in " + ActionVerb + " file '" + Upload.FileName + "' to FTP folder " + Upload.Destination; EventLogging.WriteEntry(message, EventLogEntryType.SuccessAudit); // remember messages for email sending messages.AppendLine(message); // delete local file if (DeleteFiles && File.Exists(Upload.FileName)) { File.Delete(Upload.FileName); message = "Success in deleting local file '" + Upload.FileName + "'"; EventLogging.WriteEntry(message, EventLogEntryType.SuccessAudit); messages.AppendLine(message); } } // Send Email if (FoundFilesToSync) { SendEmail.Send(true, Parameters, messages); } else { // nothing different found message = "No files found for " + ActionVerb; EventLogging.WriteEntry(message, EventLogEntryType.SuccessAudit); } } return(0); } catch (Exception e) { // log Error message message = "Error in " + ActionVerb + " : " + e.ToString(); EventLogging.WriteEntry(message, EventLogEntryType.Error); messages.AppendLine(message); SendEmail.Send(false, Parameters, messages); return(1); } }