package com.mes.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Date; 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.MimeMessage; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.mes.payplus.util.weixin.MD5Util; import com.sun.mail.smtp.SMTPAddressFailedException; public class SendEmailUtil { public final static String proxy_url="proxy.zte.com.cn";//代理url public final static String proxy_port="80";//代理端口 private static String email_user; private static String email_password; private static String email_host; private static String email_from; static{ Properties pro = new Properties(); String classPath =SendEmailUtil.class.getClassLoader().getResource("").getPath(); String filePath = classPath + "email.properties"; try { InputStream inStream = new FileInputStream(new File(filePath)); pro.load(inStream); } catch (IOException e) { e.printStackTrace(); } email_user = pro.getProperty("email_user"); email_password = pro.getProperty("email_password"); email_host=pro.getProperty("email_host"); email_from=pro.getProperty("email_from"); } /** * 字符串数组转化成List * @param strs * @return */ public static ArrayList<String> transList(String[] strs){ ArrayList<String> list=new ArrayList<String>(); if(strs.length!=0){ for(int i=0;i<strs.length;i++){ list.add(strs[i]); } } return list; } //发短信 Post方法没有调通 显示必填参数有些没填 但是看接口说明是完全填了的,,,班车管家 public static boolean sendSmsPost(String content,String phoneNum) throws Exception { boolean flag=false; URL url = new URL("http://www.jc-chn.cn/smsSend.do?"); // 设置请求参数 String psd1=MD5Util.MD5Encode("qx7q61pf", null);//47x88ytq String psd2=MD5Util.MD5Encode("zxyh01"+psd1,null); StringBuffer sb = new StringBuffer(); sb.append("username=zxyh01"); sb.append("&password="+psd2); sb.append("&mobile="+phoneNum);//多个电话号码用,分开 sb.append("&content=" + URLEncoder.encode(content, "utf-8")); sb.append("&dstime="); // 通用的请求属性 //配置代理 方便本地 URLConnection conn = url.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setDoOutput(true); conn.setDoInput(true); // 获取connection对象的输出流 PrintWriter out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(sb.toString()); // 定义flush输出流的缓冲 out.flush(); // 定义bufferedReader输入流读取URL响应 BufferedReader in = new BufferedReader(new InputStreamReader( url.openStream())); String line = ""; String result = ""; while ((line = in.readLine()) != null) { result += line + "\n"; } out.close(); in.close(); System.out.println(result); return flag; } //发短信 GeT方法调通 可以使用 public static boolean sendSmsGet(String content,String phoneNum) throws IOException { boolean flag=false; //从配置中读取短信相关信息 String[] strs =changePhoneUser(); //把需要的读取出来 String uri=strs[0]; String user_name=strs[1]; String password=strs[2]; StringBuffer sb = new StringBuffer(uri+"?"); sb.append("username="+user_name); //第一次加密 String psd1=MD5Util.MD5Encode(password, null); //第二次加密 String psd2=MD5Util.MD5Encode(user_name+psd1,null); sb.append("&password="+psd2); sb.append("&mobile="+phoneNum); sb.append("&plaintext=乐享生活"); sb.append("&content="+URLEncoder.encode(content,"utf-8")); sb.append("&dstime="); URL url = new URL(sb.toString()); //配置代理 方便本地测试 URLConnection conn = null; String proxy_set = "false"; if (proxy_set!=null && proxy_set.equals("true")) { //HttpProxy proxy = new HttpProxy("proxy.zte.com.cn",80); System.setProperty("http.proxyHost", proxy_url); System.setProperty("http.proxyPort", proxy_port); conn = url.openConnection(); //conn.connect(); } else { conn = url.openConnection(); } conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.connect(); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String line = ""; String result = ""; while ((line=in.readLine())!=null){ result += line + "\n"; } in.close(); System.out.println("短信发送返回值:"+result); if((!result.contains("-"))&&(!"0".equals(result))){ flag=true; } return flag; } /** * 从配置文件中读取 短信服务器信息 * 动态改变短信服务器 名字 密码 修改位置 email_info.properties */ public static String[] changePhoneUser(){ //Prop prop=new Prop("config/email_info.properties"); /* String user ="zxyh04"; String password="4k0ggxjy";*/ String user ="zxyh01"; String password="qx7q61pf"; String url="http://www.jc-chn.cn/smsSend.do"; String[] strs={url,user,password}; return strs; } /** * 发邮件 */ public static boolean sendEmail(String toEmail, String context, String subject, HttpServletRequest request, HttpServletResponse response) { boolean flag=false; Properties props = new Properties(); Authenticator auth = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(email_user,email_password); } }; props.put("mail.smtp.host", email_host); props.put("mail.smtp.auth", "true"); props.put("mail.from", email_from); props.put("mail.smtp.reportsuccess", true); Session session = Session.getInstance(props, auth); session.setDebug(false); StringBuffer sb = new StringBuffer(); try { MimeMessage msg = new MimeMessage(session); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, toEmail); msg.setSubject(subject); msg.setSentDate(new Date()); msg.setText(context); Transport.send(msg); flag=true; } catch (SMTPAddressFailedException ex) { // 250是发送OK的 捕获到250 Mail OK // 550 User not found: [email protected] 550错误 只能获取到Invalid // Addresses没有发现这个邮箱 int message=ex.getReturnCode(); if(message==250){ flag=true; } System.out.println("SMTPAddressFailedException---------------------------"+ ex.getReturnCode()); System.out.println("SMTPAddressFailedException---------------------------"+ ex.getMessage()); sb.append("SMTPAddressFailedException---------------------------"+ ex.getMessage() + "\t"); } catch (MessagingException mex) { String message=mex.getMessage(); if(message.contains("250")){ flag=true; } System.out.println("MessagingException------" + mex.getMessage()); sb.append("MessagingException------" + mex.getMessage()); } catch (Exception e) { e.getMessage(); } return flag; } public static void main(String[] args) { } }
邮件和短信的发送
猜你喜欢
转载自lijie-insist.iteye.com/blog/2269220
今日推荐
周排行