网易云发送验证码短信,发送通知短信,java版

网易云中的短信服务

发送验证码短信,创建  MobileMessageSend :

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

public class MobileMessageSend {
    // 发送验证码的请求路径URL
	private static final String SERVER_URL = "https://api.netease.im/sms/sendcode.action";
	private static final String APP_KEY = "";// 账号
	private static final String APP_SECRET = "";// 密钥
	private static final String NONCE = "123456";// 随机数
	private static final String MOULD_ID = "";// 模板ID

	public static String sendMsg(String phone, String mouldid) throws IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost post = new HttpPost(SERVER_URL);
		String curTime = String.valueOf((new Date().getTime() / 1000L));
		String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);

		// 设置请求的header
		post.addHeader("AppKey", APP_KEY);
		post.addHeader("Nonce", NONCE);
		post.addHeader("CurTime", curTime);
		post.addHeader("CheckSum", checkSum);
		post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

		// 设置请求参数
		List<NameValuePair> nameValuePairs = new ArrayList<>();
		nameValuePairs.add(new BasicNameValuePair("mobile", phone));
		nameValuePairs.add(new BasicNameValuePair("templateid", mouldid));
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));

		// 执行请求
		HttpResponse response = httpclient.execute(post);
		String responseEntity = EntityUtils.toString(response.getEntity(), "utf-8");

		// 判断是否发送成功,发送成功返回true
		String code = JSON.parseObject(responseEntity).getString("code");
		if (code.equals("200")) {
			return "success";
		}
		return "error";
	}
}

调用发短信:


//phone 手机号
//mobid 短信模板ID
String str = MobileMessageSend.sendMsg(phone, mobid);

验证码校验,创建  MobileMessageCheck :

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;


public class MobileMessageCheck {

    private static final String SERVER_URL="https://api.netease.im/sms/verifycode.action";//校验验证码的请求路径URL
    private static final String APP_KEY="";//账号
    private static final String APP_SECRET="";//密钥
    private static final String NONCE="123456";//随机数

    public static String checkMsg(String phone,String sum) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost post = new HttpPost(SERVER_URL);

        String curTime=String.valueOf((new Date().getTime()/1000L));
        String checkSum=CheckSumBuilder.getCheckSum(APP_SECRET,NONCE,curTime);

        //设置请求的header
        post.addHeader("AppKey",APP_KEY);
        post.addHeader("Nonce",NONCE);
        post.addHeader("CurTime",curTime);
        post.addHeader("CheckSum",checkSum);
        post.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");

        //设置请求参数
        List<NameValuePair> nameValuePairs =new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("mobile",phone));
        nameValuePairs.add(new BasicNameValuePair("code",sum));


        post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));

        //执行请求
        HttpResponse response=httpclient.execute(post);
        String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");

        //判断是否发送成功,发送成功返回true
        String code= JSON.parseObject(responseEntity).getString("code");
        //System.out.println(code);
        if (code.equals("200")){
            return "success";
        }
        return "error";
    }
}

验证码验证:

//phone 手机号
//code  验证码
String str = MobileMessageCheck.checkMsg(phone, code);

if (str.equals("success")) {
//验证码和手机号 正确
}

=================================  通知类短信 ================================================

创建 SendMsg

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;

/**
 * 发送模板短信请求
 */
public class SendMsg {
	// 发送验证码的请求路径URL
	private static final String SERVER_URL = "https://api.netease.im/sms/sendtemplate.action";
	// 网易云信分配的账号,请替换你在管理后台应用下申请的Appkey
	private static final String APP_KEY = "";
	// 网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret
	private static final String APP_SECRET = "";
	// 随机数
	private static final String NONCE = "123456";
	// 短信模板ID
	private static final String TEMPLATEID = "";
	// 手机号,接收者号码列表,JSONArray格式,限制接收者号码个数最多为100个
	private static final String MOBILES = "['13888888888','13666666666']";
	// 短信参数列表,用于依次填充模板,JSONArray格式,每个变量长度不能超过30字,对于不包含变量的模板,不填此参数表示模板即短信全文内容
	private static final String PARAMS = "['朋友','100','2018年08月20号']";

	public static String sendnotice(String templateid, String mobiles, String params) throws IOException {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(SERVER_URL);
		String curTime = String.valueOf((new Date()).getTime() / 1000L);
		/*
		 * 参考计算CheckSum的java代码,在上述文档的参数列表中,有CheckSum的计算文档示例
		 */
		String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);

		// 设置请求的header
		httpPost.addHeader("AppKey", APP_KEY);
		httpPost.addHeader("Nonce", NONCE);
		httpPost.addHeader("CurTime", curTime);
		httpPost.addHeader("CheckSum", checkSum);
		httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

		// 设置请求的的参数,requestBody参数
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		/*
		 * 1.如果是模板短信,请注意参数mobile是有s的,详细参数配置请参考“发送模板短信文档” 2.参数格式是jsonArray的格式,例如
		 * "['13888888888','13666666666']"
		 * 3.params是根据你模板里面有几个参数,那里面的参数也是jsonArray格式
		 */
		nvps.add(new BasicNameValuePair("templateid", templateid));
		nvps.add(new BasicNameValuePair("mobiles", mobiles));
		nvps.add(new BasicNameValuePair("params", params));

		httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));

		// 执行请求
		HttpResponse response = httpClient.execute(httpPost);
		/*
		 * 1.打印执行结果,打印结果一般会200、315、403、404、413、414、500
		 * 2.具体的code有问题的可以参考官网的Code状态表
		 */
		// System.out.println(EntityUtils.toString(response.getEntity(),
		// "utf-8"));
		String responseEntity = EntityUtils.toString(response.getEntity(), "utf-8");
		// 判断是否发送成功,发送成功返回true
		String code = JSON.parseObject(responseEntity).getString("code");
		System.out.println("返回码:" + code);
		if (code.equals("200")) {
			return "success";
		}
		return "error";
	}

//	public static void main(String[] args) throws IOException {
//		String xx = sendnotice("9505219", "['13888888888']", "[]");
//		System.out.println("返回码:" + xx);
//	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用JavaMail API来发送邮件。以下是一个简单的示例代码: ```java import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class EmailSender { public static void main(String[] args) { // 收件人电子邮箱 String to = "recipient@example.com"; // 发件人电子邮箱 String from = "sender@example.com"; // smtp服务器 String host = "smtp.ym.163.com"; // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); properties.setProperty("mail.smtp.auth", "true"); // 获取默认的Session对象 Session session = Session.getDefaultInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("yourusername", "yourpassword"); // 发件人的账号和密码 } }); try { // 创建默认的 MimeMessage 对象。 MimeMessage message = new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from)); // Set To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: 头部头字段 message.setSubject("Test Email"); // 设置消息体 message.setText("This is a test email"); // 发送消息 Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } } } ``` 请将上述代码中的 `yourusername` 和 `yourpassword` 替换为你的网易企业邮箱的用户名和密码,并将 `recipient@example.com` 替换为收件人的邮箱地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值