【问题标题】:Send SMS using AWS SNS - .Net Core使用 AWS SNS 发送 SMS - .Net Core
【发布时间】:2020-04-09 04:48:39
【问题描述】:

我正在尝试使用 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;
        }

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

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

如果有人可以帮助我解决这个问题,我将不胜感激。提前致谢

【问题讨论】:

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


    【解决方案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);
    
        }

    【讨论】:

    • 您应该解释您在此处发布的内容。我知道它是解决 OP 问题的方法,但是这段代码实际上做了什么?它是如何工作的?
    • 此代码可用于通过 AWS SNS 向任何手机号码发送交易短信。您只需要从您的 AWS 账户更改 accessKey 和 secretKey。希望这会有所帮助。
    • 这对最新版本仍然有效吗?我尝试了这段代码并插入了访问密钥和密钥。我无法让它工作,也没有错误消息。只是从不发送。
    【解决方案2】:

    以下功能对我有用。 确保喜欢您提供的凭据的帐户在 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();
            }
    

    【讨论】:

    • 还要确保您用于发送 SMS 消息的区域受 AWS 支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 2017-01-28
    相关资源
    最近更新 更多