用戶(hù)注冊(cè)賬號(hào)之后需要給注冊(cè)郵箱發(fā)送郵件激活驗(yàn)證,使用工具javax.mail-1.5.1.jar
service層
1.將用戶(hù)表單數(shù)據(jù)插入數(shù)據(jù)庫(kù)
dao.inserUserInfo(user);
2.傳入激活郵件跳轉(zhuǎn)鏈接和激活碼
String emailActive ="<a href='http://localhost/bookStore/servlet/CheckAcitiveServelet?activeCode=" + user.getActiveCode() + "'>點(diǎn)我激活</a>";
3.調(diào)用API
SendJMail.sendMail(user.getEmail(), emailActive);
utils層
//發(fā)送郵件的具體實(shí)現(xiàn)
public class SendJMail
{
public static boolean sendMail(String email, String emailMsg)
{
System.out.println("http://k6 正在發(fā)送郵件");
//
String from = "13593686503@163.com"; // 郵件發(fā)送人的郵件地址
String to = email; // 收件人的郵件地址
final String username = "13593686503@163.com"; // 發(fā)件人的郵件帳戶(hù)
final String password = "123456"; // 發(fā)件人的郵件密碼
// 定義Properties對(duì)象,設(shè)置環(huán)境信息
Properties props = System.getProperties();
// 設(shè)置郵件服務(wù)器的地址
props.setProperty("mail.smtp.host", "smtp.163.com"); // 指定的smtp服務(wù)器
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");// 設(shè)置發(fā)送郵件使用的協(xié)議 pop3
// 創(chuàng)建Session對(duì)象,session對(duì)象表示整個(gè)郵件的環(huán)境信息
Session session = Session.getInstance(props);
// 設(shè)置輸出調(diào)試信息
session.setDebug(true);
try
{
// Message的實(shí)例對(duì)象表示一封電子郵件
MimeMessage message = new MimeMessage(session);
// 設(shè)置發(fā)件人的地址
message.setFrom(new InternetAddress(from));
// 設(shè)置主題
message.setSubject("網(wǎng)上商城歡迎您,點(diǎn)擊激活用戶(hù)");
// 設(shè)置郵件的文本內(nèi)容
message.setText("Welcome to JavaMail World!");
//發(fā)送鏈接顯示出來(lái)
message.setContent((emailMsg), "text/html;charset=utf-8");
// 從session的環(huán)境中獲取發(fā)送郵件的對(duì)象
Transport transport = session.getTransport();
//設(shè)置 連接郵件服務(wù)器
transport.connect("smtp.163.com", 25, username, password);
// 設(shè)置收件人地址,并發(fā)送消息
transport.sendMessage(message,new Address[]{new InternetAddress(to)});
transport.close();
return true;
}
catch (MessagingException e)
{
e.printStackTrace();
return false;
}
}
}