欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-12 10:03:55
1.浏览aws 开发人员指南 https://docs.aws.amazon.com/zh_cn/sns/latest/dg/sms_publish-to-phone.html 2.安装 aws sms net api : AWSSDK.SimpleNotificationService 3.调用  ......

1.浏览aws 开发人员指南

2.安装 aws sms net api : awssdk.simplenotificationservice

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

3.调用 amazonsimplenotificationserviceclient 发送sms

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的控制台查看