短信验证(吉信通),邮箱验证

一:吉信通短信发送平台

官网

使用步骤

1、  可以先和 业务咨询,聊一下公司需要  

2、  查看接口服务 

3、 相关下载,下载编程语言对应代码示例  

JAVA.zip 是 HTTP 调用案例  

Web_java.zip 是 WebService 调用案例  

4、 整合案例代码,编写 SmsUtils 工具类  

提供 sendSmsByHTTP 调用吉信通 HTTP 接口发送短信  

提供 sendSmsByWebService 调用吉信通 WebService 接口发送短信 

HTTP 方式,如果返回内容,以 000 开头,发送成功  
000/Send:1/Consumption:.1/Tmoney:1.1/sid:0917161858851881 
 
WebService 方式,如果返回内容 16 位信息编码(短信编号),发送成功  
0917162054582440 

工具类


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.apache.commons.lang3.RandomStringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
/**
 * 调用吉信通 发短信工具类
 * 
 * @author itcast
 *
 */
public class SmsUtils {
	private static String userid = "seawind";
	private static String pass = "itcast123456";
 
	/**
	 * 调用HTTP 协议方式发送短信
	 */
	public static String sendSmsByHTTP(String mobile, String content)
			throws UnsupportedEncodingException {
		HttpURLConnection httpconn = null;
		String result = "Error";
		StringBuilder sb = new StringBuilder();
		sb.append("http://service.winic.org:8009/sys_port/gateway/index.asp?");
 
		// 以下是参数
		sb.append("id=").append(URLEncoder.encode(userid, "gb2312"));
		sb.append("&pwd=").append(pass);
		sb.append("&to=").append(mobile);
		sb.append("&content=").append(URLEncoder.encode(content, "gb2312"));
		sb.append("&time=").append("");
		try {
			URL url = new URL(sb.toString());
			httpconn = (HttpURLConnection) url.openConnection();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					httpconn.getInputStream()));
			result = rd.readLine();
			rd.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (httpconn != null) {
				httpconn.disconnect();
				httpconn = null;
			}
		}
		return result;
	}
 
	/**
	 * 调用 WebService 协议方式发送短信
	 * 
	 * @param mobiles
	 * @param msg
	 * @return
	 */
	public static String sendSmsByWebService(String mobiles, String msg) {
		String result = "-12";
		try {
			Document doc;
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			dbf.setNamespaceAware(true);
			DocumentBuilder db = dbf.newDocumentBuilder();
			InputStream is = getSoapInputStream(userid, pass, mobiles, msg, "");
			if (is != null) {
				doc = db.parse(is);
				NodeList nl = doc.getElementsByTagName("SendMessagesResult");
				Node n = nl.item(0);
				result = n.getFirstChild().getNodeValue();
				is.close();
			}
			return result;
		} catch (Exception e) {
			System.out.print("SmsSoap.sendSms error:" + e.getMessage());
			return "-12";
		}
	}
 
	private static String getSoapSmssend(String userid, String pass,
			String mobiles, String msg, String time) {
		try {
			String soap = "";
			soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
					+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
					+ "<soap:Body>"
					+ "<SendMessages xmlns=\"http://tempuri.org/\">" + "<uid>"
					+ userid + "</uid>" + "<pwd>" + pass + "</pwd>" + "<tos>"
					+ mobiles + "</tos>" + "<msg>" + msg + "</msg>" + "<otime>"
					+ time + "</otime>" + "</SendMessages>" + "</soap:Body>"
					+ "</soap:Envelope>";
			return soap;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
 
	private static InputStream getSoapInputStream(String userid, String pass,
			String mobiles, String msg, String time) throws Exception {
		URLConnection conn = null;
		InputStream is = null;
		try {
			String soap = getSoapSmssend(userid, pass, mobiles, msg, time);
			if (soap == null) {
				return null;
			}
			try {
 
				URL url = new URL("http://service2.winic.org:8003/Service.asmx");
 
				conn = url.openConnection();
				conn.setUseCaches(false);
				conn.setDoInput(true);
				conn.setDoOutput(true);
				conn.setRequestProperty("Content-Length",
						Integer.toString(soap.length()));
				conn.setRequestProperty("Content-Type",
						"text/xml; charset=utf-8");
				conn.setRequestProperty("HOST", "service2.winic.org");
				conn.setRequestProperty("SOAPAction",
						"\"http://tempuri.org/SendMessages\"");
 
				OutputStream os = conn.getOutputStream();
				OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
				osw.write(soap);
				osw.flush();
			} catch (Exception ex) {
				System.out.print("SmsSoap.openUrl error:" + ex.getMessage());
			}
			try {
				is = conn.getInputStream();
			} catch (Exception ex1) {
				System.out.print("SmsSoap.getUrl error:" + ex1.getMessage());
			}
 
			return is;
		} catch (Exception e) {
			System.out.print("SmsSoap.InputStream error:" + e.getMessage());
			return null;
		}
	}
 
	public static void main(String[] args) throws IOException {
		String randomCode = RandomStringUtils.randomNumeric(4);
		// System.out.println(sendSmsByHTTP("xxx", randomCode));
		// System.out.println(sendSmsByHTTP("xxx", "尊敬的用户您好,本次获取的验证码为:"
		// + randomCode + ",服务电话:4006184000"));
		System.out.println(sendSmsByWebService("xxx", "尊敬的用户您好,本次获取的验证码为:"
				+ randomCode + ",服务电话:4006184000"));
	}
}

调用工具类发送短信

@Service("smsConsumer")
public class SmsConsumer implements MessageListener {

	@Override
	public void onMessage(Message message) {
		MapMessage mapMessage = (MapMessage) message;

		// 调用SMS服务发送短信
		try {
			// String result =
			// SmsUtils.sendSmsByHTTP(mapMessage.getString("telephone"),
			// mapMessage.getString("msg"));
			String result = "000/xxxx";
			if (result.startsWith("000")) {
				// 发送成功
				System.out.println(
						"发送短信成功,手机号:" + mapMessage.getString("telephone") + 
						",短信内容:" + mapMessage.getString("msg"));
			} else {
				// 发送失败
				throw new RuntimeException("短信发送失败, 短信内容:" + result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

二. 邮件绑定功能 

在pom文件中引入邮箱发送坐标

<!-- 邮件发送工具 -->
<dependency>
	<groupId>javamail</groupId>
	<artifactId>mail</artifactId>
	<version>1.3.2</version>
</dependency>

工具类

​


import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtils {
	private static String smtp_host = "smtp.163.com"; // 网易
	private static String username = "*******@163.com"; // 邮箱账户
	private static String password = "*******"; // 邮箱授权码

	private static String from = "*******@163.com"; // 使用当前账户
	public static String activeUrl = "http://localhost:9003/bos_fore/customer_activeMail";

	public static void sendMail(String subject, String content, String to) {
		Properties props = new Properties();
		props.setProperty("mail.smtp.host", smtp_host);
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.smtp.auth", "true");
		Session session = Session.getInstance(props);
		Message message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(from));
			message.setRecipient(RecipientType.TO, new InternetAddress(to));
			message.setSubject(subject);
			message.setContent(content, "text/html;charset=utf-8");
			Transport transport = session.getTransport();
			transport.connect(smtp_host, username, password);
			transport.sendMessage(message, message.getAllRecipients());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("邮件发送失败...");
		}
	}

	public static void main(String[] args) {
		sendMail("测试邮件", "你好,all  of the world", "*******@163.com");
	}
}

​

​

注意:需要配置客户端授权开启 否则第三方会进行拦截

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值