protected override void AdapterOut(params Employee[] entities) { foreach (var item in entities) { item.AvatarList = AttachmentsIO.getAvatarsFromFolder(item.AvatarFolder, "Avatar"); if (item.TrainingScores != null) { if (item.Level1Key > 0) { for (int i = item.TrainingScores.Count - 1; i >= 0; i--) { var score = item.TrainingScores.ElementAt(i); if (score.Training.Level1Key != item.Level1Key) { item.TrainingScores.Remove(score); } } } item.TrainingScores = (from e in item.TrainingScores group e by e.Training.CertificationKey into groups select groups.OrderByDescending(c => c.Training.DateExpiresAt).First()).ToList(); } } }
protected override void OnBeforeSaving(Email entity, OPERATION_MODE mode = OPERATION_MODE.NONE) { if (mode == OPERATION_MODE.ADD) { #region Validations if (entity.ToList.Count() == 0 && entity.CcList.Count() == 0 && entity.BccList.Count == 0) { throw new KnownError("Cannot send email without Recipients."); } #endregion entity.CreatedAt = DateTimeOffset.Now; } #region Send Email //Copy Attachments when resent: if (entity.IsResent) { string baseAttachmentsPath = AppSettings.Get <string>("EmailAttachments"); entity.AttachmentsFolder = AttachmentsIO.CopyAttachments(entity.AttachmentsFolder, entity.Attachments, baseAttachmentsPath); entity.Attachments = entity.Attachments.Where(e => !e.ToDelete).ToList(); } var emailService = new MailgunService { From = Auth.Email, Subject = entity.Subject, Body = entity.Body, AttachmentsFolder = entity.AttachmentsFolder }; foreach (var item in entity.ToList) { emailService.To.Add(item.Email); } foreach (var item in entity.CcList) { emailService.Cc.Add(item.Email); } foreach (var item in entity.BccList) { emailService.Bcc.Add(item.Email); } emailService.Bcc.Add(Auth.Email); //Add sender as recipient as well. try { emailService.SendMail(); } catch (Exception ex) { throw new KnownError("Could not send email:\n" + ex.Message); } #endregion }
public override List <MdcAttachmentFile> AdapterOut(params MdcAttachmentFile[] entities) { foreach (var item in entities) { item.Attachments = AttachmentsIO.getAttachmentsFromFolder(item.AttachmentsFolder, "MdcAttachment"); } return(entities.ToList()); }
protected override List <Email> AdapterOut(params Email[] entities) { foreach (var item in entities) { item.Attachments = AttachmentsIO.getAttachmentsFromFolder(item.AttachmentsFolder, "EmailAttachments"); } return(entities.ToList()); }
public void SendMail() { var request = new RestRequest(); request.Method = Method.POST; request.AddParameter("from", From); foreach (var to in To) { request.AddParameter("to", to); } foreach (var cc in Cc) { request.AddParameter("cc", cc); } foreach (var bcc in Bcc) { request.AddParameter("bcc", bcc); } request.AddParameter("subject", Subject); string baseAttachmentsPath = AppSettings.Get <string>("EmailAttachments"); var attachments = AttachmentsIO.getAttachmentsFromFolder(AttachmentsFolder, "EmailAttachments"); foreach (var attachment in attachments) { string filePath = baseAttachmentsPath + attachment.Directory + "\\" + attachment.FileName; request.AddFile("attachment", filePath); } if (!string.IsNullOrWhiteSpace(Template)) { request.AddParameter("template", Template); foreach (var param in TemplateParameters) { request.AddParameter($"v:{param.Key}", param.Value); } } else { request.AddParameter("html", Body); } var response = Client.Execute(request); var content = response.Content; if (!response.IsSuccessful) { throw new Exception(content); } }
protected override void AdapterOut(params PurchaseRequest[] entities) { var ctx = context as POContext; foreach (var item in entities) { //item.PRNumber = ctx.PRNumbers.FirstOrDefault(e => e.PRNumberKey == item.PRNumberKey); item.PRLines = item.PRLines?.OrderBy(e => e.PRLineKey).ToList(); item.Attachments = AttachmentsIO.getAttachmentsFromFolder(item.AttachmentsFolder, "PR_Attachments"); } }
public override List <Account> AdapterOut(params Account[] entities) { foreach (var item in entities) { item.Password = null; item.ConfirmPassword = null; item.PasswordHash = null; item.UpdatePassword = false; item.Avatars = AttachmentsIO.getAvatarsFromFolder(item.AvatarFolder, "Avatar"); } return(entities.ToList()); }
public void SendMail() { try { smtp.EnableSsl = true; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential(From, FromPassword); MailMessage message = new MailMessage(); message.From = new MailAddress(From, From); foreach (var to in To) { message.To.Add(new MailAddress(to)); } foreach (var cc in Cc) { message.CC.Add(new MailAddress(cc)); } foreach (var bcc in Bcc) { message.Bcc.Add(new MailAddress(bcc)); } message.Subject = Subject; message.IsBodyHtml = true; message.BodyEncoding = System.Text.Encoding.UTF8; message.Body = Body; string baseAttachmentsPath = AppSettings.Get <string>("EmailAttachments"); var attachments = AttachmentsIO.getAttachmentsFromFolder(AttachmentsFolder, "EmailAttachments"); foreach (var attachment in attachments) { string filePath = baseAttachmentsPath + attachment.Directory + "\\" + attachment.FileName; FileInfo file = new FileInfo(filePath); message.Attachments.Add(new System.Net.Mail.Attachment(new FileStream(filePath, FileMode.Open, FileAccess.Read), attachment.FileName)); } smtp.Send(message); } catch (System.Exception ex) { throw ex; } }
protected override void onBeforeSaving(PurchaseRequest entity, BaseEntity parent = null, OPERATION_MODE mode = OPERATION_MODE.NONE) { if (mode == OPERATION_MODE.ADD) { var ctx = context as POContext; if (entity.GeneralManager != null) { ctx.Entry(entity.GeneralManager).State = EntityState.Unchanged; } if (entity.Requisitor != null) { ctx.Entry(entity.Requisitor).State = EntityState.Unchanged; } if (entity.DepartmentAssigned != null) { ctx.Entry(entity.DepartmentAssigned).State = EntityState.Unchanged; } if (entity.DepartmentManager != null) { ctx.Entry(entity.DepartmentManager).State = EntityState.Unchanged; } //if (string.IsNullOrWhiteSpace(entity.FriendlyIdentifier)) //{ // throw new KnownError("Friendly identifier is a required field"); //} #region PR Number Generation DateTimeOffset date = DateTimeOffset.Now; int sequence = 0; var last = ctx.PRNumbers.Where(n => n.CreatedAt.Year == date.Year && n.CreatedAt.Month == date.Month && n.CreatedAt.Day == date.Day).OrderByDescending(n => n.Sequence) .FirstOrDefault(); if (last != null) { sequence = last.Sequence + 1; } string generated = date.Year.ToString().Substring(2) + date.Month.ToString("D2") + date.Day.ToString("D2") + sequence.ToString("D3"); PRNumber cqaNumber = ctx.PRNumbers.Add(new PRNumber() { CreatedAt = date, Sequence = sequence, GeneratedNumber = generated, Revision = "A" }); ctx.SaveChanges(); entity.PRNumberKey = cqaNumber.PRNumberKey; #endregion #region var comment = new Comment(); comment.CommentByUserKey = LoggedUser.LocalUser.UserKey; ctx.Comments.Add(comment); ctx.SaveChanges(); entity.CommentKey = comment.CommentKey; #endregion entity.RequisitorKey = LoggedUser.LocalUser.UserKey; } #region Attachments string baseAttachmentsPath = ConfigurationManager.AppSettings["PR_Attachments"]; if (entity != null && entity.Attachments != null) { foreach (var file in entity.Attachments) { if (file.ToDelete) { string filePath = baseAttachmentsPath + "\\" + file.Directory + "\\" + file.FileName; AttachmentsIO.DeleteFile(filePath); } } } #endregion }
public object Post(PostAttachment request) { var response = new CommonResponse(); string attachmentKind = request.AttachmentKind; if (attachmentKind == null || attachmentKind == "") { response.ErrorThrown = true; response.ResponseDescription = "Attachment Kind was not specified."; return(response); } if (Request.Files.Length == 0) { throw new HttpError(HttpStatusCode.BadRequest, "NoFile"); } var postedFile = Request.Files[0]; string fileName = postedFile.FileName; string baseAttachmentsPath = AppSettings.Get <string>(attachmentKind); bool useAttachmentsRelativePath = false; string sUseAttachmentsRelativePath = AppSettings.Get <string>("UseAttachmentsRelativePath"); if (!string.IsNullOrWhiteSpace(sUseAttachmentsRelativePath) && bool.TryParse(sUseAttachmentsRelativePath, out bool bUseAttachmentsRelativePath)) { useAttachmentsRelativePath = bUseAttachmentsRelativePath; } string currentPathAttachments; string folderName = request.TargetFolder; if (!string.IsNullOrWhiteSpace(folderName)) { if (useAttachmentsRelativePath) { currentPathAttachments = "~/" + baseAttachmentsPath + folderName + @"/".MapHostAbsolutePath(); } else { currentPathAttachments = baseAttachmentsPath + folderName + @"\"; } if (!Directory.Exists(currentPathAttachments)) { Directory.CreateDirectory(currentPathAttachments); } } else { string folderPrefix = request.FolderPrefix; if (string.IsNullOrWhiteSpace(folderPrefix) || folderPrefix == "undefined" || folderPrefix == "null") { folderPrefix = ""; } do { DateTime date = DateTime.Now; folderName = folderPrefix + date.ToString("yy") + date.Month.ToString("d2") + date.Day.ToString("d2") + "_" + MD5HashGenerator.GenerateKey(date); if (useAttachmentsRelativePath) { currentPathAttachments = "~/" + baseAttachmentsPath + folderName.MapHostAbsolutePath(); } else { currentPathAttachments = baseAttachmentsPath + folderName; } } while (Directory.Exists(currentPathAttachments)); Directory.CreateDirectory(currentPathAttachments); if (useAttachmentsRelativePath) { currentPathAttachments += @"/"; } else { currentPathAttachments += @"\"; } } if (postedFile.ContentLength > 0) { postedFile.SaveTo(currentPathAttachments + Path.GetFileName(postedFile.FileName)); } var attachmentsResult = AttachmentsIO.getAttachmentsFromFolder(folderName, attachmentKind); response.ErrorThrown = false; response.ResponseDescription = folderName; response.Result = attachmentsResult; return(response); }
public object avatar() { CommonResponse response = new CommonResponse(); try { if (HttpContext.Current.Request.Files.AllKeys.Any()) { var postedFile = HttpContext.Current.Request.Files["file"]; if (postedFile != null) { string fileName = postedFile.FileName; string baseAttachmentsPath = ConfigurationManager.AppSettings["Avatar"]; string currentPathAttachments; string folderName = HttpContext.Current.Request["targetFolder"]; if (folderName != null && folderName.Trim() != "") { currentPathAttachments = baseAttachmentsPath + folderName + @"\"; if (!Directory.Exists(currentPathAttachments)) { Directory.CreateDirectory(currentPathAttachments); } else { AttachmentsIO.ClearDirectory(currentPathAttachments); } } else { do { DateTime date = DateTime.Now; folderName = date.ToString("yy") + date.Month.ToString("d2") + date.Day.ToString("d2") + "_" + MD5HashGenerator.GenerateKey(date); currentPathAttachments = baseAttachmentsPath + folderName; } while (Directory.Exists(currentPathAttachments)); Directory.CreateDirectory(currentPathAttachments); currentPathAttachments += @"\"; } if (postedFile.ContentLength > 0) { postedFile.SaveAs(currentPathAttachments + Path.GetFileName(postedFile.FileName)); } List <Avatar> attachmentsResult = AttachmentsIO.getAvatarsFromFolder(folderName, "Avatar"); response.ErrorThrown = false; response.ResponseDescription = folderName; response.Result = attachmentsResult; } } } catch (Exception e) { response.ErrorThrown = true; response.ResponseDescription = "ERROR: " + e.Message; response.Result = e; } return(response); }
public object attachmentsPost() { CommonResponse response = new CommonResponse(); try { string attachmentKind = HttpContext.Current.Request["attachmentKind"]; if (attachmentKind == null || attachmentKind == "") { response.ErrorThrown = true; response.ResponseDescription = "Attachment Kind was not specified."; return(response); } var postedFile = HttpContext.Current.Request.Files["file"]; if (postedFile != null) { string fileName = postedFile.FileName; string baseAttachmentsPath = ConfigurationManager.AppSettings[attachmentKind]; bool useAttachmentsRelativePath = false; string sUseAttachmentsRelativePath = ConfigurationManager.AppSettings["UseAttachmentsRelativePath"]; if (!string.IsNullOrWhiteSpace(sUseAttachmentsRelativePath) && bool.TryParse(sUseAttachmentsRelativePath, out bool bUseAttachmentsRelativePath)) { useAttachmentsRelativePath = bUseAttachmentsRelativePath; } string currentPathAttachments; string folderName = HttpContext.Current.Request["targetFolder"]; if (folderName != null && folderName.Trim() != "") { if (useAttachmentsRelativePath) { currentPathAttachments = HostingEnvironment.MapPath("~/" + baseAttachmentsPath + folderName + @"/"); } else { currentPathAttachments = baseAttachmentsPath + folderName + @"\"; } if (!Directory.Exists(currentPathAttachments)) { Directory.CreateDirectory(currentPathAttachments); } } else { string folderPrefix = HttpContext.Current.Request["folderPrefix"]; if (string.IsNullOrWhiteSpace(folderPrefix) || folderPrefix == "undefined" || folderPrefix == "null") { folderPrefix = ""; } do { DateTime date = DateTime.Now; folderName = folderPrefix + date.ToString("yy") + date.Month.ToString("d2") + date.Day.ToString("d2") + "_" + MD5HashGenerator.GenerateKey(date); if (useAttachmentsRelativePath) { currentPathAttachments = HostingEnvironment.MapPath("~/" + baseAttachmentsPath + folderName); } else { currentPathAttachments = baseAttachmentsPath + folderName; } } while (Directory.Exists(currentPathAttachments)); Directory.CreateDirectory(currentPathAttachments); if (useAttachmentsRelativePath) { currentPathAttachments += @"/"; } else { currentPathAttachments += @"\"; } } if (postedFile.ContentLength > 0) { postedFile.SaveAs(currentPathAttachments + Path.GetFileName(postedFile.FileName)); } List <Attachment> attachmentsResult = AttachmentsIO.getAttachmentsFromFolder(folderName, attachmentKind); response.ErrorThrown = false; response.ResponseDescription = folderName; response.Result = attachmentsResult; } } catch (Exception e) { response.ErrorThrown = true; response.ResponseDescription = "ERROR: " + e.Message; response.Result = e; } return(response); }
public object Get(GetAvatarFromFolder request) { return(AttachmentsIO.getAvatarsFromFolder(request.Directory, request.AttachmentKind)); }
public void SendMail() { try { From = AppSettings.Get <string>("smtp.from"); FromPassword = AppSettings.Get <string>("smtp.password"); var smtpServer = AppSettings.Get <string>("smtp.server"); var smtpPort = AppSettings.Get <string>("smtp.port"); var smtpSSL = AppSettings.Get("smtp.ssl", true); if (string.IsNullOrWhiteSpace(smtpServer) || string.IsNullOrWhiteSpace(smtpPort)) { return; } smtp = new SmtpClient(smtpServer, int.Parse(smtpPort)); smtp.EnableSsl = smtpSSL; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential(From, FromPassword); MailMessage message = new MailMessage(); message.From = new MailAddress(From, From); foreach (var to in To) { message.To.Add(new MailAddress(to)); } foreach (var cc in Cc) { message.CC.Add(new MailAddress(cc)); } foreach (var bcc in Bcc) { message.Bcc.Add(new MailAddress(bcc)); } message.Subject = Subject; message.IsBodyHtml = true; message.BodyEncoding = System.Text.Encoding.UTF8; message.Body = Body; string baseAttachmentsPath = AppSettings.Get <string>("EmailAttachments"); var attachments = AttachmentsIO.getAttachmentsFromFolder(AttachmentsFolder, "EmailAttachments"); foreach (var attachment in attachments) { string filePath = baseAttachmentsPath + attachment.Directory + "\\" + attachment.FileName; FileInfo file = new FileInfo(filePath); message.Attachments.Add(new System.Net.Mail.Attachment(new FileStream(filePath, FileMode.Open, FileAccess.Read), attachment.FileName)); } smtp.Send(message); Log.Info($"SMTP Email sent by: [{From}] to:[{To.Join(", ")}] cc:[{Cc.Join(", ")}] bcc:[{Bcc.Join(", ")}] subject: [{Subject}]"); } catch (System.Exception ex) { throw ex; } }
public object Post(PostAvatar request) { var response = new CommonResponse(); if (Request.Files.Length == 0) { throw new HttpError(HttpStatusCode.BadRequest, "NoFile"); } var postedFile = Request.Files[0]; string fileName = postedFile.FileName; string baseAttachmentsPath = ConfigurationManager.AppSettings["Avatar"]; bool useAttachmentsRelativePath = false; string sUseAttachmentsRelativePath = ConfigurationManager.AppSettings["UseAttachmentsRelativePath"]; if (!string.IsNullOrWhiteSpace(sUseAttachmentsRelativePath) && bool.TryParse(sUseAttachmentsRelativePath, out bool bUseAttachmentsRelativePath)) { useAttachmentsRelativePath = bUseAttachmentsRelativePath; } string currentPathAttachments; string folderName = request.TargetFolder; if (!string.IsNullOrWhiteSpace(folderName)) { if (useAttachmentsRelativePath) { currentPathAttachments = "~/" + baseAttachmentsPath + folderName + @"/".MapHostAbsolutePath(); } else { currentPathAttachments = baseAttachmentsPath + folderName + @"\"; } if (!Directory.Exists(currentPathAttachments)) { Directory.CreateDirectory(currentPathAttachments); } else { AttachmentsIO.ClearDirectory(currentPathAttachments); } } else { do { DateTime date = DateTime.Now; folderName = date.ToString("yy") + date.Month.ToString("d2") + date.Day.ToString("d2") + "_" + MD5HashGenerator.GenerateKey(date); if (useAttachmentsRelativePath) { currentPathAttachments = "~/" + baseAttachmentsPath + folderName.MapHostAbsolutePath(); } else { currentPathAttachments = baseAttachmentsPath + folderName; } } while (Directory.Exists(currentPathAttachments)); Directory.CreateDirectory(currentPathAttachments); if (useAttachmentsRelativePath) { currentPathAttachments += @"/"; } else { currentPathAttachments += @"\"; } } if (postedFile.ContentLength > 0) { postedFile.SaveTo(currentPathAttachments + Path.GetFileName(postedFile.FileName)); } var attachmentsResult = AttachmentsIO.getAvatarsFromFolder(folderName, "Avatar"); response.ErrorThrown = false; response.ResponseDescription = folderName; response.Result = attachmentsResult; return(response); }