邮箱注册(发送邮件验证码;QQ邮箱)

邮箱注册(发送邮件验证码;QQ邮箱)

先去QQ邮箱——》点击设置有POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务。开通会有授权码
复制粘贴代码就可以使用。根据指导更改配置即可。
如有报错 不能使用 还请大家谅解。(没有经过作者同意,禁止修改代码,盗用(利用代码去骗取财钱);免费供大家参考使用)
联系方式QQ:913237269,添加时请说明一下。
如有问题或者有帮助请评论

邮箱注册(发送邮件验证码;QQ邮箱)_第1张图片

配置文件

项目配置文件application

#邮件发送配置
#程序猿不用修改
spring.mail.default-encoding=UTF-8
#程序猿不用修改
spring.mail.host=smtp.qq.com
#更换成自己的邮箱(或者就是开通服务的邮箱)
[email protected]
#更换自己的邮箱复服务授权码(开通邮箱的服务密码)
spring.mail.password=chvxmgfbce
#程序猿不用修改
spring.mail.properties.mail.smtp.auth=true
#程序猿不用修改
spring.mail.properties.mail.smtp.starttls.enable=true
#程序猿不用修改
spring.mail.properties.mail.smtp.starttls.required=true

控制层代码实现(controller)

package com.ljma.web.mng.email.controller;

import com.ljma.web.mng.email.service.Emailservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Random;

/**
  * @description: 邮箱发送注册码
  * @author 卢嘉城
  * @date 2019/7/11
  */
@RestController
@RequestMapping("/email/")
public class EmailController {
@Autowired
private Emailservice emailservice;
@RequestMapping(“getCheckCode”)
@ResponseBody
public String getCheckCode(String email){
String checkCode = String.valueOf(new Random().nextInt(899999) + 100000);
String message = “您的注册验证码为:”+checkCode;
try {
emailservice.sendSimpleMail(email, “注册验证码”, message);
}catch (Exception e){
return “”;
}
return checkCode;
}

}

邮箱注册接口(service)

package com.ljma.web.mng.email.service;

import java.util.List;

/**
  * @description: 邮箱接口
  * @author 卢嘉城
  * @date 2019/7/11
  */
public interface Emailservice {
void sendSimpleMail(String to,String title,String content);

 void sendAttachmentsMail(String to, String title, String cotent, List fileList);

}

邮箱注册实现类(impl)

package com.ljma.web.mng.email.service.impl;

import com.ljma.web.mng.email.service.Emailservice;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
import org.apache.tomcat.jni.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.activation.DataSource;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
  * @description: 邮箱实现类
  * @author 卢嘉城
  * @date 2019/7/11
  */

@Service(“emailservice”)
public class EmailServiceImpl implements Emailservice {
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender mailSender;
Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void sendSimpleMail(String to, String title, String content){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(title);
message.setText(content);
mailSender.send(message);
logger.info(“邮件发送成功”);
}
@Override
public void sendAttachmentsMail(String to, String title, String cotent, List fileList){
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(title);
helper.setText(cotent);
String fileName = null;
for (File file:fileList) {
fileName = MimeUtility.encodeText(String.valueOf(file.getClass()), “GB2312”, “B”);
helper.addAttachment(fileName, (DataSource) file);
}
} catch (Exception e) {
e.printStackTrace();
}
mailSender.send(message);
logger.info(“邮件发送成功”);
}
}

你可能感兴趣的:(邮箱注册(发送邮件验证码;QQ邮箱))