阿里短信单发,批量发送

1.导入

        
        
            com.aliyun
            aliyun-java-sdk-core
            4.0.3
        

单条发送

	/**
	 * 阿里云短信发送
	 */
	public  String sendALMSM(String content, String mobile,String temNo) {
		DefaultProfile profile = DefaultProfile.getProfile(PropertiesUtils.getInstance().getProperty("regionId"), PropertiesUtils.getInstance().getProperty("accessKeyId"), PropertiesUtils.getInstance().getProperty("secret"));
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", PropertiesUtils.getInstance().getProperty("regionId"));
		request.putQueryParameter("PhoneNumbers", mobile); //接收短信的手机号码。
		request.putQueryParameter("SignName", PropertiesUtils.getInstance().getProperty("signName"));//短信签名名称
		request.putQueryParameter("TemplateCode", temNo);//短信模板ID
		request.putQueryParameter("TemplateParam", content);//短信模板变量对应的实际值,JSON格式 {"code":"1111"}

		CommonResponse response= null;
		try {
			response = client.getCommonResponse(request);
			System.out.println("短信接口返回的数据----------------");
			System.out.println(response.getData());

		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return response.getData();
	}

 

批量发送

public static String sendALMSMPl(String mobile,String SignNameJson,String temNo,String content) {
		DefaultProfileprofile = DefaultProfile.getProfile("cn-hangzhou", "", "");
		IAcsClient client = new DefaultAcsClient(profile);

	        CommonRequest request = new CommonRequest();
	        request.setMethod(MethodType.POST);
	        request.setDomain("dysmsapi.aliyuncs.com");
	        request.setVersion("2017-05-25");
	        request.setAction("SendBatchSms");
	        request.putQueryParameter("RegionId", "cn-hangzhou");
	        request.putQueryParameter("PhoneNumberJson", mobile);
	        request.putQueryParameter("SignNameJson", SignNameJson);
	        request.putQueryParameter("TemplateCode", temNo);
	        request.putQueryParameter("TemplateParamJson", content);
	        CommonResponse response= null;
	        try {
	             response = client.getCommonResponse(request);
	            System.out.println(response.getData());
	        } catch (ServerException e) {
	            e.printStackTrace();
	        } catch (ClientException e) {
	            e.printStackTrace();
	        }
	        return response.getData();
	}

 

                      

你可能感兴趣的:(阿里短信)