1:安裝 eyoumailserversetup 易郵郵件服務器 注冊賬號
2:安裝Foxmail
登錄以后會有個還原頁面
3:測試
4:java 代碼編寫
配置文件:
mail.host=http://172.16.71.27:8080/ mail.smtpHost=172.16.71.27 mail.username=Admin@liveyc.com mail.password=Admin
server層代碼:
1 package com.liveyc.eloan.base.service.impl; 2 3 import java.util.Date; 4 import java.util.Properties; 5 import java.util.UUID; 6 7 import javax.mail.internet.MimeMessage; 8 9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Value; 11 import org.springframework.mail.javamail.JavaMailSenderImpl; 12 import org.springframework.mail.javamail.MimeMessageHelper; 13 import org.springframework.stereotype.Service; 14 15 import com.liveyc.eloan.base.dao.EmailverifyDao; 16 import com.liveyc.eloan.base.domain.Emailverify; 17 import com.liveyc.eloan.base.service.IEmailVerifyService; 18 import com.liveyc.eloan.base.util.DateUtil; 19 import com.liveyc.eloan.util.BidConst; 20 import com.liveyc.eloan.util.UserContext; 21 22 @Service 23 public class EmailVerifyServiceImpl implements IEmailVerifyService { 24 25 @Autowired 26 private EmailverifyDao emailverifyDao; 27 28 @Value("${mail.host}") 29 private String host; 30 31 @Value("${mail.smtpHost}") 32 private String smtpHost; 33 34 @Value("${mail.username}") 35 private String username; 36 37 @Value("${mail.password}") 38 private String password; 39 40 /** 41 * http://localhost:8080/checkEmailBind.do?key=4f399b0b-c17b-4e9d-9abc-0 42 * c816cd474bb 1,得到當前用戶,得到要綁定的郵箱,綁定的時間,生成一個隨機碼--->EmailVerify; 43 * 2,發送一封郵件;在郵件的這里(http://localhost/checkEmailBind.do?key=隨機碼) 44 */ 45 @Override 46 public void sendVerifyEmail(String email) { 47 Emailverify ev = new Emailverify(); 48 ev.setUserinfoId(UserContext.getCurrent().getId()); 49 ev.setEmail(email); 50 ev.setSendTime(new Date()); 51 ev.setUuid(UUID.randomUUID().toString().replace("-", "")); 52 53 // 構造郵件內容 54 StringBuilder content = new StringBuilder(100) 55 .append("<html><head></head><body><h1>這是你在藍源Eloan網站的驗證郵件,請點擊<a href='") 56 .append(host).append("checkEmailBind.do?key=") 57 .append(ev.getUuid()).append("'>").append("這里").append("</a>") 58 .append("完成郵箱綁定,有效期為"+BidConst.EMAIL_VALID_DAY+"天</h1></body></html>"); 59 60 System.out.println(content); 61 62 try { 63 sendEmail(email, "綁定郵箱驗證郵件", content.toString()); 64 this.emailverifyDao.insert(ev); 65 } catch (Exception e) { 66 throw new RuntimeException(e.getMessage()); 67 } 68 } 69 70 /** 71 * 發送郵件 72 * 73 * @param email 74 * @param string 75 * @param string2 76 */ 77 private void sendEmail(String email, String subject, String content) 78 throws Exception { 79 JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); 80 81 // 設置發送郵件的SMTP服務器地址 82 senderImpl.setHost(smtpHost); 83 84 // 創建一個郵件對象 85 MimeMessage mailMessage = senderImpl.createMimeMessage(); 86 87 // 創建郵件對象的服務類 88 MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,"UTF-8"); 89 90 messageHelper.setTo(email);// 設置發給誰 91 messageHelper.setFrom(username);// 發件人 92 messageHelper.setSubject(subject);// 設置郵件標題 93 messageHelper.setText(content, true);// 設置郵件內容 94 95 senderImpl.setUsername(username); // 根據自己的情況,設置username 96 senderImpl.setPassword(password);// 設置密碼 97 98 Properties prop = new Properties(); 99 prop.put("mail.smtp.auth", "true");// 設置發送郵件需要身份認證 100 prop.put("mail.smtp.timeout", "25000");// 設置發送超時時間 101 senderImpl.setJavaMailProperties(prop); 102 103 senderImpl.send(mailMessage); 104 } 105 106 /** 107 * 獲取請求中的隨機碼; 108 * 109 * 2,根據隨機碼得到EmailVerify對象; 3,只需要驗證時間是否超時; 4,把emailverify中的郵箱地址綁定到指定用戶上; 110 */ 111 @Override 112 public Emailverify verifyEmail(String key) { 113 Emailverify ev = this.emailverifyDao.selectByUuid(key); 114 if (ev != null 115 && DateUtil.getSecondsBetweenDates(ev.getSendTime(), new Date()) <= 3600 * 24 * BidConst.EMAIL_VALID_DAY) { 116 return ev; 117 } 118 return null; 119 } 120 121 }
5:頁面測試
收到郵件了