使用Amazon AWS SNS 發送 SMS 消息 .net

1.瀏覽aws 開發人員指南

https://docs.aws.amazon.com/zh_cn/sns/latest/dg/sms_publish-to-phone.htmlhtml

2.安裝 aws sms net api : AWSSDK.SimpleNotificationService

3.調用 AmazonSimpleNotificationServiceClient 發送SMSapi

string defaultSenderID="AppName";
string message = "[AppName] Enter this verification code 000000 in your app now.";//消息
string phoneNumber = "+8618372712159";     //手機號
string defaultSMSType = "Promotional";     //Promotional – 不重要的消息|Transactional – 爲客戶事務處理提供支持的重要消息
SMS_Entity mode = new SMS_Entity() { Contents = message, MobileNumber = phoneNumber };
try
{
    AmazonSimpleNotificationServiceClient snsClient = new AmazonSimpleNotificationServiceClient(awsAccessKeyId, awsSecretAccessKey, RegionEndpoint.APSoutheast1);
    PublishRequest publishReq = new PublishRequest();
    publishReq.Message = message;
    publishReq.PhoneNumber = phoneNumber;
    publishReq.MessageAttributes = new Dictionary<string, MessageAttributeValue>();
    publishReq.MessageAttributes.Add("AWS.SNS.SMS.SenderID", new MessageAttributeValue() { DataType = "String", StringValue = defaultSenderID });
    publishReq.MessageAttributes.Add("AWS.SNS.SMS.SMSType", new MessageAttributeValue() { DataType = "String", StringValue = defaultSMSType });
    PublishResponse response = snsClient.Publish(publishReq);
    mode.Response = JsonConvert.SerializeObject(response);
    mode.SendStatus = response.HttpStatusCode.ToString();
    if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
        mode.Result = true;
    else
        mode.Result = false;
}
catch (Exception ex)
{
    mode.SendStatus = "error:" + ex.Message;
    mode.Result = false;
}

注意:awsAccessKeyId,awsSecretAccessKey,RegionEndpoint,須要登錄aws的控制檯查看app

相關文章
相關標籤/搜索