如何利用阿里云市场购买并使用短信服务

本文涉及的产品
数字短信套餐包(仅限零售电商行业),100条 12个月
国际/港澳台短信套餐包,全球plus 100条 6个月
短信服务,200条 3个月
简介: 如何利用阿里云市场购买并使用短信服务

就在前几天,谷粒学院项目又遇到了这样一个需求,我就开始忙着申请、开通这个业务,最后得知:个人用户无法使用短信服务,必须是公司或者上线的业务才可申请 ,这就让我们这些苦逼的大学生为难了。 之后我尝试过 购买服务器搭建网站 再申请、转腾讯云、公众号申请等沙雕操作,最终都以失败告终!💔💔💔


后来某一天来自佛祖的运气,让我通过一个弹幕得知,可以在阿里云 云市场购买其他云的短信服务,😘😘😘 妙极了!


❤️❤️❤️下面介绍如何使用阿里云时长购买并使用短信服务的详细流程!❤️❤️❤️


使用步骤


1、进入云市场官网 https://market.aliyun.com/


d5724fbeca3b1f29241399aae7337abe.png


2、搜索对应的产品


1e9fbc7db08b53e5871a6e07faddd790.png


3、进行购买


d7c3d6964fc4ab2f46052a4c74a08e61.png


4、购买成功后可以进入控制台进行查看


4473d6eb36950f209f43de3fb2ebde62.png


d2d7d525b06aee334d3695ceab802ff3.png


35866355841ed7ddc86a1d3a7bdc24f3.png


5、如何使用


查看购买下方的指南



24e6f0ffc2d9d2a19ef0fc983f78c94d.png

26d39b57caf1717e989fdc2e364e0f6b.png

933a281d7be87ba90ca4be9e8dc21143.png


6、查看短信模板


d6de987439b3d4a7d52ca827ca334e4d.png


现在我们 mobile,templateId,param(后序自己进行字符串拼接)都有了,就差smsId了


7、联系客服进行身份认证,申请smsId.


如何申请: http://help.guoyangyun.com/Problem/Certification.html


b4b1b7d61258990d03c8c7cd2c37c956.png


客服的服务态度绝对是五星的,回复的very quickly,也很有耐心.


d6f770a1d13afd3d60d4a64feee6b449.png


认证成功后,客服会发送自己一个smsId:xxxxxxxxx


8、使用代码开始发送短信


90987e1cf8152ab87ed9a9c7e781be9d.png


具体代码

下面贴出Java发送验证码的详细代码

  • 依赖 pom.xml
<dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.15</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.2.1</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-util</artifactId>
        <version>9.3.7.v20160115</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>


  • httpUtils
