一,功能點html
後臺管理系統,添加用戶時。對註冊的新用戶郵箱發送初始密碼。apache
二,代碼實現ide
1.Mail實體類工具
public class Mail { private Set<String> receivers; // 收件人的郵箱 private String subject; // 主題 private String message; // 信息(支持HTML) public Mail() { } public Mail(Set<String> receivers, String subject, String message) { this.receivers = receivers; this.subject = subject; this.message = message; } public Set<String> getReceivers() { return receivers; } public void setReceivers(Set<String> receivers) { this.receivers = receivers; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
2.郵箱工具類this
package com.ideal.manage.guest.util; import com.ideal.manage.guest.bean.mail.Mail; import org.apache.commons.lang3.StringUtils; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created on 15/11/21. * Remark:發送郵件工具類 */ public class MailUtil { private static Logger logger = LoggerFactory.getLogger(MailUtil.class); public static boolean send(Mail mail) { String from = "visitor_yishang@126.com";//填寫本身的郵箱 int port = 25; String host = "smtp.126.com";//這裏我用的是smtp協議 String pass = "qwer1234";//填寫本身的郵箱的受權碼 String nickname = "admin"; HtmlEmail email = new HtmlEmail(); try { email.setHostName(host); email.setCharset("UTF-8"); for (String str : mail.getReceivers()) { email.addTo(str); } email.setFrom(from, nickname); email.setSmtpPort(port); email.setAuthentication(from, pass); email.setSubject(mail.getSubject()); email.setMsg(mail.getMessage()); email.send(); logger.info("{} 發送郵件到 {}", from, StringUtils.join(mail.getReceivers(), ",")); return true; } catch (EmailException e) { logger.error(from + "發送郵件到" + StringUtils.join(mail.getReceivers(), ",") + "失敗", e); return false; } } }
3.業務代碼:添加用戶時,發送郵件加密
/** * 保存用戶 * @param id * @param loginName * @param password * @param name * @param sex * @param telephone * @param mobile * @param email * @param role * @param customer * @param jobTitle * @param remark */ public void saveUser(String id,String loginName,String password, String name,Long sex,String telephone, String mobile,String email,String role, String customer,String jobTitle,String remark){ MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal(); User user = new User(); if(id != null){ user = findById(id); user.setLastUpdateUser(new User(shiroUser.getId())); user.setLastUpdateTime(new Date()); }else { user.setCreateTime(new Date()); user.setCreateUser(new User(shiroUser.getId())); user.setDelFlag(0L); } user.setLoginName(loginName); user.setPassword(password); //密碼加密 PasswordHelper passwordHelper = new PasswordHelper(); passwordHelper.encryptPassword(user); user.setName(name); user.setSex(sex); user.setTelephone(telephone); user.setMobile(mobile); user.setEmail(email); if(role != null){ Role r = roleService.findById(role); user.setRole(r); } if(customer != null){ Customer c = customerService.findById(customer); user.setCustomer(c); } user.setJobTitle(jobTitle); user.setRemark(remark); userRepository.save(user); //發送郵件 String message = "您的帳戶已經開通, 請妥善保管好您的我的密碼:" + password; String subject = "用戶添加成功通知"; sendEmailToUser(user, subject, message); } /** * 發送郵件 * @param user * @param subject * @param message */ private void sendEmailToUser(User user, String subject, String message) { Mail mail = new Mail(); mail.setMessage(message); mail.setReceivers(Sets.newHashSet(user.getEmail())); mail.setSubject(subject); MailUtil.send(mail); }
4.對了,別忘了在pom添加依賴idea
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.4</version> </dependency>
三,郵箱配置spa
登陸個人126郵箱後找到設置,以下圖code
把POP3/SMTP/IMAP勾上就能夠了,並設置受權碼就能夠了。htm
添加用戶後,你的郵箱會收到一封郵件