當我們在網站上注冊一個賬戶,一般都會有一個驗證資訊發送置你的郵箱或者需要手機驗證,方可注冊成功,
那麼這個過程是怎麼實作的呢?今天主要介紹下!
一、注冊郵件的發送過程
首先我們了解下注冊郵件的發送過程,首先是我們登入到發送郵件的用戶端,編輯郵件,将其資訊存儲SMTP
伺服器上,然後收件方登入到收件的用戶端伺服器上,從POP3伺服器上取出發送方發送的郵件,大緻過程就是這樣。
具體内容就不在這詳細介紹了。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TN2ITMxAzM1ETOyETM1EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
二、編輯代碼
這裡簡單介紹下在本地發送注冊郵件的過程
<span style="font-family:KaiTi_GB2312;font-size:18px;">package cn.itcast.shop.utils;
import java.net.PasswordAuthentication;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.context.support.StaticApplicationContext;
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
/**
* 郵件發送的工具類
*
* @author X
*
*/
public class MailUitls {
/**
*
* @param to
* :收件人
* @param code
* :激活碼
*/
public static void sendMail(String to, String code)
throws MessagingException {
/**
* 1.獲得一個session對象
*
*/
// 獲得連接配接對象
Properties props = new Properties();
// 發送郵件的主機
props.setProperty("mail.host", "localhost");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
// 發件人的使用者名和密碼,連接配接伺服器的郵箱和密碼
return new javax.mail.PasswordAuthentication("[email protected]",
"111");
}
});</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;">
// 建立郵件的對象
javax.mail.Message message = new MimeMessage(session);</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;">
// 設定發件人</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;">
try {
// 設定發件人的郵箱位址
message.setFrom(new InternetAddress("[email protected]"));
// 設定收件人
message.addRecipient(RecipientType.TO, new InternetAddress(to));
// 抄送cc 密送bcc
// 設定标題
message.setSubject("來自提高班官方激活郵件");
// 設定郵件的正文
message.setContent(
"<h1>點下面連接配接完成操作</h><h3><a href='http://192.168.21.25:8080/SSH-shop/user_acitve.action?code="
+ code
+ "'>http://192.168.21.25:8080/SSH-shop/user_acitve.action?code="
+ code + "</a></h3>", "text/html;charset=UTF-8");
// 發送郵件
Transport.send(message);
} catch (AddressException e) {</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;">
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;">//做一個簡單的測試,驗證是否正确
public static void main(String[] args) {
try {
sendMail("[email protected]", "1231312312");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
</span>
注意:在這裡發送郵件的主機設定的是本地,也可以自行換成為其他的。
最終效果圖:
簡單的一個發送注冊檔案的過程就完成了,今天先簡單介紹到這裡,未完待續.....