作者:波波无敌1989_424 | 来源:互联网 | 2023-09-18 13:02
首先需要导入三个必需的jar包:1.mail.jar2.activation-1.1.1.jar3.javax.mail-1.6.0.jar第二。将三个jar包构建路径:点击Add
首先需要导入三个必需的jar包:
1.mail.jar
2.activation-1.1.1.jar
3.javax.mail-1.6.0.jar
第二。将三个jar包构建路径: 点击 Add to Aspectpath,三个jar包都需要。
Controller代码:`
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;//这两个确保导入成功,未报异常
@Controller
public class SendEmailController {
@Autowired
private JavaMailSender javaMailSender;
@RequestMapping("send")
public @ResponseBody void senMsg(HttpSession httpSession, @RequestParam String email){
//生成六位数验证码
String Captcha = String.valueOf(new Random().nextInt(899999) + 100000);
httpSession.setAttribute("Captcha",Captcha);
SimpleMailMessage message = new SimpleMailMessage();
//发件人的邮箱地址
message.setFrom("XXXXXX@qq.com");
//收件人的邮箱地址
message.setTo(email);
//邮件主题
message.setSubject("邮件的主题");
//邮件内容
message.setText("欢迎您注册地软平台:"+Captcha+"若想要了解相关消息,请访问官网:xxxxxxxxx");
//发送邮件
javaMailSender.send(message);
}
注册-Controller:
@RequestMapping("Register-QAZXFSDFGER")
@ResponseBody
public String Register(String username,String nickname,String password,String phone,String email,String emailcode,
HttpSession session,HttpServletRequest req) {
String sessiOnemailcode=(String) session.getAttribute("Captcha");
if(emailcode.equals(sessionemailcode)&&emailcode!=null&&sessionemailcode!=null)
{
int userid = (int)(Math.random()*100000);
`userservice.Register(userid,username,nickname,password,phone,email);
//注册成功返回登录
return "OK";
}
//注册失败返回注册页
return "No";
}``
注册jsp页面-ajax:
`
//注册表单代码:
`