Exemplo n.º 1
0
        public async Task <IActionResult> SendTestbyBulk([FromBody] SendInvitationByBulkViewModel sendByBulkVM)
        {
            string[] userIds = null;
            userIds = sendByBulkVM.ids.Split(",");


            try
            {
                for (int i = 0; i < userIds.Length; i++)
                {
                    var is_user_exist = _context.Users.Any(u => u.UserId == Int32.Parse(userIds[i]));
                    if (!is_user_exist)
                    {
                        return(Json(StatusCode(404)));
                    }

                    var    user        = _context.Users.FirstOrDefault(u => u.UserId == Int32.Parse(userIds[i]));
                    string senderEmail = _configuration.GetSection("AdminEmailCredentials:SenderEmail").Value;

                    var content = "";
                    var subject = "You are invited to take an online test.";



                    await _send.SendEmailAsync(user.FirstName, senderEmail, user.Email, content, subject);
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(Json(Ok()));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> SendInvitationbyBulk(SendInvitationByBulkViewModel sendByBulkVM)
        {
            if (sendByBulkVM.ids == null)
            {
                return(Json(BadRequest("Ids is null.")));
            }

            string[] userIds = null;

            Console.WriteLine(sendByBulkVM.ids);
            if (sendByBulkVM.ids.Contains(","))
            {
                userIds = sendByBulkVM.ids.Split(",");
            }

            else
            {
                userIds    = new string[1];
                userIds[0] = sendByBulkVM.ids;
            }

            var path = "";

            // Creating a folder where the uploaded file will be stored.
            if (sendByBulkVM.UploadedFile == null || sendByBulkVM.UploadedFile.Length == 0)
            {
            }
            else
            {
                var dateTime = DateTime.Now.ToString("yyyy_MM_ddTHH_mm_ss");
                path = Path.Combine("C:\\Email", dateTime.ToString(), sendByBulkVM.UploadedFile.FileName);
                Directory.CreateDirectory(Path.GetDirectoryName(path));
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    sendByBulkVM.UploadedFile.CopyTo(stream);
                }
            }

            try
            {
                for (int i = 0; i < userIds.Length; i++)
                {
                    var is_user_exist = _context.Users.Any(u => u.UserId == Int32.Parse(userIds[i]));
                    if (!is_user_exist)
                    {
                        return(Json(StatusCode(404)));
                    }

                    var    user        = _context.Users.FirstOrDefault(u => u.UserId == Int32.Parse(userIds[i]));
                    string senderEmail = _configuration.GetSection("AdminEmailCredentials:SenderEmail").Value;
                    var    content     = sendByBulkVM.Content;

                    if (sendByBulkVM.UploadedFile == null || sendByBulkVM.UploadedFile.Length == 0)
                    {
                    }
                    else
                    {
                        var dir   = _configuration.GetSection("Directory3:ForEmail").Value;
                        var path2 = dir + DateTime.Now.ToString("yyyy_MM_ddTHH_mm_ss") + "//" + sendByBulkVM.UploadedFile.FileName;
                        Console.WriteLine(path2);
                        content += "<br>Attached herewith is a document: " + "<a href=\"" + path2 + "\">" + "link" + " </a></p>";
                    }



                    var subject = sendByBulkVM.Subject;
                    await _send.SendEmailAsync(user.FirstName, senderEmail, user.Email, content, subject);
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(Json(Ok()));
        }