用java實現(xiàn)郵箱驗證其實很簡單
我們只需要一個jar包
mail.jar
先創(chuàng)建一個郵箱發(fā)送類
public class MailUtils {
public static void sendMail(String email, String emailMsg)
throws AddressException, MessagingException {
// 1.創(chuàng)建一個程序與郵件服務器會話對象 Session
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");
props.setProperty("mail.host", "smtp.163.com");
props.setProperty("mail.smtp.auth", "true");// 指定驗證為true
// 創(chuàng)建驗證器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("cui******@163.com", "12313456");//注冊郵箱的帳號和授權碼
}
};
Session session = Session.getInstance(props, auth);
// 2.創(chuàng)建一個Message,它相當于是郵件內(nèi)容
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress("**********@163.com","8896"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 設置發(fā)送者
message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 設置發(fā)送方式與接收者
message.setSubject("用戶激活");
// message.setText("這是一封激活郵件,請<a href=\'#\'>點擊</a>");
message.setContent(emailMsg, "text/html;charset=utf-8");
// 3.創(chuàng)建 Transport用于將郵件發(fā)送
Transport.send(message);
}
}
這里我用的是網(wǎng)易郵箱,郵箱需要設置開通smtp協(xié)議,得到授權碼
測試類
public class Test { public static void main(String[] args) { try {
String emailMsg="這是一封激活郵件"; MailUtils.sendMail(驗證的郵箱, emailMsg); System.out.println("郵件發(fā)送成功!"); } catch (MessagingException e) { e.printStackTrace(); } } }
名稱欄目:用java實現(xiàn)郵箱驗證
文章位置:http://m.rwnh.cn/article6/cgdhog.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設、移動網(wǎng)站建設、App設計、全網(wǎng)營銷推廣、面包屑導航、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)