Esempio n. 1
0
        public async Task <string> SendEmail([FromForm] EmailMessageModel model)
        {
            EmailMessageModel emailMessage = new EmailMessageModel
            {
                Name       = model.Name,
                Email      = model.Email,
                Message    = model.Message,
                Attachment = model.Attachment
            };

            if (emailMessage.Attachment != null)
            {
                if (_fileInsider.IsFormatAllowed(emailMessage.Attachment))
                {
                    if (emailMessage.Attachment.Length > 10485760)
                    {
                        return("File size not supported.");
                    }
                }
                else
                {
                    return("File format not supported.");
                }
            }

            MailMessage message = new MailMessage();

            message.Body = $"<h2>Name: {emailMessage.Name}, Email: {emailMessage.Email}</h2><p>{emailMessage.Message}</p>";

            if (emailMessage.Attachment != null)
            {
                Attachment data = new Attachment(emailMessage.Attachment.OpenReadStream(), emailMessage.Attachment.FileName, emailMessage.Attachment.ContentType);
                message.Attachments.Add(data);
            }

            message.IsBodyHtml = true;
            message.From       = new MailAddress(_appSettings.EmailSenderAddress, _appSettings.EmailSenderName);
            message.To.Add(new MailAddress(_appSettings.EmailReceiverAddress));
            message.Subject = _appSettings.EmailSubject;

            using (var client = new SmtpClient(_appSettings.SMTPHost, int.Parse(_appSettings.SMTPPort)))
            {
                client.Credentials = new NetworkCredential(_appSettings.SMTPUsername, _appSettings.SMTPPassword);
                client.EnableSsl   = true;

                try
                {
                    await client.SendMailAsync(message);

                    return("Message successfully sent!");
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"{nameof(SendEmail)} => FAIL with args: {model.Name} {model.Email} {model.Message} {model.Attachment.FileName} {model.Attachment.ContentType}");
                }

                return("Something went wrong. Try again!");
            }
        }
Esempio n. 2
0
        public async Task <string> CreatePostAsync([FromForm] AddEditPortfolioPostModel form)
        {
            if (form.Attachment != null)
            {
                if (_fileInsider.IsFormatAllowed(form.Attachment))
                {
                    if (form.Attachment.Length > 2097152)
                    {
                        return("File size not supported.");
                    }
                }
                else
                {
                    return("File format not supported.");
                }
            }

            try
            {
                DateTimeOffset newStartDate  = new DateTimeOffset(DateTime.Parse(form.StartDateInput));
                DateTimeOffset newFinishDate = new DateTimeOffset(DateTime.Parse(form.FinishDateInput));

                PortfolioPostModel newPost = new PortfolioPostModel
                {
                    Id              = form.Id,
                    Header          = form.Header,
                    CustomerTitle   = form.CustomerTitle,
                    Location        = form.Location,
                    StartDate       = newStartDate,
                    FinishDate      = newFinishDate,
                    ImageSource     = form.ImageSource,
                    VideoSource     = form.VideoSource,
                    MainText        = form.MainText,
                    PlayStoreSource = form.PlayStoreSource,
                    PlayStoreText   = form.PlayStoreText,
                    AppStoreSource  = form.AppStoreSource,
                    AppStoreText    = form.AppStoreText,
                    Footer          = form.Footer
                };

                if (form.Attachment != null)
                {
                    var generatedFileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + Path.GetExtension(form.Attachment.FileName);
                    var filePath          = Path.Combine(_saveDir, generatedFileName);

                    using (var stream = System.IO.File.Create(filePath))
                    {
                        await form.Attachment.CopyToAsync(stream);

                        newPost.ImageSource = "/img/uploads/" + generatedFileName;
                    }
                }

                _context.Add(newPost);
                await _context.SaveChangesAsync();

                return("Post successfully saved!");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"{nameof(CreatePostAsync)} => FAIL with args: {form}");
            }

            return("Something went wrong. Try again!");
        }