package com.rg.sms;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class HttpUtils {
    /**
     * get
     *
     * @param host
     * @param path
     * @param method
     * @param headers
     * @param querys
     * @return
     * @throws Exception
     */
    public static HttpResponse doGet(String host, String path, String method,
                                     Map <String, String> headers,
                                     Map <String, String> querys)
            throws Exception {
        HttpClient httpClient = wrapClient(host);
        HttpGet request = new HttpGet(buildUrl(host, path, querys));
        for (Map.Entry <String, String> e : headers.entrySet()) {
            request.addHeader(e.getKey(), e.getValue());
        }
        return httpClient.execute(request);
    }
    /**
     * post form
     *
     * @param host
     * @param path
     * @param method
     * @param headers
     * @param querys
     * @param bodys
     * @return
     * @throws Exception
     */
    public static HttpResponse doPost(String host, String path, String method,
                                      Map <String, String> headers,
                                      Map <String, String> querys,
                                      Map <String, String> bodys)
            throws Exception {
        HttpClient httpClient = wrapClient(host);
        HttpPost request = new HttpPost(buildUrl(host, path, querys));
        for (Map.Entry <String, String> e : headers.entrySet()) {
            request.addHeader(e.getKey(), e.getValue());
        }
        if (bodys != null) {
            List <NameValuePair> nameValuePairList = new ArrayList <NameValuePair>();
            for (String key : bodys.keySet()) {
                nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
            }
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
            formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
            request.setEntity(formEntity);
        }
        return httpClient.execute(request);
    }
    /**
     * Post String
     *
     * @param host
     * @param path
     * @param method
     * @param headers
     * @param querys
     * @param body
     * @return
     * @throws Exception
     */
    public static HttpResponse doPost(String host, String path, String method,
                                      Map <String, String> headers,
                                      Map <String, String> querys,
                                      String body)
            throws Exception {
        HttpClient httpClient = wrapClient(host);
        HttpPost request = new HttpPost(buildUrl(host, path, querys));
        for (Map.Entry <String, String> e : headers.entrySet()) {
            request.addHeader(e.getKey(), e.getValue());
        }
        if (StringUtils.isNotBlank(body)) {
            request.setEntity(new StringEntity(body, "utf-8"));
        }
        return httpClient.execute(request);
    }
    /**
     * Post stream
     *
     * @param host
     * @param path
     * @param method
     * @param headers
     * @param querys
     * @param body
     * @return
     * @throws Exception
     */
    public static HttpResponse doPost(String host, String path, String method,
                                      Map <String, String> headers,
                                      Map <String, String> querys,
                                      byte[] body)
            throws Exception {
        HttpClient httpClient = wrapClient(host);
        HttpPost request = new HttpPost(buildUrl(host, path, querys));
        for (Map.Entry <String, String> e : headers.entrySet()) {
            request.addHeader(e.getKey(), e.getValue());
        }
        if (body != null) {
            request.setEntity(new ByteArrayEntity(body));
        }
        return httpClient.execute(request);
    }
    /**
     * Put String
     *
     * @param host
     * @param path
     * @param method
     * @param headers
     * @param querys
     * @param body
     * @return
     * @throws Exception
     */
    public static HttpResponse doPut(String host, String path, String method,
                                     Map <String, String> headers,
                                     Map <String, String> querys,
                                     String body)
            throws Exception {
        HttpClient httpClient = wrapClient(host);
        HttpPut request = new HttpPut(buildUrl(host, path, querys));
        for (Map.Entry <String, String> e : headers.entrySet()) {
            request.addHeader(e.getKey(), e.getValue());
        }
        if (StringUtils.isNotBlank(body)) {
            request.setEntity(new StringEntity(body, "utf-8"));
        }
        return httpClient.execute(request);
    }
    /**
     * Put stream
     *
     * @param host
     * @param path
     * @param method
     * @param headers
     * @param querys
     * @param body
     * @return
     * @throws Exception
     */
    public static HttpResponse doPut(String host, String path, String method,
                                     Map <String, String> headers,
                                     Map <String, String> querys,
                                     byte[] body)
            throws Exception {
        HttpClient httpClient = wrapClient(host);
        HttpPut request = new HttpPut(buildUrl(host, path, querys));
        for (Map.Entry <String, String> e : headers.entrySet()) {
            request.addHeader(e.getKey(), e.getValue());
        }
        if (body != null) {
            request.setEntity(new ByteArrayEntity(body));
        }
        return httpClient.execute(request);
    }
    /**
     * Delete
     *
     * @param host
     * @param path
     * @param method
     * @param headers
     * @param querys
     * @return
     * @throws Exception
     */
    public static HttpResponse doDelete(String host, String path, String method,
                                        Map <String, String> headers,
                                        Map <String, String> querys)
            throws Exception {
        HttpClient httpClient = wrapClient(host);
        HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
        for (Map.Entry <String, String> e : headers.entrySet()) {
            request.addHeader(e.getKey(), e.getValue());
        }
        return httpClient.execute(request);
    }
    private static String buildUrl(String host, String path, Map <String, String> querys) throws UnsupportedEncodingException {
        StringBuilder sbUrl = new StringBuilder();
        sbUrl.append(host);
        if (!StringUtils.isBlank(path)) {
            sbUrl.append(path);
        }
        if (null != querys) {
            StringBuilder sbQuery = new StringBuilder();
            for (Map.Entry <String, String> query : querys.entrySet()) {
                if (0 < sbQuery.length()) {
                    sbQuery.append("&");
                }
                if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
                    sbQuery.append(query.getValue());
                }
                if (!StringUtils.isBlank(query.getKey())) {
                    sbQuery.append(query.getKey());
                    if (!StringUtils.isBlank(query.getValue())) {
                        sbQuery.append("=");
                        sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
                    }
                }
            }
            if (0 < sbQuery.length()) {
                sbUrl.append("?").append(sbQuery);
            }
        }
        return sbUrl.toString();
    }
    private static HttpClient wrapClient(String host) {
        HttpClient httpClient = new DefaultHttpClient();
        if (host.startsWith("https://")) {
            sslClient(httpClient);
        }
        return httpClient;
    }
    private static void sslClient(HttpClient httpClient) {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] xcs, String str) {
                }
                public void checkServerTrusted(X509Certificate[] xcs, String str) {
                }
            };
            ctx.init(null, new TrustManager[]{tm}, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx);
            ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            ClientConnectionManager ccm = httpClient.getConnectionManager();
            SchemeRegistry registry = ccm.getSchemeRegistry();
            registry.register(new Scheme("https", 443, ssf));
        } catch (KeyManagementException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchAlgorithmException ex) {
            throw new RuntimeException(ex);
        }
    }
}


  • SMSUtils类
