public async Task <IActionResult> Send(InfoFormViewModel contact, IFormFile[] attachments, CaptchaResponse captcha)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.ErrorMsg = "Напиши мне что-нибудь. Плохо, когда поля пустые :-(";
                return(View("Index", contact));
            }

            // validate input
            var requestIsValid = await this.verificationService.IsCaptchaValid(captcha.Token);

            if (!requestIsValid)
            {
                ViewBag.ErrorMsg = "Ты пытаешся нас сломать или это гугл приуныл?";
                return(View("Index", contact));
            }

            Dictionary <string, Stream> files = new();

            if (attachments != null && attachments.Length > 0)
            {
                long totalSize = 0;
                foreach (IFormFile attachment in attachments)
                {
                    var filename = Path.GetFileName(attachment.FileName);
                    var stream   = attachment.OpenReadStream();

                    totalSize += stream.Length;

                    if (totalSize > fileSizeLimit)
                    {
                        ViewBag.ErrorMsg = "Файл слишком большой";
                        return(View("Index", contact));
                    }

                    files[filename] = stream;
                }
            }

            var email = this.gmailSettings.Username;

            if (this.mailService.Send(email, email, contact.Subject, contact.Content, files))
            {
                ModelState.Clear();
                ViewBag.SuccessMsg = "Вялікі дзякуй!";
                ViewBag.ErrorMsg   = string.Empty;
            }
            else
            {
                ViewBag.ErrorMsg = "Упс.. Что то пошло не так. Мы уже разбираемся в этом. Попробуй чуть позже";
            }
            return(View("Index", new InfoFormViewModel()
            {
                ClientKey = this.captchaSettings.ClientKey
            }));
        }
Beispiel #2
0
        public async Task <IActionResult> Send(InfoFormViewModel contact, IFormFile[] attachments, CaptchaResponse captcha)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.msg = "Fields are empty";
                return(View("Index", contact));
            }

            // validate input
            var requestIsValid = await this.verificationService.IsCaptchaValid(captcha.Token);

            if (!requestIsValid)
            {
                ViewBag.msg = "Capcha is not valid";
                return(View("Index", contact));
            }

            Dictionary <string, Stream> files = new();

            if (attachments != null && attachments.Length > 0)
            {
                foreach (IFormFile attachment in attachments)
                {
                    var filename = Path.GetFileName(attachment.FileName);
                    var stream   = attachment.OpenReadStream();
                    files[filename] = stream;
                }
            }

            var email = this.gmailSettings.Username;

            if (this.mailService.Send(email, email, contact.Subject, contact.Content, files))
            {
                ViewBag.msg = "Sent Mail Successfully";
            }
            else
            {
                ViewBag.msg = "Failed";
            }
            return(View("Index", new InfoFormViewModel()));
        }