java用户注册邮箱发送注册激活码

package cn.itcast.shop.utils;


import java.util.Properties;


import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


/*
 * 发送邮件的工具类
 * */
public class MailUtils {
   
/*
* 发送邮件的方法
* @param to  收件人
* @param code 激活码 

* */
public static void sendMail(String to,String code){

/*
* 创建session对象
* 创建一个代表邮件的对象Message
* 发送邮件Transpot

* */
//1 获得连接对象
Properties props=new Properties();

props.setProperty("mail.host", "localhost");
Session session =Session.getInstance(props, new Authenticator() {

@Override
protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication("service@shop.com","111");
}

});
//2创建邮件对象
Message message=new MimeMessage(session);
try {
message.setFrom(new InternetAddress("service@shop.com"));

message.setRecipient(RecipientType.TO, new InternetAddress(to));
message.setSubject("来自传智播客的购物系统");
message.setContent("<h1>来之传智商城的购物系统,点下面的连接完成激活</h1> <h3> <a href='http://172.17.215.235:8080/shop/user_active.action?code="+code+"'>http://172.17.215.235:8080/shop/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8");
Transport.send(message);

} catch (AddressException e) {

e.printStackTrace();
} catch (MessagingException e) {

e.printStackTrace();
}

}

public static void main(String[] args) {


sendMail("aaa@shop.com", "11111111111111111");
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值