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: 461503821@163.com 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) {
}
}
分享到:
相关推荐
用友软件NC63平台短信发送跟邮件发送配置跟开发文档!
使用java发送短信的方法有两个(一个是使用阿里的短信服务,一个是使用uoleem短信服务),使用java发送邮件的方法只有一个,这个方法支持群发和单发
java发送短信和发送邮件的代码
VFP种实现发送邮件,不带附件,变通给手机发送短信。 如给13980001234@139.com
实现短信/邮件发送,短信邮件服务商通过插件方式集成,需要jms,发送接口在SendNotifyApi.java
微信企业微信短信邮件qq发送消息
异步手机短信和邮件发送消息中心平台构建.docx
编写matlab程序自动发邮件和短信的技巧,里面附有matlab代码
Springboot阿里、腾讯短信和邮件发送service封装,根据配置启用或禁用
主要介绍了iOS开发中实现邮件和短信发送的简单示例,编程语言依然是传统的Objective-C,需要的朋友可以参考下
该包包含了java短信验证功能实现,和需要的架包,以及邮件发送功能
黑马程序员---瑞吉外卖--SpringBoot项目 新增sendMail()方法,用户登录时使用QQ邮件发送验证码,完善用户登录功能。 亲测有效!
打开压缩包里面,manyshipsRemindInit.xml里面配置数据库连接池信息,发送邮件的信箱,发送短信的特服号密码
IOS打电话、发短信、发邮件的封装类,使用非常方便
neil-mailer:用于将批量电子邮件和短信发送到Electric Neil邮件列表的本地脚本
自动发短信邮件脚本
对于账号为139移动邮箱还可以发送邮箱中的手机短信,及大的方便了不支持POP3收发邮件的邮箱实现批量发送邮件功能。邮件内容可以使用HTML邮件编辑。 多线程登录多个邮箱同时发送 ---快发邮件想不快都难。 目前系统...
使用Java进行邮件短信发送
发送短信VI,可以输入手机号码、邮件地址、服务器等信息,实现电脑给手机发短信
该解决方案将利用智能手机的高普及率,主要通过电子邮件和短信发送通知。预期的结果) 项目/产品成功的好处: 用户可以收到有关影响其财产的停电的及时通知, 用户可以选择采取行动来保护自己的资产, 用户可以确定...