package cn.itcast.travel.utils;
import org.apache.commons.mail.HtmlEmail;
/**
* 发送邮件工具类
*/
public final class MailUtil {
/**
* 发送邮件
* 参数一:发送邮件给谁
* 参数二:发送邮件的内容
*/
public static void sendMail(String toEmail, String emailMsg) throws Exception {
//创建HemlEmail对象,封装邮件发送参数
HtmlEmail htmlEmail = new HtmlEmail();
//你的邮件服务器的地址
htmlEmail.setHostName("smtp.163.com");
//注意:参数1--邮箱账号 参数2--邮件发送授权码
htmlEmail.setAuthentication("shitcast1231@163.com", "itcast123456");
//设置发件人, 其中参数代表发件人邮件地址
htmlEmail.setFrom("shitcast1231@163.com");
//设置收件人,如果想添加多个收件人,将此语句多写几次即可。其中参数代表收件人邮件地址
htmlEmail.addTo(toEmail);
//设置邮件主题
htmlEmail.setSubject("账号激活");
//设置编码格式及正文内容
htmlEmail.setCharset("gb2312");
htmlEmail.setMsg(emailMsg);
htmlEmail.send();//发送邮件
}
public static void main(String[] args) throws Exception {
String toEmail = "shitcast123456@163.com";
String emailMsg = "<h2>恭喜您!注册成功!</h2><a href='http://localhost:8080/userServlet?requestMethod=active'>请点击此处激活账号</a>";
sendMail(toEmail,emailMsg);
System.out.println("发送成功。。。");
}
}
package cn.itcast.travel.utils;
import java.security.MessageDigest;
/**
* 写一个MD5算法,运行结果与MySQL的md5()函数相同
* 将明文密码转成MD5密码
* 123456->e10adc3949ba59abbe56e057f20f883e
*/
public final class Md5Util {
private Md5Util(){}
/**
* 将明文密码转成MD5密码
*/
public static String encodeByMd5(String password) throws Exception{
//Java中MessageDigest类封装了MD5和SHA算法,今天我们只要MD5算法
MessageDigest md5 = MessageDigest.getInstance("MD5");
//调用MD5算法,即返回16个byte类型的值
byte[] byteArray = md5.digest(password.getBytes());
//注意:MessageDigest只能将String转成byte[],接下来的事情,由我们程序员来完成
return byteArrayToHexString(byteArray);
}
/**
* 将byte[]转在16进制字符串
*/
private static String byteArrayToHexString(byte[] byteArray) {
StringBuffer sb = new StringBuffer();
//遍历
for(byte b : byteArray){//16次
//取出每一个byte类型,进行转换
String hex = byteToHexString(b);
//将转换后的值放入StringBuffer中
sb.append(hex);
}
return sb.toString();
}
/**
* 将byte转在16进制字符串
*/
private static String byteToHexString(byte b) {//-31转成e1,10转成0a,。。。
//将byte类型赋给int类型
int n = b;
//如果n是负数
if(n < 0){
//转正数
//-31的16进制数,等价于求225的16进制数
n = 256 + n;
}
//商(14),数组的下标
int d1 = n / 16;
//余(1),数组的下标
int d2 = n % 16;
//通过下标取值
return hex[d1] + hex[d2];
}
private static String[] hex = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
/**
* 测试
*/
public static void main(String[] args) throws Exception{
String password = "123456";
String passwordMD5 = Md5Util.encodeByMd5(password);
System.out.println(password);
System.out.println(passwordMD5);
}
}
package cn.itcast.travel.utils;
import java.util.UUID;
/**
* 产生UUID随机字符串工具类
*/
public final class UuidUtil {
private UuidUtil(){}
public static String getUuid(){
return UUID.randomUUID().toString().replace("-","");
}
/**
* 测试
*/
public static void main(String[] args) {
System.out.println(UuidUtil.getUuid());
System.out.println(UuidUtil.getUuid());
System.out.println(UuidUtil.getUuid());
System.out.println(UuidUtil.getUuid());
}
}
工具类
最新推荐文章于 2022-08-16 14:29:21 发布