1. 程式人生 > >【java】呼叫阿里雲通訊介面發簡訊

【java】呼叫阿里雲通訊介面發簡訊

【前言】

         呼叫簡訊傳送驗證碼的方法阿里雲都寫好了,而且會自動限制單日傳送量,每分鐘一條,同一手機號單日10條等等。安全性很好。而且國際國內都能發。

【介面程式碼】

package com.candyshop.utils.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.dysmsapi.transform.v20170525.SendSmsResponseUnmarshaller;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Created on 2018年5月8日.
 * 
 * 工程依賴了2個jar包(存放在工程的libs目錄下) 1:aliyun-java-sdk-core.jar
 * 2:aliyun-java-sdk-dysmsapi.jar
 *
 * 備註:Demo工程編碼採用UTF-8 用來進行簡訊傳送
 */
public class MessageUtil {

	// 產品名稱:雲通訊簡訊API產品,開發者無需替換
	private static final String product = "Dysmsapi";
	// 產品域名,開發者無需替換
	private static final String domain = "dysmsapi.aliyuncs.com";

	// TODO 此處需要替換成開發者自己的AK(在阿里雲訪問控制檯尋找)
	private static final String accessKeyId = "";
	private static final String accessKeySecret = "";

	public static SendSmsResponse sendSms(String phoneNumber, long identifyCode)
			throws ClientException {

		// 可自助調整超時時間
		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");

		// 初始化acsClient,暫不支援region化
		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",
				accessKeyId, accessKeySecret);
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
				domain);
		IAcsClient acsClient = new DefaultAcsClient(profile);

		// 組裝請求物件-具體描述見控制檯-文件部分內容
		SendSmsRequest request = new SendSmsRequest();
		// 必填:待發送手機號
		request.setPhoneNumbers(phoneNumber);
		// 必填:簡訊簽名-可在簡訊控制檯中找到
		request.setSignName("");
		// 必填:簡訊模板-可在簡訊控制檯中找到
		// request.setTemplateCode("");
		request.setTemplateCode("");

		// 可選:模板中的變數替換JSON串,如模板內容為"親愛的${name},您的驗證碼為${code}"時,此處的值為
		request.setTemplateParam("{\"code\":\"" + identifyCode + "\"}");

		// 選填-上行簡訊擴充套件碼(無特殊需求使用者請忽略此欄位)
		// request.setSmsUpExtendCode("90997");

		// 可選:outId為提供給業務方擴充套件欄位,最終在簡訊回執訊息中將此值帶回給呼叫者
		request.setOutId("yourOutId");

		// hint 此處可能會丟擲異常,注意catch
		SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

		return sendSmsResponse;
	}

	public static QuerySendDetailsResponse querySendDetails(String bizId)
			throws ClientException {

		// 可自助調整超時時間
		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");

		// 初始化acsClient,暫不支援region化
		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",
				accessKeyId, accessKeySecret);
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
				domain);
		IAcsClient acsClient = new DefaultAcsClient(profile);

		// 組裝請求物件
		QuerySendDetailsRequest request = new QuerySendDetailsRequest();
		// 必填-號碼
		request.setPhoneNumber("15000000000");
		// 可選-流水號
		request.setBizId(bizId);
		// 必填-傳送日期 支援30天內記錄查詢,格式yyyyMMdd
		SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
		request.setSendDate(ft.format(new Date()));
		// 必填-頁大小
		request.setPageSize(10L);
		// 必填-當前頁碼從1開始計數
		request.setCurrentPage(1L);

		// hint 此處可能會丟擲異常,注意catch
		QuerySendDetailsResponse querySendDetailsResponse = acsClient
				.getAcsResponse(request);

		return querySendDetailsResponse;
	}

	// TODO 封板測試前幹掉main方法
	public static void main(String[] args) throws ClientException,
			InterruptedException {

		// 發簡訊
		SendSmsResponse response = sendSms("",);
		System.out.println("簡訊介面返回的資料----------------");
		System.out.println("Code=" + response.getCode());
		System.out.println("Message=" + response.getMessage());
		System.out.println("RequestId=" + response.getRequestId());
		System.out.println("BizId=" + response.getBizId());

		Thread.sleep(3000L);

		// 查明細
		if (response.getCode() != null && response.getCode().equals("OK")) {
			QuerySendDetailsResponse querySendDetailsResponse = querySendDetails(response
					.getBizId());
			System.out.println("簡訊明細查詢介面返回資料----------------");
			System.out.println("Code=" + querySendDetailsResponse.getCode());
			System.out.println("Message="
					+ querySendDetailsResponse.getMessage());
			int i = 0;
			for (QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse
					.getSmsSendDetailDTOs()) {
				System.out.println("SmsSendDetailDTO[" + i + "]:");
				System.out.println("Content=" + smsSendDetailDTO.getContent());
				System.out.println("ErrCode=" + smsSendDetailDTO.getErrCode());
				System.out.println("OutId=" + smsSendDetailDTO.getOutId());
				System.out
						.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());
				System.out.println("ReceiveDate="
						+ smsSendDetailDTO.getReceiveDate());
				System.out
						.println("SendDate=" + smsSendDetailDTO.getSendDate());
				System.out.println("SendStatus="
						+ smsSendDetailDTO.getSendStatus());
				System.out.println("Template="
						+ smsSendDetailDTO.getTemplateCode());
			}
			System.out.println("TotalCount="
					+ querySendDetailsResponse.getTotalCount());
			System.out.println("RequestId="
					+ querySendDetailsResponse.getRequestId());
		}

	}
}

【總結】

        不怕不知道,就怕不知道。