示例#1
0
 public async Task SendEmail([ActivityTrigger] MailItemModel model, ILogger logger)
 {
     try
     {
         await _sendGridService.SendEmail(
             from : string.Empty,
             subject : model.Subject,
             to : model.To,
             plainContent : model.PlainBody,
             htmlContent : model.HtmlBody);
     }
     catch (Exception ex)
     {
         logger.LogError(ex.Message, ex);
     }
 }
        public async ValueTask <GenericCommandResult> Handle(PostSendEmailCommandInput command)
        {
            command.Validate();
            if (command.Invalid)
            {
                return(new GenericCommandResult(false, "Incorrect  data!", null, StatusCodes.Status400BadRequest, command.Notifications));
            }

            bool sendEmail = await _sendGridService.SendEmail(command.To, command.From, command.FromName, command.Subject, command.Message);

            if (!sendEmail)
            {
                return(new GenericCommandResult(false, "It was not possible to send your email !", null, StatusCodes.Status403Forbidden, command.Notifications));
            }


            return(new GenericCommandResult(true, "Your email sent successfully!", null, StatusCodes.Status200OK, command.Notifications));
        }
        public async Task Consume(ConsumeContext <NotifyByEmail> context)
        {
            var message = context.Message;

            try
            {
                await _sendGridService.SendEmail(
                    string.Empty,
                    message.Subject,
                    message.To,
                    message.PlainBody,
                    message.HtmlBody);

                await context.Publish(new NotificationSentSuccessfully(message.CorrelationId));
            }
            catch (Exception ex)
            {
                await context.Publish(new NotificationSentUnsuccessfully(message.CorrelationId, ex.Message));
            }
        }
        public async Task <IActionResult> Post([FromBody] EmailRequest model)
        {
            Data.Entities.Email newRecord = null;

            if (ModelState.IsValid)
            {
                InitUserCredentials();

                newRecord = new Data.Entities.Email
                {
                    Guid             = Guid.NewGuid(),
                    CompanyGuid      = CompanyGuid,
                    CreationTime     = DateTime.UtcNow,
                    CreationUserGuid = UserGuid
                };

                newRecord = _mapper.Map(model, newRecord);

                try
                {
                    User user = await _userService.Get((Guid)model.ToUserGuid);

                    if (user != null)
                    {
                        var toEmailAddress = user.Email;

                        #if !RELEASE
                        toEmailAddress = "*****@*****.**";
                        #endif

                        var response = await _sendGridService.SendEmail(new SendGridRequest
                        {
                            To = new [] { new EmailAddress(toEmailAddress) },
                            DynamicTemplateData = new SendGridDynamicData
                            {
                                TicketGuid = (Guid)model.TicketGuid
                            },
                            Template = (Enumerations.SendGridTemplateEnum)model.SendGridTemplate
                        });

                        if (response.StatusCode != HttpStatusCode.BadRequest)
                        {
                            var messageKvp = response.Headers.FirstOrDefault(x => x.Key == "X-Message-Id");

                            if (messageKvp.Value.Any())
                            {
                                newRecord.SendGridId = messageKvp.Value.FirstOrDefault();
                            }

                            await _emailService.Save(newRecord);
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e.StackTrace);
                    throw;
                }
            }

            return(CreatedAtAction(nameof(Post), _mapper.Map(newRecord, new EmailResponse())));
        }