一、创建项目过程
1、以我自己的项目工程图为例(随便创建 一个sprinboot项目即可)
在pox.xml中添加依赖
<!-- 发送邮件 --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
2、创建一个SendEmailService接口
package com.xsy.service; public interface SendEmailService { //发送邮件接口 int sendEmail(String addr) throws Exception; }
3、创建一个发送邮件的实现类(记得修改里面的内容,qq邮箱以及授权码,如果邮箱授权码不会,可以上网搜搜,很简单)SendEmailServiceImpl.java
package com.xsy.service.impl; import com.xsy.service.SendEmailService; import org.springframework.stereotype.Service; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Date; import java.util.Properties; import java.util.Random; @Service("sendEmail") public class SendEmailServiceImpl implements SendEmailService { public static final String SMTPSERVER = "smtp.qq.com"; public static final String SMTPPORT = "465"; public static final String ACCOUT = "自己的qq邮箱"; public static final String PWD = "邮箱的授权码,不是qq密码"; @Override public int sendEmail(String addr) throws Exception{ System.out.println("传过来的地址参数"+addr); // 创建邮件配置 Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求) props.setProperty("mail.smtp.host", SMTPSERVER); // 发件人的邮箱的 SMTP 服务器地址 props.setProperty("mail.smtp.port", SMTPPORT); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.auth", "true"); // 需要请求认证 props.setProperty("mail.smtp.ssl.enable", "true");// 开启ssl // 根据邮件配置创建会话,注意session别导错包 Session session = Session.getDefaultInstance(props); // 开启debug模式,可以看到更多详细的输入日志 // session.setDebug(true); Random random = new Random(); int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数 //创建邮件 MimeMessage message = createEmail(session,addr,rannum); //获取传输通道 Transport transport = session.getTransport(); transport.connect(SMTPSERVER,ACCOUT, PWD); //连接,并发送邮件 transport.sendMessage(message, message.getAllRecipients()); transport.close(); System.out.println("返回的验证码数据"+rannum); return rannum; } public MimeMessage createEmail(Session session,String ad,int num) throws Exception { // 根据会话创建邮件 MimeMessage msg = new MimeMessage(session); // address邮件地址, personal邮件昵称, charset编码方式 InternetAddress fromAddress = new InternetAddress(ACCOUT, "橙子商城", "utf-8"); // 设置发送邮件方 msg.setFrom(fromAddress); InternetAddress receiveAddress = new InternetAddress( ad, "test", "utf-8"); // 设置邮件接收方 msg.setRecipient(Message.RecipientType.TO, receiveAddress); // 设置邮件标题 msg.setSubject("橙子商城", "utf-8"); msg.setText("注册验证码为:"+num); // 设置显示的发件时间 msg.setSentDate(new Date()); // 保存设置 msg.saveChanges(); return msg; } }
4、在controller包下创建一个控制类UtilsController.java,用来测试
package com.xsy.controller; import com.xsy.bean.Msg; import com.xsy.service.SendEmailService; import com.xsy.service.UpImageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.List; @RestController @RequestMapping("/utils") public class UtilsController { @Autowired private SendEmailService sendEmailService; @Autowired private UpImageService upImageService; //发送邮箱验证码 @RequestMapping(value = "/sendEmail",method = RequestMethod.GET) public Msg sendEmail() throws Exception{ int num=sendEmailService.sendEmail("1035336407@qq.com"); System.out.println("验证码"+num); return Msg.success().add("checkCode",num); } }
5、运行,访问