コード例 #1
0
        public async Task SendingTwoSmsMessages()
        {
            SmsClient client = CreateSmsClient();

            try
            {
                SmsSendResult firstMessageResult = await client.SendAsync(
                    from : TestEnvironment.FromPhoneNumber,
                    to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");

                SmsSendResult secondMessageResult = await client.SendAsync(
                    from : TestEnvironment.FromPhoneNumber,
                    to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");

                Assert.AreNotEqual(firstMessageResult.MessageId, secondMessageResult.MessageId);
                assertHappyPath(firstMessageResult);
                assertHappyPath(secondMessageResult);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(ex.Message);
                Assert.Fail($"Unexpected error: {ex}");
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
コード例 #2
0
        public async Task SendingSmsMessageFromFakeNumber()
        {
            SmsClient client = InstrumentClient(
                new SmsClient(
                    TestEnvironment.LiveTestConnectionString,
                    InstrumentClientOptions(new SmsClientOptions())));

            try
            {
                SmsSendResult result = await client.SendAsync(
                    from : "+15550000000",
                    to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");
            }
            catch (RequestFailedException ex)
            {
                Assert.IsNotEmpty(ex.Message);
                Assert.True(ex.Message.Contains("400"));
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
コード例 #3
0
        public async Task SendingSmsMessageToFakeNumber()
        {
            SmsClient client = InstrumentClient(
                new SmsClient(
                    TestEnvironment.LiveTestConnectionString,
                    InstrumentClientOptions(new SmsClientOptions())));

            try
            {
                SmsSendResult result = await client.SendAsync(
                    from : TestEnvironment.FromPhoneNumber,
                    to : "+15550000000",
                    message : "Hi");

                Assert.AreEqual(400, result.HttpStatusCode);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(ex.Message);
                Assert.Fail($"Unexpected error: {ex}");
            }

            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
コード例 #4
0
        public async Task SendingSmsMessage()
        {
            SmsClient client = InstrumentClient(
                new SmsClient(
                    TestEnvironment.LiveTestConnectionString,
                    InstrumentClientOptions(new SmsClientOptions())));

            try
            {
                SmsSendResult result = await client.SendAsync(
                    from : TestEnvironment.FromPhoneNumber,
                    to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");

                Console.WriteLine($"Sms id: {result.MessageId}");
                assertHappyPath(result);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(ex.Message);
                Assert.Fail($"Unexpected error: {ex}");
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
コード例 #5
0
        public async Task SendingSmsMessage()
        {
            SmsClient client = InstrumentClient(
                new SmsClient(
                    TestEnvironment.ConnectionString,
                    InstrumentClientOptions(new SmsClientOptions())));

            #region Snippet:Azure_Communication_Sms_Tests_Troubleshooting
            try
            {
                #region Snippet:Azure_Communication_Sms_Tests_SendAsync
                SmsSendResult result = await client.SendAsync(
                    //@@ from: "+18001230000" // Phone number acquired on your Azure Communication resource
                    //@@ to: "+18005670000",
                    /*@@*/ from : TestEnvironment.FromPhoneNumber,
                    /*@@*/ to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");

                Console.WriteLine($"Sms id: {result.MessageId}");
                #endregion Snippet:Azure_Communication_Sms_Tests_SendAsync
                /*@@*/ Assert.IsFalse(string.IsNullOrWhiteSpace(result.MessageId));
                /*@@*/ Assert.AreEqual(202, result.HttpStatusCode);
                /*@@*/ Assert.IsTrue(result.Successful);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(ex.Message);
            }
            #endregion Snippet:Azure_Communication_Sms_Tests_Troubleshooting
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
        private static void Main()
        {
            var       connectionString = "<connection-string>"; // Find your Communication Services resource in the Azure portal
            SmsClient smsClient        = new SmsClient(connectionString);

            SmsSendResult sendResult = smsClient.Send(
                from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
                to: "<to-phone-number>",     // E.164 formatted recipient phone number
                message: "Hello 👋🏻");

            Console.WriteLine($"Message id {sendResult.MessageId}");

            Response <IReadOnlyList <SmsSendResult> > response = smsClient.Send(
                from: "<from-phone-number>",
                to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }, // E.164 formatted recipient phone numbers
                message: "Hello 👋🏻",
                options: new SmsSendOptions(enableDeliveryReport: true)            // OPTIONAL
            {
                Tag = "greeting",                                                  // custom tags
            });

            IEnumerable <SmsSendResult> results = response.Value;

            foreach (SmsSendResult result in results)
            {
                Console.WriteLine($"Sms id: {result.MessageId}");
                Console.WriteLine($"Send Result Successful: {result.Successful}");
            }
        }
コード例 #7
0
        public async Task SendingSmsMessage()
        {
            SmsClient client = InstrumentClient(
                new SmsClient(
                    TestEnvironment.ConnectionString,
                    InstrumentClientOptions(new SmsClientOptions())));

            #region Snippet:Azure_Communication_Sms_Tests_Troubleshooting
            try
            {
                SmsSendResult result = await client.SendAsync(
                    //@@ from: "<from-phone-number>" // Your E.164 formatted phone number used to send SMS
                    //@@ to: "<to-phone-number>", // E.164 formatted recipient phone number
                    /*@@*/ from : TestEnvironment.FromPhoneNumber,
                    /*@@*/ to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");

                Console.WriteLine($"Sms id: {result.MessageId}");
                /*@@*/ assertHappyPath(result);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(ex.Message);
            }
            #endregion Snippet:Azure_Communication_Sms_Tests_Troubleshooting
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
コード例 #8
0
        public async Task SendingSmsMessageUsingTokenCredential()
        {
            SmsClient client = CreateSmsClientWithToken();

            try
            {
                Response <SmsSendResult> response = await client.SendAsync(
                    from : TestEnvironment.FromPhoneNumber,
                    to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");

                SmsSendResult result = response.Value;
                Console.WriteLine($"Sms id: {result.MessageId}");
                assertHappyPath(result);
                assertRawResponseHappyPath(response.GetRawResponse().ContentStream ?? new MemoryStream());
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(ex.Message);
                Assert.Fail($"Unexpected error: {ex}");
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
        public SmsSendResult SendSms(Uri resourceEndpoint, string from, string to, string message)
        {
            SmsClient     smsClient  = new SmsClient(resourceEndpoint, this.credential);
            SmsSendResult sendResult = smsClient.Send(
                from: from,
                to: to,
                message: message,
                new SmsSendOptions(enableDeliveryReport: true)  // optional
                );

            return(sendResult);
        }
コード例 #10
0
        public async Task SendingSMSMessage()
        {
            SmsClient smsClient = CreateSmsClient();

            #region Snippet:Azure_Communication_Sms_Tests_SendAsync
            SmsSendResult sendResult = await smsClient.SendAsync(
                from : "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
                to : "<to-phone-number>",     // E.164 formatted recipient phone number
                message : "Hi");

            Console.WriteLine($"Sms id: {sendResult.MessageId}");
            #endregion Snippet:Azure_Communication_Sms_Tests_SendAsync
            Console.WriteLine($"Send Result Successful: {sendResult.Successful}");
        }
コード例 #11
0
        public void SendingSMSMessage()
        {
            SmsClient smsClient = CreateSmsClient();

            #region Snippet:Azure_Communication_Sms_Tests_Send
            SmsSendResult sendResult = smsClient.Send(
                //@@ from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
                //@@ to: "<to-phone-number>", // E.164 formatted recipient phone number
                /*@@*/ from: TestEnvironment.FromPhoneNumber,
                /*@@*/ to: TestEnvironment.ToPhoneNumber,
                message: "Hi");
            Console.WriteLine($"Sms id: {sendResult.MessageId}");
            #endregion Snippet:Azure_Communication_Sms_Tests_SendAsync
            Console.WriteLine($"Send Result Successful: {sendResult.Successful}");
        }
コード例 #12
0
        public async Task SendingSmsFromNullNumberShouldThrow()
        {
            SmsClient client = CreateSmsClient();

            try
            {
                SmsSendResult result = await client.SendAsync(
                    from : null,
                    to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");
            }
            catch (ArgumentNullException ex)
            {
                Assert.AreEqual("from", ex.ParamName);
                return;
            }
            Assert.Fail("SendAsync should have thrown an exception.");
        }
コード例 #13
0
        internal static SmsSendResponse DeserializeSmsSendResponse(JsonElement element)
        {
            IReadOnlyList <SmsSendResult> value = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("value"))
                {
                    List <SmsSendResult> array = new List <SmsSendResult>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(SmsSendResult.DeserializeSmsSendResult(item));
                    }
                    value = array;
                    continue;
                }
            }
            return(new SmsSendResponse(value));
        }
コード例 #14
0
        public async Task SendingSmsMessageUsingTokenCredential()
        {
            TokenCredential tokenCredential;

            if (Mode == RecordedTestMode.Playback)
            {
                tokenCredential = new MockCredential();
            }
            else
            {
                tokenCredential = new DefaultAzureCredential();
            }
            SmsClient client = InstrumentClient(
                new SmsClient(
                    new Uri(ConnectionString.Parse(TestEnvironment.ConnectionString, allowEmptyValues: true).GetRequired("endpoint")),
                    tokenCredential,
                    InstrumentClientOptions(new SmsClientOptions())));

            try
            {
                SmsSendResult result = await client.SendAsync(
                    from : TestEnvironment.FromPhoneNumber,
                    to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");

                Console.WriteLine($"Sms id: {result.MessageId}");
                Assert.IsFalse(string.IsNullOrWhiteSpace(result.MessageId));
                Assert.AreEqual(202, result.HttpStatusCode);
                Assert.IsTrue(result.Successful);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
コード例 #15
0
        public async Task SendingSmsMessageFromUnauthorizedNumber()
        {
            SmsClient client = CreateSmsClient();

            try
            {
                SmsSendResult result = await client.SendAsync(
                    from : "+18007342577",
                    to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");
            }
            catch (RequestFailedException ex)
            {
                Assert.IsNotEmpty(ex.Message);
                Assert.True(ex.Message.Contains("401"));
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
        static void Main(string[] args)
        {
            // You can find your endpoint and access key from your resource in the Azure portal
            // e.g. "https://<RESOURCE_NAME>.communication.azure.com";
            Uri endpoint = new("https://<RESOURCENAME>.communication.azure.com/");

            // We need an instance of the program class to use within this method.
            Program instance = new();

            Console.WriteLine("Retrieving new Access Token, using Managed Identities");
            Response <AccessToken> response = instance.CreateIdentityAndGetTokenAsync(endpoint);

            Console.WriteLine($"Retrieved Access Token: {response.Value.Token}");

            Console.WriteLine("Sending SMS using Managed Identities");

            // You will need a phone number from your resource to send an SMS.
            SmsSendResult result = instance.SendSms(endpoint, "<Your ACS Phone Number>", "<The Phone Number you'd like to send the SMS to.>", "Hello from Managed Identities");

            Console.WriteLine($"Sms id: {result.MessageId}");
            Console.WriteLine($"Send Result Successful: {result.Successful}");
        }
コード例 #17
0
        public async Task SendingSmsMessageUsingTokenCredential()
        {
            TokenCredential tokenCredential;

            if (Mode == RecordedTestMode.Playback)
            {
                tokenCredential = new MockCredential();
            }
            else
            {
                tokenCredential = new DefaultAzureCredential();
            }
            SmsClient client = InstrumentClient(
                new SmsClient(
                    TestEnvironment.LiveTestEndpoint,
                    tokenCredential,
                    InstrumentClientOptions(new SmsClientOptions())));

            try
            {
                SmsSendResult result = await client.SendAsync(
                    from : TestEnvironment.FromPhoneNumber,
                    to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");

                Console.WriteLine($"Sms id: {result.MessageId}");
                assertHappyPath(result);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(ex.Message);
                Assert.Fail($"Unexpected error: {ex}");
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
コード例 #18
0
        public async Task SendingTwoSmsMessages()
        {
            SmsClient client = CreateSmsClient();

            try
            {
                Response <SmsSendResult> firstMessageResponse = await client.SendAsync(
                    from : TestEnvironment.FromPhoneNumber,
                    to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");

                Response <SmsSendResult> secondMessageResponse = await client.SendAsync(
                    from : TestEnvironment.FromPhoneNumber,
                    to : TestEnvironment.ToPhoneNumber,
                    message : "Hi");

                assertRawResponseHappyPath(firstMessageResponse.GetRawResponse().ContentStream ?? new MemoryStream());
                assertRawResponseHappyPath(secondMessageResponse.GetRawResponse().ContentStream ?? new MemoryStream());

                SmsSendResult firstMessageResult  = firstMessageResponse.Value;
                SmsSendResult secondMessageResult = secondMessageResponse.Value;

                Assert.AreNotEqual(firstMessageResult.MessageId, secondMessageResult.MessageId);
                assertHappyPath(firstMessageResult);
                assertHappyPath(secondMessageResult);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine(ex.Message);
                Assert.Fail($"Unexpected error: {ex}");
            }
            catch (Exception ex)
            {
                Assert.Fail($"Unexpected error: {ex}");
            }
        }
コード例 #19
0
 public void assertHappyPath(SmsSendResult sendResult)
 {
     Assert.True(sendResult.Successful);
     Assert.AreEqual(202, sendResult.HttpStatusCode);
     Assert.IsFalse(string.IsNullOrWhiteSpace(sendResult.MessageId));
 }