Send SMS using AWS SNS - .Net Core

时光总嘲笑我的痴心妄想 提交于 2020-01-24 14:34:07

问题


I'm trying to use AWS world wide messaging service using C#/.Net Core.

However I do not receive the message in my phone number. Below is the code:

public static async Task<PublishResponse> sendSMS()
        {

            string accessKey = "my Key";
            string secretAccessKey = "my secret key";
            var client = new AmazonSimpleNotificationServiceClient(accessKey, 
            secretAccessKey, RegionEndpoint.USEast1);

            string phoneNumber = "my number";

            PublishRequest req = new PublishRequest();
            req.Message = "Hellloooo from core";
            req.PhoneNumber = "+2" + phoneNumber;   
            PublishResponse res = await client.PublishAsync(req);
            return res;
        }

And I invoke this method in the main function:

 public static void Main(string[] args)
        {
             var respond = sendSMS();     
        }

I appreciate if anyone could help me with this. thanks in advance


回答1:


 public static async Task<PublishResponse> SendMessageToMobileAsync(string countryCode, string mobileNumber, string message)
    {
        var accessKey = "xxx";
        var secretKey = "xxx";
        var client = new AmazonSimpleNotificationServiceClient(accessKey, secretKey, RegionEndpoint.USEast1);
        var messageAttributes = new Dictionary<string, MessageAttributeValue>();
        var smsType = new MessageAttributeValue
        {
            DataType = "String",
            StringValue = "Transactional"
        };

        messageAttributes.Add("AWS.SNS.SMS.SMSType", smsType);
        
        PublishRequest request = new PublishRequest
        {
            Message = message,
            PhoneNumber = countryCode + mobileNumber,
            MessageAttributes = messageAttributes
        };

        return await client.PublishAsync(request);

    }


来源:https://stackoverflow.com/questions/44078176/send-sms-using-aws-sns-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!