发送邮箱验证码

	private boolean sendVerifyMailBoxCode(String email, String nickname, String verifyCode) throws Exception {

		Properties prop = new Properties();
		prop.setProperty("mail.host", "smtp.office365.com");// 设置qq邮件服务器
		prop.setProperty("mail.transport.protocol", "smtp");// 邮件发送协议
		prop.setProperty("mail.smtp.auth", "true");// 需要验证用户名密码
		prop.setProperty("mail.smtp.port","587");//SMTP服务器端口   587
		prop.setProperty("mail.smtp.starttls.enable", "true");
		MailSSLSocketFactory sf = new MailSSLSocketFactory();
		sf.setTrustAllHosts(true);
		prop.put("mail.smtp.ssl.socketFactory", sf);
		prop.put("mail.smtp.ssl.protocols", "TLSv1.2");
        //这是两种连接方式,选其一即可
		//MyAuthentication authentication = new MyAuthentication(username, password);
		//Session session = Session.getInstance(prop,authentication);
		Session session = Session.getInstance(prop,null);

		// 开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
		session.setDebug(true);

		// 2.通过Session得到transport对象
		Transport ts = session.getTransport();

		// 3.使用邮箱的用户名和授权码连上邮件服务器
		ts.connect("smtp.office365.com", "邮箱号", "授权码");

		// 4.创建邮件:写邮件
		// 注意需要传递Session;
		MimeMessage message = new MimeMessage(session);

		// 指定邮件的发件人
		message.setFrom(new InternetAddress("telecom@zsmls.com"));

		// 指明邮件的收件人
		message.setRecipients(Message.RecipientType.TO, email);

		// 邮件的标题
		message.setSubject("SCM找回密码");

		// =================================带图片邮件start=================================================

/*        // 准备图片数据 下面是使用的格式
        MimeBodyPart image = new MimeBodyPart();
        // 图片需要经过数据处理... DataHandler:数据处理
        DataHandler dh = new DataHandler(new FileDataSource("C:\\Users\\86152\\Pictures\\tool\\1.jpg"));
        image.setDataHandler(dh);// 在我们的Body中放入这个处理的图片数据
        image.setContentID("1.jpg");// 给图片设置一个ID,我们在后面可以使用!*/

		// 准备正文数据
		MimeBodyPart text = new MimeBodyPart();
//        text.setContent("这是一封正文带图片<img src='cid:1.jpg'>的邮件", "text/html;charset=UTF-8");
		String content = "<div style=\"background-color:#f7f7f7;padding-top:30px;padding-bottom:30px;\">\r\n" +
"测试内容----\r是转义字符"+nickname+"的验证码是"+verifyCode+"\n"+
				"    </div>";
		text.setContent(content,"text/html;charset=UTF-8");
		// 描述数据关系
		MimeMultipart mm = new MimeMultipart();
//        mm.addBodyPart(image);
		mm.addBodyPart(text);
		mm.setSubType("mixed");

		// 设置到消息中,保存修改
		message.setContent(mm);
		message.saveChanges();

		// =================================带图片邮件end=================================================
		// 5.发送邮件
		ts.sendMessage(message, message.getAllRecipients());

		//这个不能加
		//Transport.send(message);

		// 6.关闭连接
		ts.close();

		return true;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中发送邮箱验证码可以通过使用 JavaMailSender 来实现。首先,你需要在 `application.properties` 或 `application.yml` 配置文件中配置邮件服务器的相关信息,如下所示: application.properties: ```properties spring.mail.host=your.mail.server spring.mail.port=your.mail.port spring.mail.username=your.username spring.mail.password=your.password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` application.yml: ```yaml spring: mail: host: your.mail.server port: your.mail.port username: your.username password: your.password properties: mail: smtp: auth: true starttls: enable: true ``` 接下来,你可以创建一个邮件服务类来发送验证邮件,示例如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender javaMailSender; public void sendVerificationCode(String to, String code) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject("验证码"); message.setText("您的验证码是:" + code); javaMailSender.send(message); } } ``` 在上述示例中,`JavaMailSender` 是由 Spring Boot 自动配置的邮件发送器。你可以在需要发送验证码的地方调用 `sendVerificationCode` 方法来发送邮件。 注意:为了使用JavaMailSender,你需要在项目的依赖管理文件(例如 pom.xml)中添加相应的依赖。你可以添加以下依赖来使用 Spring Boot 提供的邮件支持: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 以上就是使用 Spring Boot 发送邮箱验证码的简单示例。你可以根据自己的实际需求进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值