|
@@ -1,60 +1,34 @@
|
|
package com.yc.education.util;
|
|
package com.yc.education.util;
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
-import java.io.InputStream;
|
|
|
|
-import java.net.URLDecoder;
|
|
|
|
-import java.net.URLEncoder;
|
|
|
|
-import java.util.Random;
|
|
|
|
-
|
|
|
|
-import javax.servlet.http.HttpSession;
|
|
|
|
-
|
|
|
|
import org.apache.commons.httpclient.HttpClient;
|
|
import org.apache.commons.httpclient.HttpClient;
|
|
import org.apache.commons.httpclient.HttpStatus;
|
|
import org.apache.commons.httpclient.HttpStatus;
|
|
import org.apache.commons.httpclient.NameValuePair;
|
|
import org.apache.commons.httpclient.NameValuePair;
|
|
import org.apache.commons.httpclient.URI;
|
|
import org.apache.commons.httpclient.URI;
|
|
import org.apache.commons.httpclient.methods.GetMethod;
|
|
import org.apache.commons.httpclient.methods.GetMethod;
|
|
|
|
|
|
-public class HttpSender{
|
|
|
|
- //用来产生随机验证码的
|
|
|
|
- private static Random rand = new Random();
|
|
|
|
-
|
|
|
|
- private static String[] str = {"0","1","2","3","4","5","6","7","8","9"};
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- //存放产生的随机数
|
|
|
|
- String sms="";
|
|
|
|
- //生成六位数的随机数
|
|
|
|
- StringBuffer buf = new StringBuffer();
|
|
|
|
- for(int i=0;i<6;i++){
|
|
|
|
- buf.append(str[rand.nextInt(10)]);
|
|
|
|
- }
|
|
|
|
- sms= buf.toString();
|
|
|
|
- System.out.println( buf.toString());
|
|
|
|
-
|
|
|
|
- //session.setAttribute("sms", buf.toString());
|
|
|
|
-
|
|
|
|
- String url = "http://www.uoleem.com.cn/api/";// 应用地址
|
|
|
|
- String username = "xxltl";// 账号
|
|
|
|
- String pwd = "xxltl63811727";// 密码
|
|
|
|
- String mobile = "13772164740";// 手机号码,多个号码使用","分割
|
|
|
|
- String content = "【香榭丽涂料】校验码:"+sms+",您正在注册香榭丽会员,请尽快操作!";// 短信内容
|
|
|
|
|
|
+import javax.mail.Message;
|
|
|
|
+import javax.mail.Session;
|
|
|
|
+import javax.mail.Transport;
|
|
|
|
+import javax.mail.internet.InternetAddress;
|
|
|
|
+import javax.mail.internet.MimeMessage;
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.Properties;
|
|
|
|
|
|
- try {
|
|
|
|
- String returnString = HttpSender.batchSend(url, username, pwd, mobile, content);
|
|
|
|
- System.out.println(returnString);
|
|
|
|
- // TODO 处理返回值,参见HTTP协议文档
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- // TODO 处理异常
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+public class HttpSender{
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * uoleem短信服务
|
|
|
|
+ * @param url
|
|
|
|
+ * @param username
|
|
|
|
+ * @param pwd
|
|
|
|
+ * @param mobile
|
|
|
|
+ * @param content
|
|
|
|
+ * @return
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
public static String batchSend(String url, String username, String pwd, String mobile, String content) throws Exception {
|
|
public static String batchSend(String url, String username, String pwd, String mobile, String content) throws Exception {
|
|
HttpClient client = new HttpClient();
|
|
HttpClient client = new HttpClient();
|
|
GetMethod method = new GetMethod();
|
|
GetMethod method = new GetMethod();
|
|
@@ -83,6 +57,124 @@ public class HttpSender{
|
|
} finally {
|
|
} finally {
|
|
method.releaseConnection();
|
|
method.releaseConnection();
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //产品名称:云通信短信API产品,开发者无需替换
|
|
|
|
+ static final String product = "Dysmsapi";
|
|
|
|
+ //产品域名,开发者无需替换
|
|
|
|
+ static final String domain = "dysmsapi.aliyuncs.com";
|
|
|
|
+
|
|
|
|
+ // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
|
|
|
|
+ static final String accessKeyId = "nI7j7sUzmnm3HIGn";
|
|
|
|
+ static final String accessKeySecret = "1ATZnBIGvT6iWN6IIqryAvnUhE23LP";
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /*//发送短信方法
|
|
|
|
+ public static boolean sendSms(String signname, String templatecode, String phone, String paramstring) 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(phone);
|
|
|
|
+ //必填:短信签名-可在短信控制台中找到
|
|
|
|
+ request.setSignName(signname);
|
|
|
|
+ //必填:短信模板-可在短信控制台中找到
|
|
|
|
+ request.setTemplateCode(templatecode);
|
|
|
|
+ //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
|
|
|
|
+ request.setTemplateParam(paramstring);
|
|
|
|
+
|
|
|
|
+ //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
|
|
|
|
+ //request.setSmsUpExtendCode("90997");
|
|
|
|
+
|
|
|
|
+ //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
|
|
|
|
+ request.setOutId("yourOutId");
|
|
|
|
+
|
|
|
|
+ //hint 此处可能会抛出异常,注意catch
|
|
|
|
+ SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
|
|
|
+
|
|
|
|
+ if("OK".equals(sendSmsResponse.getCode())){
|
|
|
|
+ return true;
|
|
|
|
+ }else{
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+*/
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 邮件发送帮助方法
|
|
|
|
+ * @param mailTitle
|
|
|
|
+ * @param mailContent
|
|
|
|
+ * @param re
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public static void sendMail(String mailTitle,String mailContent,String re) throws Exception {
|
|
|
|
+ Properties props = new Properties(); //可以加载一个配置文件
|
|
|
|
+ // 使用smtp:简单邮件传输协议
|
|
|
|
+ props.put("mail.smtp.host", "smtp.mxhichina.com");//存储发送邮件服务器的信息
|
|
|
|
+ props.put("mail.smtp.auth", "true");//同时通过验证
|
|
|
|
+
|
|
|
|
+ Session session = Session.getInstance(props);//根据属性新建一个邮件会话
|
|
|
|
+ //session.setDebug(true); //有他会打印一些调试信息。
|
|
|
|
+
|
|
|
|
+ MimeMessage message = new MimeMessage(session);//由邮件会话新建一个消息对象
|
|
|
|
+ message.setFrom(new InternetAddress("kefu@jzdsh.com"));//设置发件人的地址
|
|
|
|
+ message.setRecipient(Message.RecipientType.TO, new InternetAddress(re));//设置收件人,并设置其接收类型为TO
|
|
|
|
+ message.setSubject(mailTitle);//设置标题
|
|
|
|
+ //设置信件内容
|
|
|
|
+ //message.setText(mailContent); //发送 纯文本 邮件 todo
|
|
|
|
+ message.setContent(mailContent, "text/html;charset=utf-8"); //发送HTML邮件,内容样式比较丰富
|
|
|
|
+ message.setSentDate(new Date());//设置发信时间
|
|
|
|
+ message.saveChanges();//存储邮件信息
|
|
|
|
+
|
|
|
|
+ //发送邮件
|
|
|
|
+ //Transport transport = session.getTransport("smtp");
|
|
|
|
+ Transport transport = session.getTransport("smtp");
|
|
|
|
+ transport.connect("kefu@jzdsh.com", "62340162Jzdsh"); //发件人邮箱和密码
|
|
|
|
+ transport.sendMessage(message, message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址
|
|
|
|
+ transport.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 邮件发送帮助方法2
|
|
|
|
+ * @param mailTitle
|
|
|
|
+ * @param mailContent
|
|
|
|
+ * @param re
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public static void sendorMail(String username,String password,String mailTitle,String mailContent,String re) throws Exception {
|
|
|
|
+ Properties props = new Properties(); //可以加载一个配置文件
|
|
|
|
+ // 使用smtp:简单邮件传输协议
|
|
|
|
+ props.put("mail.smtp.host", "smtp.mxhichina.com");//存储发送邮件服务器的信息
|
|
|
|
+ props.put("mail.smtp.auth", "true");//同时通过验证
|
|
|
|
+
|
|
|
|
+ Session session = Session.getInstance(props);//根据属性新建一个邮件会话
|
|
|
|
+ //session.setDebug(true); //有他会打印一些调试信息。
|
|
|
|
+
|
|
|
|
+ MimeMessage message = new MimeMessage(session);//由邮件会话新建一个消息对象
|
|
|
|
+ message.setFrom(new InternetAddress(username));//设置发件人的地址
|
|
|
|
+ message.setRecipient(Message.RecipientType.TO, new InternetAddress(re));//设置收件人,并设置其接收类型为TO
|
|
|
|
+ message.setSubject(mailTitle);//设置标题
|
|
|
|
+ //设置信件内容
|
|
|
|
+ //message.setText(mailContent); //发送 纯文本 邮件 todo
|
|
|
|
+ message.setContent(mailContent, "text/html;charset=utf-8"); //发送HTML邮件,内容样式比较丰富
|
|
|
|
+ message.setSentDate(new Date());//设置发信时间
|
|
|
|
+ message.saveChanges();//存储邮件信息
|
|
|
|
+
|
|
|
|
+ //发送邮件
|
|
|
|
+ //Transport transport = session.getTransport("smtp");
|
|
|
|
+ Transport transport = session.getTransport("smtp");
|
|
|
|
+ transport.connect(username, password); //发件人邮箱和密码
|
|
|
|
+ transport.sendMessage(message, message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址
|
|
|
|
+ transport.close();
|
|
|
|
+ System.err.println("这儿执行了没啊 ");
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|