public ActionResult Send(correos correos) { List <string> mails = Newtonsoft.Json.JsonConvert.DeserializeObject <List <string> >(correos.To); if (Request.Files[0].ContentLength > 0) { List <MailFile> mailAttachments = new List <MailFile>(); for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i] as HttpPostedFileBase; byte[] fileB = new byte[file.ContentLength]; using (Stream s = file.InputStream) { s.Position = 0; s.Read(fileB, 0, (int)s.Length); } MailFile mf = new MailFile( Name: file.FileName, FileBytes: fileB, FileType: file.ContentType); mailAttachments.Add(mf); } bool sent = Mail.SendWithAttachments(mails, correos.Asunto, correos.Contenido, mailAttachments); if (sent) { return(RedirectToAction("Index")); } } else { bool sent = Mail.Send(mails, correos.Asunto, correos.Contenido); if (sent) { return(RedirectToAction("Index")); } } return(RedirectToAction("Send", new { id = correos.Id, Error = "No se pudo enviar correo, intente de nuevo mas tarde." })); }
public static MailFile[] ParseFolder(string folder, char delimiter, bool versions) { if(string.IsNullOrEmpty(folder)) return null; if(!Directory.Exists(folder)) return null; folder = Path.GetFullPath(folder); string[] names = Directory.GetFiles(folder); if(names == null) return new MailFile[0]; // get all files in the folder MailFile[] infos = new MailFile[names.Length]; for(int irun=0; irun < names.Length; irun++) { string name = names[irun]; infos[irun].Folder = folder; infos[irun].FileName = Path.GetFileName(name); infos[irun].MailboxNameDecode(delimiter); } // we must sort by mailbox name + version SortFiles(infos); // count versions uint ucnt = 0; uint[] uver = new uint[infos.Length]; string mbox = ""; for(int irun=0; irun < infos.Length; irun++) { if(infos[irun].MailboxName == "") continue; if(mbox == infos[irun].MailboxName) { uver[ucnt-1] = uver[ucnt-1] + 1; continue; } mbox = infos[irun].MailboxName; ucnt++; } // create the final MailFile array ... MailFile[] roots = new MailFile[ucnt]; mbox = null; ucnt = 0; ushort latest = 0; for(int irun=0; irun < infos.Length; irun++) { if(infos[irun].MailboxName == "") continue; if(mbox == infos[irun].MailboxName) { if(infos[irun].Latest <= latest) continue; latest = infos[irun].Latest; roots[ucnt-1].Latest = latest; continue; } roots[ucnt] = infos[irun]; latest = roots[ucnt].Latest; mbox = infos[irun].MailboxName; // fill the Versions array if(uver[ucnt] > 0 && versions) { uint uarr = uver[ucnt] + 1; ushort[] varr = new ushort[uarr]; string[] narr = new string[uarr]; for(uint uarc=0; uarc < uarr; uarc++) { varr[uarc] = infos[irun+(int)uarc].Latest; narr[uarc] = infos[irun+(int)uarc].FileName; } roots[ucnt].VersionNumbers = varr; roots[ucnt].VersionNames = narr; } ucnt++; } return roots; }
public static void DumpMailFiles(MailFile[] files) { if(files == null) { Console.WriteLine("No files"); return; } foreach(MailFile f in files) Console.WriteLine(f.ToString()); }
public static int FindMailbox(MailFile[] existing, string mailbox) { if(existing == null) return -2; if(string.IsNullOrEmpty(mailbox)) return -2; for(int irun=0; irun < existing.Length; irun++) if(existing[irun].MailboxName == mailbox) return irun; return -1; }
private static void SortFiles(MailFile[] infos) { // get maximum name length string[] names = new string[infos.Length]; int ilen = 0; for(int irun=0; irun < names.Length; irun++) ilen = Math.Max(ilen, infos[irun].MailboxName.Length); // create sort key ... for(int irun=0; irun < names.Length; irun++) names[irun] = string.Format("{0}{1,4:X}", infos[irun].MailboxName.PadRight(ilen), infos[irun].Latest); // we must sort by mailbox name + version Array.Sort(names, infos); }