private void DeliverViaApplication(CcmsiUser user, UserReport userReport, DeliveredReportInfo info, string fileName, MemoryStream sourceStream, ScheduleRecipient recipient) { try { if (EnableFileServerSecurity) { if (!ImpersonateUser(FileServerUserID, FileServerDomain, FileServerPassword)) { throw new ApplicationException("Failed to impersonate user with access to file share"); } } var recipientCcmsiUser = GetCcmsiUser(recipient.UserID); string fileFolder = Path.Combine(FileServerPath, recipientCcmsiUser.UserID); if (!Directory.Exists(fileFolder)) { Directory.CreateDirectory(fileFolder); } string filePath = Path.Combine(fileFolder, fileName); sourceStream.Position = 0; using (FileStream targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { //read from the input stream in 4K chunks and save to file stream const int BUFFER_LENGTH = 4096; byte[] buffer = new byte[BUFFER_LENGTH]; int byteCount = 0; while ((byteCount = sourceStream.Read(buffer, 0, BUFFER_LENGTH)) > 0) { targetStream.Write(buffer, 0, byteCount); } targetStream.Close(); } int userReportOutputId = createUserReportOutputRecord(user, userReport, info, fileName, recipient); createReportDeliveryLogRecord(recipient.ScheduleID, recipient.ScheduleRecipientID, userReportOutputId); if (!LogExceptionsOnly) { Logger.Write(string.Format(" Report delivered to file share. UserID = {0}, User Name = {1}, File Name = {2}", user.ID, user.UserID, filePath), "SLLog"); } } finally { UndoImpersonation(); } }
private void DeliverViaEmail(string subject, string body, Schedule.Priority priority, string toEmailAddresses, DeliveredReportInfo info, string fileName, MemoryStream reportStream, ScheduleRecipient recipient, int scheduleID) { // Build the email message MailMessage email = new MailMessage(); email.IsBodyHtml = true; email.From = new MailAddress(SmtpFromAddress); email.Subject = subject; email.Body = body; switch (priority) { case Schedule.Priority.High: email.Priority = MailPriority.High; break; case Schedule.Priority.Low: email.Priority = MailPriority.Low; break; case Schedule.Priority.Normal: email.Priority = MailPriority.Normal; break; } reportStream.Position = 0; System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType(info.Extension.ToUpper() == "PDF" ? "application/pdf" : "application/vnd.ms-excel"); contentType.Name = fileName; Attachment attachment = new Attachment(reportStream, contentType); email.Attachments.Add(attachment); string[] addresses = toEmailAddresses.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); foreach (string address in addresses) { email.To.Add(address); } //// prepend security string //email.Subject = "[" + Environment.MachineName + "] " + email.Subject; if (IsEmailDeliveyEnabled) { SmtpClient.Send(email); } if (recipient != null) { createReportDeliveryLogRecord(scheduleID, recipient.ScheduleRecipientID, null); } else { createReportDeliveryLogRecord(scheduleID, null, null); } }
private int createUserReportOutputRecord(CcmsiUser user, UserReport userReport, DeliveredReportInfo info, string filename, ScheduleRecipient recipient) { using (ReportProjectDBEntities entities = new ReportProjectDBEntities()) { UserReportOutput rpt = new UserReportOutput() { UserReport = entities.UserReport.Where(r => r.UserReportID == userReport.UserReportID).First(), UserID = recipient.UserID, FileName = filename, ClientNumber = userReport.ClientNumber, ReportRunDate = info.ExecutionDate, CreatedDate = DateTime.Now, CreatedByUserID = Convert.ToInt32(userReport.CreatedByUserID) }; entities.AddToUserReportOutput(rpt); entities.SaveChanges(); return(rpt.UserReportOutputID); } }