后台管理系统之邮件开发(Java实现)

一,功能点

后台管理系统,添加用户时。对注册的新用户邮箱发送初始密码。

后台管理系统之邮件开发(Java实现)

二,代码实现

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.邮箱工具类

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添加依赖

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>

三,邮箱配置

登录我的126邮箱后找到设置,如下图

后台管理系统之邮件开发(Java实现)

把POP3/SMTP/IMAP勾上就可以了,并设置授权码就可以了。

后台管理系统之邮件开发(Java实现)

添加用户后,你的邮箱会收到一封邮件

后台管理系统之邮件开发(Java实现)

上一篇:Change the default MySQL data directory with SELinux enabled


下一篇:Centos7部署Kubernetes集群(单工作节点)+配置dashboard可视化UI