發送激活郵件
電子郵箱
郵件服務器
郵件協議
Smtp郵件發送
Pop3郵件接收
Imap郵件接收
郵件發送的全過程
搭建
搭建郵箱服務器
點擊安裝,注意:一定要安裝在C盤
2 修改域名
工具--->服務器設置---->aigouwu.com
添加幾個郵箱,用來接收郵件,
3 註冊賬號
賬號---->新建賬號
安裝客戶端軟件(接收和發送郵件)
Outlook和foxmail
新建郵箱用戶,
需要注意的是
*如果添加的是sohu、sina等公網的郵箱則接收服務器和發送服務器地址都是公網上的,
配置自己電腦郵件服務器的郵箱用戶則將接收服務器地址和發送服務器地址都是 localhost
配置
配置發送郵件服務器
Localhost
配置接收郵件服務器
Localhost
編碼實現
複製和放到lib中,
刪除javaee5.0裏面的activation.jar mail.jar
Add jars--->使用WINRAR打開----->刪除activation.jar和mail.jar
編碼:
package com.hbliti.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;
/**
* 發送驗證郵件工具類
* @author[email protected]*
*/
publicclass MailUtils {
/**
*
* @param to:收件人
* @param code:激活碼
*/
publicstaticvoid sendMail(String to , String code){
/**
* 發送郵件的步驟:
* 1.獲得一個Session對象
* 2.創建一個代表郵件的對象Message
* 3.發送郵件Transport
*
*/
Properties props=new Properties();
props.setProperty("mail.host", "localhost");
props.setProperty("mail.smtp.auth", "true");
//獲得連接對象
//Authentication:通過用戶名和密碼通過認證
//props:郵件服務器地址和smtp協議
//Authentication:使用這個郵箱名和密碼來向用戶發送郵件
Session session=Session.getInstance(props, new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
returnnew PasswordAuthentication("[email protected]", "111");
}
});
//2.創建一個郵件對象
Message message=new MimeMessage(session);
try {
//郵件的發件人
message.setFrom(new InternetAddress("[email protected]"));
//郵件的收件人
message.addRecipient(RecipientType.TO, new InternetAddress(to));
//郵件的主題
message.setSubject("來自94aigouwu網的官方激活郵件");
//郵件的內容
message.setContent("<h1>來自94aigouwu網的官方激活郵件,點下面鏈接來激活操作</h1><h3><a href='http://localhost:8080/94aigouwu/user_active.action?code="+code+"'>http://localhost:8080/94aigouwu/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8");
//發送郵件
Transport.send(message);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}