Exemplo n.º 1
0
        public ActionResult GetMailAttachment(int courseId, int threadId, string fileName)
        {
            var course = (from c in db.CourseUsers
                          where c.UserProfileID == CurrentUser.ID && c.AbstractCourseID == courseId
                          select c).FirstOrDefault();

            if (course != null)
            {
                MailAttachmentFilePath mfp = Directories.GetMailAttachment(courseId, threadId);
                Stream stream = FileSystem.GetDocumentForRead(mfp.GetPath() + "\\" + fileName);
                return(new FileStreamResult(stream, MimeMapping.GetMimeMapping(fileName))
                {
                    FileDownloadName = fileName
                });
            }
            return(RedirectToAction("Index", "Home"));
        }
Exemplo n.º 2
0
        private List <string> GetAttachmentUrls(int ContextID, int ThreadID)
        {
            MailAttachmentFilePath mfp = Directories.GetMailAttachment(ContextID, ThreadID);

            if (mfp.AllFiles().Count() > 0)
            {
                List <string> attachmentUrls = new List <string>();
                foreach (string filePath in mfp.AllFiles())
                {
                    string attachmentUrl = StringConstants.WebClientRoot + "FileHandler/GetMailAttachment?" + "courseId={0}" + "&threadId={1}" + "&fileName={2}";
                    attachmentUrl = string.Format(attachmentUrl, ContextID.ToString(), ThreadID.ToString(), Path.GetFileName(filePath));
                    attachmentUrls.Add(attachmentUrl);
                }
                return(attachmentUrls);
            }
            return(new List <string>());
        }
Exemplo n.º 3
0
        public ViewResult Index(MailSort sortBy = MailSort.LatestDate)
        {
            ViewBag.BoxHeader = "Inbox";
            List <Mail> mails = db.Mails.Where(m => m.ToUserProfileID == CurrentUser.ID && m.DeleteFromInbox == false).ToList();

            OrderMailAndSetViewBag(sortBy, ref mails);

            if (mails.Count() > 0)
            {
                List <int> threadIds = new List <int>();
                foreach (Mail mail in mails)
                {
                    MailAttachmentFilePath mfp = Directories.GetMailAttachment(mail.ContextID, mail.ThreadID);
                    if (mfp.AllFiles().Count() > 0)
                    {
                        threadIds.Add(mail.ThreadID);
                    }
                }
                ViewBag.AttachmentThreadIds = threadIds;
            }
            return(View(mails));
        }
Exemplo n.º 4
0
        public ActionResult Create(Mail mail, IEnumerable <HttpPostedFileBase> files = null)
        {
            if (ModelState.IsValid)
            {
                string   recipient_string = Request.Params["recipientlist"];
                string[] recipients;
                string   currentCourse = Request.Form["CurrentlySelectedCourse"];             //gets selected FROM courseid
                string   mailReply     = Request.Form["mailReply"];
                string   forwardedAttachmentsContextId = Request.Form["forwarded_contextId"]; //get forwarded attachments context id
                string   forwardedAttachmentsThreadId  = Request.Form["forwarded_threadId"];  //get forwarded attachments thread id
                bool     forwardedAttachments          = false;

                if (!String.IsNullOrEmpty(forwardedAttachmentsContextId) && !String.IsNullOrEmpty(forwardedAttachmentsThreadId))
                {
                    forwardedAttachments = true;
                }

                if (mailReply == "" || mailReply == null)
                {
                    mail.ContextID = Convert.ToInt16(currentCourse);
                }
                // AJ: Keep the ContextID the same if this is a reply to avoid confusion

                mail.Context = db.Courses.Where(b => b.ID == mail.ContextID).FirstOrDefault();

                if (recipient_string != null)
                {
                    recipients = recipient_string.Split(',');
                    int count    = 0;
                    int threadID = 0;
                    int dummyOut = 0;

                    bool attachmentsSaved = false;

                    foreach (string id in recipients)
                    {
                        if (Int32.TryParse(id, out dummyOut))
                        {
                            Mail newMail = new Mail();
                            newMail.FromUserProfileID = CurrentUser.ID;
                            newMail.Read             = false;
                            newMail.ToUserProfileID  = Convert.ToInt32(id);
                            newMail.Subject          = mail.Subject;
                            newMail.Message          = mail.Message;
                            newMail.ThreadID         = threadID;
                            newMail.ContextID        = mail.ContextID;
                            newMail.DeleteFromInbox  = false;
                            newMail.DeleteFromOutbox = false;

                            if (newMail.ContextID <= 0)
                            {
                                newMail.ContextID = ActiveCourseUser.AbstractCourseID;
                            }

                            //need to create the mail before we can send the notification and set the threadID
                            db.Mails.Add(newMail);
                            db.SaveChanges();

                            // need to have an email created to get a valid id to set the thread ids to.
                            if (count == 0)
                            {
                                threadID         = newMail.ID;
                                newMail.ThreadID = newMail.ID;

                                db.SaveChanges();
                            }

                            //now that the message is saved, set up attachments.
                            //handle file attachments
                            if ((null != files && !attachmentsSaved) || forwardedAttachments)
                            {
                                MailAttachmentFilePath mfp = Directories.GetMailAttachment(mail.ContextID, newMail.ThreadID);
                                if (forwardedAttachments)
                                {   //we are also adding attachments from the forwarded mail
                                    MailAttachmentFilePath forwardedMfp = Directories.GetMailAttachment(Int32.Parse(forwardedAttachmentsContextId), Int32.Parse(forwardedAttachmentsThreadId));
                                    foreach (var item in forwardedMfp.AllFiles())
                                    {
                                        mfp.AddFile(Path.GetFileName(item), forwardedMfp.OpenFileRead(Path.GetFileName(item)));
                                    }
                                }

                                if (null != files && !attachmentsSaved)
                                {
                                    foreach (var file in files)
                                    {
                                        if (null != file && file.ContentLength > 0)
                                        {
                                            Regex  illegalInFileName = new Regex(@"[\\/:*?""<>|+#]");
                                            string fileName          = illegalInFileName.Replace(Path.GetFileName(file.FileName), "");
                                            mfp.AddFile(fileName, file.InputStream);
                                        }
                                    }
                                }
                                attachmentsSaved = true; //we only need to save attachments once for each mail thread
                            }

                            using (NotificationController nc = new NotificationController())
                            {
                                nc.SendMailNotification(newMail);
                            }
                            ++count;
                        }
                    }
                    return(RedirectToAction("Index"));
                }
            }
            return(View(mail));
        }