阿里云短信服务(SMS)是一种基于云的通信工具,可以用来发送验证码、通知等短信内容。阿里云短信服务的优势包括高效、稳定和安全。它支持多种编程语言,如Java、Python等,能够快速集成到各种应用中。
应用场景包括:
在发送短信前,需要准备接收短信的手机号码列表。确保这些号码已通过实名认证,并且已同意接收短信通知。
在发送短信之前,确保已经获得了短信签名和模板,并且都已经通过审核。下面是一个简单的Java发送短信示例:
import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.dysmsapi.transform.v20170525.SendSmsResponseUnmarshaller; public class SendSmsDemo { private static final String regionId = "your-region-id"; private static final String accessKeyId = "your-access-key-id"; private static final String accessKeySecret = "your-access-key-secret"; private static final String signName = "your-sign-name"; private static final String templateCode = "your-template-code"; public static void main(String[] args) { DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret); IAcsClient acsClient = new DefaultAcsClient(profile); SendSmsRequest sendSmsRequest = new SendSmsRequest(); sendSmsRequest.setMethod(MethodType.POST); sendSmsRequest.setPhoneNumbers("your-phone-number"); sendSmsRequest.setSignName(signName); sendSmsRequest.setTemplateCode(templateCode); sendSmsRequest.setTemplateParam("{\"code\":\"123456\"}"); try { SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(sendSmsRequest); if ("OK".equals(sendSmsResponse.getCode())) { System.out.println("发送成功"); } else { System.out.println("发送失败:" + sendSmsResponse.getMessage()); } } catch (Exception e) { e.printStackTrace(); } } }短信发送状态查询及调试
下面是一个简单的示例代码,展示如何通过API查询短信发送状态:
import requests def check_sms_status(phone_number, biz_id): url = "https://sms.aliyuncs.com/?Action=QuerySendDetails" params = { "RegionId": "cn-hangzhou", "PhoneNumbers": phone_number, "SendDate": "2023-01-01", "BizId": biz_id, "AccessKeyId": "your-access-key-id", "Signature": "your-signature" } response = requests.get(url, params=params) return response.json() if __name__ == "__main__": phone_number = "your-phone-number" biz_id = "your-biz-id" status = check_sms_status(phone_number, biz_id) print(status)