在java开发中需要涉及到短信验证的问题,以及邮箱验证的问题,但是前提是要可以发送短信,和邮件。废话不多说,先从发邮件开始吧。
直接上代码(需要依赖一个包
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
package com.my.util;
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
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;
import com.sun.mail.util.MailSSLSocketFactory;
public class SendMail {
public static void main(String[] args) throws MessagingException {
// 配置发送邮件的环境属性
final Properties props = new Properties();
System.out.println("发送邮件开始.....");
/*
* 可用的属性: mail.store.protocol / mail.transport.protocol / mail.host /
* mail.user / mail.from
*/
// 表示SMTP发送邮件,需要进行身份验证
try {
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "465");
// 发件人的账号
props.put("mail.user", "@qq.com");
// 访问SMTP服务时需要提供的密码
props.put("mail.password", "");
// 构建授权信息,用于进行SMTP进行身份验证
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
// 设置收件人
InternetAddress to = new InternetAddress("228@qq.com");
message.setRecipient(RecipientType.TO, to);
// 设置抄送
InternetAddress cc = new InternetAddress("11@qq.com");
System.out.println("抄送");
message.setRecipient(RecipientType.CC, cc);
// 设置密送,其他的收件人不能看到密送的邮件地址
//InternetAddress bcc = new InternetAddress("112@qq.com");
//message.setRecipient(RecipientType.CC, bcc);
// 设置邮件标题
message.setSubject("测试邮件");
// 设置邮件的内容体
message.setContent("<a href='http://www.baidu.com'>测试的HTML邮件事实上我的说法</a>", "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
System.out.println("发送邮件结束.....");
}
}
大概就分为三个步骤:设置邮箱发送的一些配置信息----验证用户----输入对方的账号----运行程序。。具体的步骤都在注释中。
需要注意的是:你使用发邮件的那个邮箱账号需要开启如下图所示的服务:SMTP/IMAP服务。
而且你的密码不是你的QQ密码,而是授权码。即如上图所示的生成授权码,即是你发送邮件要输入的密码。这些都准备好了后基本就可以完成了一个邮件的发送了。至于一下细节的问题,就不在描述了。
下面看下发短信的功能:
发短信需要依赖第三方服务:我选择的是(你要先注册)
该网站有10条免费的短信。可以充值十块钱100条信息,但是发送的内容要按照格式来,具体格式是在下午有具体介绍,不按照格式就是104错误。
直接上代码
package com.my.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
*
* @author PSY
*/
public class SendMessage {
public static String SMS(String postData, String postUrl) {
try {
//发送POST请求
URL url = new URL(postUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", "" + postData.length());
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(postData);
out.flush();
System.out.println("短信发送成功");
out.close();
//获取响应状态
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println("connect failed!");
System.out.println("wwww");
return "";
}
//获取响应内容体
String line, result = "";
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
while ((line = in.readLine()) != null) {
result += line + "\n";
System.out.println(result);
}
in.close();
return result;
} catch (IOException e) {
e.printStackTrace(System.out);
}
return "";
}
public static void main(String[] args) throws InterruptedException {
List <String> phone = new ArrayList<String> ();
phone.add("1762");
for(String p:phone) {
SMS("","http://sms.106jiekou.com/utf8/sms.aspx?password=()&nr=utf-8&mobile="+p+"&account=&content=您的订单编码:"+"为"
+(int)(Math.random()*50+10)
+"。系统验证码为:"+(int)(Math.random()*5000+1000)
+",骚年们请及时输入该验证码。客服电话:。如需帮助请联系客服。");
Thread.sleep(5000);
}
}
官方给的文档是:
注意:我是买了10块钱的100条短信发送。
短信内容模版:您的订单编码:【变量】。如需帮助请联系客服。
实际短信内容:您的订单编码:888888。如需帮助请联系客服。
只有中间红色字体是你要写的内容,其他的不能更改,(String字符串),这是在交钱的情况下可以改变中间的变量。
如果只是免费的十条信息就不能改变严格按照他们给的格式来:(如下所示)
http://sms.106jiekou.com/utf8/sms.aspx?account=9999&password=接口密码&mobile=13900008888&content=您的订单编码:8888。如需帮助请联系客服。(你需要改的就是在url中添加你申请的的用户名和密码,以及你要发的对方号码其他的都不能改)