Beispiel #1
0
        private async Task SendReplyToSender(SMSMessage message, string destinationNumber)
        {
            var sendRequest = new SendMessagesRequest
            {
                ApplicationId  = projectId,
                MessageRequest = new MessageRequest
                {
                    Addresses = new Dictionary <string, AddressConfiguration>
                    {
                        { destinationNumber, new AddressConfiguration {
                              ChannelType = "SMS"
                          } }
                    },
                    MessageConfiguration = new DirectMessageConfiguration {
                        SMSMessage = message
                    }
                }
            };

            try
            {
                Console.WriteLine($"Sending message: {message.Body} to {destinationNumber}");
                var response = await client.SendMessagesAsync(sendRequest).ConfigureAwait(false);

                Console.WriteLine("Message sent!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("The message wasn't sent. Error message: " + ex.Message);
            }
        }
Beispiel #2
0
        public async Task <IActionResult> SendTextMessage([FromBody] TextMessage model)
        {
            if (model == null || string.IsNullOrWhiteSpace(model.MessageBody) ||
                string.IsNullOrWhiteSpace(model.DestinationNumber) ||
                string.IsNullOrWhiteSpace(model.MessageType))
            {
                return(BadRequestResult(SMS));
            }

            if (!ValidationHelper.IsValidPhoneNumber(model.DestinationNumber))
            {
                return(BadRequestResult(SMS));
            }

            if (!ValidationHelper.IsValidMessageType(model.MessageType))
            {
                return(BadRequestResult(SMS));
            }

            var client = new AmazonPinpointClient(_awsCredentials, RegionEndpoint.GetBySystemName(_awsSettings.Region));

            SendMessagesRequest sendRequest =
                SendMessageRequest(SMS, model.DestinationNumber);

            sendRequest.MessageRequest.MessageConfiguration = new DirectMessageConfiguration
            {
                SMSMessage = new SMSMessage
                {
                    Body        = model.MessageBody,
                    MessageType =
                        model.MessageType
                        .ToUpper(),     //messageType can be TRANSACTIONAL or PROMOTIONAL
                }
            };
            try
            {
                SendMessagesResponse response = await client.SendMessagesAsync(sendRequest);

                ((IDisposable)client).Dispose();
                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    return(BadRequestResult(SMS));
                }

                if (response.MessageResponse.Result[model.DestinationNumber].StatusCode != StatusCodes.Status200OK)
                {
                    return(BadRequestResult(SMS));
                }
            }
            catch
            {
                ((IDisposable)client).Dispose();
                return(BadRequestResult(SMS));
            }
            return(new OkObjectResult(new { success = true, message = $"{SMS} sent." }));
        }
Beispiel #3
0
        private static async Task SendMessage()
        {
            using (AmazonPinpointClient client = new AmazonPinpointClient(RegionEndpoint.GetBySystemName(region)))
            {
                var sendRequest = new SendMessagesRequest
                {
                    ApplicationId  = appId,
                    MessageRequest = new MessageRequest
                    {
                        Addresses = new Dictionary <string, AddressConfiguration>
                        {
                            {
                                destinationNumber,
                                new AddressConfiguration
                                {
                                    ChannelType = "SMS"
                                }
                            }
                        },
                        MessageConfiguration = new DirectMessageConfiguration
                        {
                            SMSMessage = new SMSMessage
                            {
                                Body              = message,
                                MessageType       = messageType,
                                OriginationNumber = originationNumber,
                                SenderId          = senderId,
                                Keyword           = registeredKeyword
                            }
                        }
                    }
                };
                try
                {
                    Console.WriteLine("Sending message...");
                    var response = await client.SendMessagesAsync(sendRequest).ConfigureAwait(false);

                    Console.WriteLine("Message sent!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("The message wasn't sent. Error message: " + ex.Message);
                }
            }
        }
Beispiel #4
0
        public async Task <IActionResult> SendEmail([FromBody] EmailModel model)
        {
            if (model == null || string.IsNullOrWhiteSpace(model.SubjectBody) || string.IsNullOrWhiteSpace(model.ToAddress) ||
                (string.IsNullOrWhiteSpace(model.HtmlBody) && string.IsNullOrWhiteSpace(model.TextBody)))
            {
                return(BadRequestResult(EMAIL));
            }

            if (!ValidationHelper.IsValidEmailAddress(model.ToAddress))
            {
                return(BadRequestResult(EMAIL));
            }

            var client = new AmazonPinpointClient(_awsCredentials, RegionEndpoint.GetBySystemName(_awsSettings.Region));

            SendMessagesRequest sendRequest =
                SendMessageRequest(EMAIL.ToUpper(), model.ToAddress);

            sendRequest.MessageRequest.MessageConfiguration = new DirectMessageConfiguration
            {
                EmailMessage = new EmailMessage
                {
                    FromAddress = _awsSettings.AwsEmail.SenderAddress,
                    SimpleEmail = new SimpleEmail
                    {
                        HtmlPart = string.IsNullOrWhiteSpace(model.HtmlBody)
                            ? null
                            : new SimpleEmailPart
                        {
                            Charset = _awsSettings.AwsEmail.CharSet,
                            Data    = model.HtmlBody
                        },
                        TextPart = !string.IsNullOrWhiteSpace(model.HtmlBody)
                            ? null
                            : new SimpleEmailPart
                        {
                            Charset = _awsSettings.AwsEmail.CharSet,
                            Data    = model.TextBody
                        },
                        Subject = new SimpleEmailPart
                        {
                            Charset = _awsSettings.AwsEmail.CharSet,
                            Data    = model.SubjectBody
                        }
                    }
                }
            };
            try
            {
                SendMessagesResponse response = await client.SendMessagesAsync(sendRequest);

                ((IDisposable)client).Dispose();
                if (response.MessageResponse.Result[model.ToAddress].StatusCode != StatusCodes.Status200OK)
                {
                    return(BadRequestResult(EMAIL));
                }

                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    return(BadRequestResult(EMAIL));
                }
            }
            catch
            {
                ((IDisposable)client).Dispose();
                return(BadRequestResult("Email"));
            }
            return(new OkObjectResult(new { success = true, message = "Email sent." }));
        }