小编最近在做SSH商城的项目,遇到了有关注册用户的界面用于邮箱的验证,顺便写下来与大伙相互切磋!
【邮件发送的全过程】
【发送激活邮件】
发送激活邮件要具备的几个条件:
1、 电子邮箱:tengliu666@163.com
2、邮件服务器:eyoumailserversetup.exe(易邮邮件服务器)
3、邮件协议
*smtp :发送邮件协议
* pop :接收邮件协议
4、邮件发送全过程
5、搭建邮箱服务器
(1) 安装邮箱服务器——>修改域名:工具---->服务器设置---->shop.com
(2)注册账号:账号---->新建账号(aaa)---->服务器账号(service)
6、安装客户端软件:(接收和发送邮件.)
安装foxmail——> 配置发送邮件服务器(localhost) ——> 配置接收邮件服务器(localhost)
7、编码实现发送邮件:
(1) 复制mail.jar和activation.jar
(2) 删除EE5.0中自带发送邮件jar包: windows---->preferences--->lib
(3) 编写代码完成发送邮件:
public class MailUitls {
/**
* 发送邮件的方法
* @param to :收件人
* @param code :激活码
*/
public static void sendMail(String to,String code){
/**
* 1.获得一个Session对象.
* 2.创建一个代表邮件的对象Message.
* 3.发送邮件Transport
*/
// 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.addRecipient(RecipientType.TO, new InternetAddress(to));
// 抄送 CC 密送BCC
// 设置标题
message.setSubject("来自购物天堂官方激活邮件");
// 设置邮件正文:
message.setContent("<h1>购物天堂官方激活邮件!点下面链接完成激活操作!</h1><h3><a href='http://192.168.21.89:8080/shop/user_active.action?code="+code+"'>http://192.168.36.103:8080/shop/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8");
// 3.发送邮件:
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
sendMail("aaa@shop.com","11111111111111");
}
}