package com.rg.sms;
import org.apache.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
/**
 * @author lxy
 * @version 1.0
 * @Description
 * @date 2022/3/6 14:35
 */
public class SmsUtil {
    /**
     *
     * @param mobile  手机号
     * @param code    发送的验证码
     * @param minute  多长时间有效
     * @param smsSignId  签名id
     * @param templateId 模板id
     * @param minute
     */
    public static void sendMessage(String appCode,String mobile,String code,String minute,String smsSignId,String templateId){
        String host = "https://gyytz.market.alicloudapi.com";
        String path = "/sms/smsSend";
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appCode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", mobile);
        querys.put("param", "**code**:"+code+",**minute**:"+minute);
        querys.put("smsSignId", smsSignId);
        querys.put("templateId", templateId);
        Map <String, String> bodys = new HashMap <String, String>();
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从12
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


  • Demo
/**
 * @author lxy
 * @version 1.0
 * @Description
 * @date 2022/3/6 15:19
 */
public class Demo {
    public static void main(String[] args) {
        SmsUtil.sendMessage("xxxxxx","xxxxxx","999999","30","xxxxxxxxxxxx","a09602b817fd47e59e7c6e603d3f088d");
    }
}


查看发送结果:

image.png


image.png





相关文章
|
3月前
|
API
阿里云短信服务文档与实际API不符
阿里云短信服务文档与实际API不符
|
4月前
|
数据采集 监控 安全
阿里云短信服务+图形认证,有效降低验证码盗刷概率
阿里云短信服务+图形认证服务,有效降低验证码盗刷概率。
357 3
阿里云短信服务+图形认证,有效降低验证码盗刷概率
|
3天前
|
小程序
如何提升审核通过率?|阿里云短信服务
提升短信审核率的 tips 都在这里了哦!
|
24天前
|
安全 小程序
|
2月前
|
安全 Java API
【三方服务集成】最新版 | 阿里云短信服务SMS使用教程(包含支持单双参数模板的工具类,拿来即用!)
阿里云短信服务提供API/SDK和控制台调用方式,支持验证码、通知、推广等短信类型。需先注册阿里云账号并实名认证,然后在短信服务控制台申请资质、签名和模板,并创建AccessKey。最后通过Maven引入依赖,使用工具类发送短信验证码。
【三方服务集成】最新版 | 阿里云短信服务SMS使用教程(包含支持单双参数模板的工具类,拿来即用!)
|
2月前
|
API
如何使用控制台群发短信 | 阿里云短信服务
操作指南|通过控制台群发短信
142 7
|
5月前
|
数据采集 存储 监控
99%成功率背后:阿里云短信服务有何优势?
为什么短信会发送失败,如何提高短信发送成功率,本文将为您介绍短信发送成功率和阿里云短信服务如何保障企业短信稳定送达等相关知识。
230 1
99%成功率背后:阿里云短信服务有何优势?
|
4月前
|
存储 NoSQL Java
|
5月前
|
存储 安全 网络安全
|
5月前
|
小程序
阿里云短信签名申请流程,有图,短信接入新手教程
阿里云短信签名是短信中的标识信息,如【阿里云】,用于表明发送方身份。申请流程简便:登录阿里云短信服务控制台,选择签名管理并添加签名,填写相关信息。审核通常2小时内完成。个人用户每日限申请一个通用签名,企业用户数量不限。已通过审核的签名不可更改名称,仅能调整其他信息并重新提交审核。更多详情及FAQ