配置文件
url=jdbc:mysql:///sys
className=com.mysql.jdbc.Driver
username=root
password=123456
maxActive=5
maxWait=5000
initSize=3
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Properties;
import java.util.UUID;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import org.apache.commons.dbcp.BasicDataSource;
import sun.misc.BASE64Encoder;
public class JdbcUtils {
//配置文件的加载
public static BasicDataSource ds;
static{
Properties p = new Properties();
try {
p.load(JdbcUtils.class.getClassLoader().getResourceAsStream("properties/Myconfig.properties"));
ds=new BasicDataSource();
ds.setUrl(p.getProperty("url"));
ds.setUsername(p.getProperty("username"));
ds.setPassword(p.getProperty("password"));
ds.setDriverClassName(p.getProperty("className"));
ds.setMaxActive(Integer.parseInt(p.getProperty("maxActive")));
ds.setMaxWait(Long.parseLong(p.getProperty("maxWait")));
ds.setInitialSize(Integer.parseInt(p.getProperty("initSize")));
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件加载失败.......");
}
}
public static BasicDataSource getDataSource(){
return ds;
}
//密码加密
public static String md5(String str){
String md5Str=null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
BASE64Encoder encode = new BASE64Encoder();
md5Str=encode.encode(md5.digest(str.getBytes("utf-8")));
md5Str=md5Str.substring(1,md5Str.length()-5);
StringBuilder sb = new StringBuilder(md5Str);
md5Str=sb.reverse().toString();
} catch (Exception e) {
e.printStackTrace();
}
return md5Str;
}
//邮件的发送
// email 收件人的地址
// content 发送的邮件内容信息
public static void sendMail(String email,String content){
try{
//创建一个程序与邮件服务器会话对象Session
Properties props = new Properties();
//设置发送的协议
props.setProperty("mail.transport.protocol", "SMTP");
//设置发送邮件的服务器
props.setProperty("mail.host", "smtp.sina.com");
props.setProperty("mail.smtp.auth", "true");
//创建验证器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication(){
//设置发送人的账号和密码
return new PasswordAuthentication("lingliliangjob@sina.com", "");
}
};
//通过服务器的配置和传进去的登录验证对象来创建一个会话
//通过该会话来进行发送邮件
Session session = Session.getInstance(props, auth);
//2.创建一个Message,他相当于是邮件内容
Message message = new MimeMessage(session);
//设置发送者
message.setFrom(new InternetAddress("lingliliangjob@sina.com"));
//设置发送方式和接受者
message.setRecipient(RecipientType.TO, new InternetAddress(email));
//设置邮件发送主题
message.setSubject("用户激活");
//设置邮件内容
message.setContent(content,"text/html;charset=utf-8");
//3创建TransPort用于将邮件发送
Transport.send(message);
}catch(Exception e){
System.out.println("邮件发送失败");
}
}
// 随机的生成一个UUID的字符串
public static String getUUIDStr() {
UUID uuid = UUID.randomUUID();
return uuid.toString().replaceAll("-", "").toUpperCase();
}
}