Exemple #1
0
        public async Task <IActionResult> AddMailsAsync(Mail mail)
        {
            string message = string.Empty;

            try
            {
                if (ModelState.IsValid)
                {
                    var apiKey = configuration.GetSection("SENDGRID_API_KEY").Value;
                    var client = new SendGridClient(apiKey);

                    MailModel model = new MailModel();
                    model.MailFrom = mail.From;
                    message        = model.SetMailTo(mail.To);
                    if (message != string.Empty)
                    {
                        return(BadRequest(message));
                    }
                    model.Title = mail.Title;
                    model.Body  = mail.Body;

                    await MailService.SendAsync(model, client);

                    db.Mails.Add(mail);
                    db.SaveChanges();
                    return(CreatedAtAction(nameof(GetMails), new { id = mail.Id }, mail));
                }
                return(BadRequest("Błedne dane wejsciowe"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }