使用AWS SNS发送短信 - .Net Core

时间:2017-05-19 20:00:46

标签: amazon-web-services sms .net-core

我正在尝试使用C#/ .Net Core来使用AWS全球消息传递服务。

但是,我的电话号码中没有收到该消息。以下是代码:

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;
        }

我在main函数中调用了这个方法:

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

如果有人能帮助我,我感激不尽。提前谢谢

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
 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);

    }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

以下功能对我有用。 确保您喜欢的帐户提供的凭据在AWS IAM控制台中具有SNS完全访问权限

    public static async Task SendSMS(AWSCredentials basicCred, string phoneNum, string message, string smsType= "Promotional")
    { 
            AmazonSimpleNotificationServiceClient snsClient = new AmazonSimpleNotificationServiceClient(basicCred, Amazon.RegionEndpoint.APSoutheast2);
            PublishRequest pubRequest = new PublishRequest();

            pubRequest.Message = message;
            // add optional MessageAttributes, for example:
            pubRequest.MessageAttributes.Add("AWS.SNS.SMS.SenderID", new MessageAttributeValue
            { StringValue = "SSystems", DataType = "String" });
            pubRequest.MessageAttributes.Add("AWS.SNS.SMS.MaxPrice", new MessageAttributeValue
            { StringValue = "0.50", DataType = "Number" });

            pubRequest.PhoneNumber = phoneNum;
            pubRequest.MessageAttributes.Add("AWS.SNS.SMS.SMSType", new MessageAttributeValue
            { StringValue = smsType, DataType = "String" });

            PublishResponse pubResponse = await snsClient.PublishAsync(pubRequest);
            Console.WriteLine(pubResponse.MessageId);
            Console.ReadLine();
        }