当前位置: 首页 > news >正文

手机网站制作移动高端网站建设长沙建站公司效果

手机网站制作移动高端网站建设,长沙建站公司效果,网站服务内容怎样选,专业建站模板运行环境: IntelliJ IDEA 2022.2.5 (Ultimate Edition) (注意:idea必须在2021版本以上)JDK17 项目目录: 该项目分为pojo,service,controller,utils四个部分, 在pojo层里面写实体内容(发邮件需要的发件人邮…

运行环境:

  • IntelliJ IDEA 2022.2.5 (Ultimate Edition) (注意:idea必须在2021版本以上)
  • JDK17

项目目录:

该项目分为pojo,service,controller,utils四个部分,

在pojo层里面写实体内容(发邮件需要的发件人邮箱,授权码,服务器域名,身份验证开关),

service层里面写send方法,

utils里面写发送邮件实现的工具类,

controller层里面调用service里面的方法测试send方法。

在resource里面的application.yml写相关的发邮件参数(user,code,host,auth)

前提:

该项目涉及到了邮件的发送,所以需要邮箱的授权码

怎么获取授权码?

在 账号与安全 --安全设置--SMTP/IMAP服务 中开启服务并获取授权码

代码:

pojo层:

package com.xu.springbootconfigfile.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "email")
public class EmailProperties {//@Value("${email.user}")//发件人邮箱public String user ;//@Value("${email.code}")//发件人邮箱授权码public String code ;//@Value("${email.host}")//发件人邮箱对应的服务器域名,如果是163邮箱:smtp.163.com   qq邮箱: smtp.qq.compublic String host ;//@Value("${email.auth}")//身份验证开关private boolean auth ;public String getHost() {return host;}public void setHost(String host) {this.host = host;}public boolean isAuth() {return auth;}public void setAuth(boolean auth) {this.auth = auth;}public String getUser() {return user;}public void setUser(String user) {this.user = user;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}@Overridepublic String toString() {return "EmailProperties{" +"host='" + host + '\'' +", auth=" + auth +", user='" + user + '\'' +", code='" + code + '\'' +'}';}
}

service层:

package com.xu.springbootconfigfile.service;
public interface EmailService {boolean send(String to,String title,String content);}
package com.xu.springbootconfigfile.service.impl;
import com.xu.springbootconfigfile.pojo.EmailProperties;
import com.xu.springbootconfigfile.service.EmailService;
import com.xu.springbootconfigfile.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class EmailServiceImpl  implements EmailService {//注入email配置信息实体类@Autowiredprivate EmailProperties emailProperties;/*** @param to 收件人邮箱* @param title 邮件标题* @param content 邮件正文* @return*/@Overridepublic boolean send(String to, String title, String content) {//打印email配置信息System.out.println(emailProperties);//发送邮件boolean flag = MailUtil.sendMail(emailProperties,to, title, content);return flag;}
}

controller层:

package com.xu.springbootconfigfile.controller;
import com.xu.springbootconfigfile.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class EmailController {//注入email配置信息实体类@Autowiredprivate EmailService emailService;//测试方法@RequestMapping("/send")public Boolean send(){//收件人信箱String to = "邮箱号";//邮件标题String title = "test";//邮件正文String content  = "哈哈哈哈哈哈哈";//发送邮件boolean flag = emailService.send(to,title,content);return flag;}}

utils层:

package com.xu.springbootconfigfile.utils;
import com.xu.springbootconfigfile.pojo.EmailProperties;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import java.util.Properties;public class MailUtil {/*** 发送邮件* @param emailProperties 发件人信息(发件人邮箱,发件人授权码)及邮件服务器信息(邮件服务器域名,身份验证开关)* @param to 收件人邮箱* @param title 邮件标题* @param content 邮件正文* @return*/public static boolean sendMail(EmailProperties emailProperties, String to, String title, String content){MimeMessage message = null;try {Properties properties = new Properties();properties.put("mail.smtp.host", emailProperties.getHost());properties.put("mail.smtp.auth",emailProperties.isAuth());properties.put("mail.user", emailProperties.getUser());properties.put("mail.password", emailProperties.getCode());// 构建授权信息,用于进行SMTP进行身份验证Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(emailProperties.getUser(), emailProperties.getCode());}};// 使用环境属性和授权信息,创建邮件会话Session mailSession = Session.getInstance(properties, authenticator);// 创建邮件消息message = new MimeMessage(mailSession);}catch (Exception e){e.printStackTrace();}//如果邮件创建失败,直接返回if (message==null){return false;}try {// 设置发件人InternetAddress form = new InternetAddress(emailProperties.getUser());message.setFrom(form);// 设置收件人InternetAddress toAddress = new InternetAddress(to);message.setRecipient(Message.RecipientType.TO, toAddress);// 设置邮件标题message.setSubject(title);// 设置邮件的内容体message.setContent(content, "text/html;charset=UTF-8");// 发送邮件Transport.send(message);}catch (Exception e){e.printStackTrace();}return true;}
}

application.yml

pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.1.2</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.xu</groupId><artifactId>springboot-config-file</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-config-file</name><description>springboot-config-file</description><properties><java.version>17</java.version></properties><dependencies><!--web开发依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--java mail 依赖--><dependency><groupId>org.eclipse.angus</groupId><artifactId>jakarta.mail</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

运行结果:

显示true后,检查一下邮箱,就可以收到对应的测试邮件

http://www.yayakq.cn/news/420805/

相关文章:

  • 福州建设人才市场网站做视频网站用什么服务器
  • 西安网站seo诊断成立一个做网站的工作室
  • 网站开发junke100宁波网站建设流程有哪些
  • 阿里云网站搭建wordpress相册点击弹出框
  • 泰兴网站建设新站seo外包
  • 路由下做网站映射伴奏在线制作网站
  • 建设一站式服务网站vs2010网站开发登录代码
  • 北京做网站的大公司有哪些网站基本配置
  • 网站开发业务流程图免费做海报的网站
  • seo网站优化方案书电商数据统计网站
  • 省住房城乡建设厅网站做网站看什么书
  • php 企业网站开发教程苏州建网站的公司外包服务
  • 所有做运动的网站广州市番禺区
  • 局强化网站建设和管理男孩子怎么做网站推广
  • 网站套餐到期啥意思wordpress手机端加载不出来
  • 济宁有没有专门做网站的wordpress被入侵
  • 如何高效建设品牌网站网站开发文件结构组成
  • 如何说明学校网站建设情况有创意营销型网站建设
  • 海南网站建设开发wordpress主题手机版不显示侧边栏
  • 怎么在手机上制作网站云服务器做网站新手教程
  • 网站上传 空间 数据库推广的软件
  • 如何在年报网站上做遗失公告手机开发者选项在哪里关闭
  • 前端做网站一般用什么框架宜丰做网站的
  • 想建设个人网站去那里建设石家庄php网站建设
  • 安全可信网站营销网站制作方法
  • 淘宝网站网页图片怎么做的网站建设优化扬州
  • 如何建立一个个人博客网站住宅小区物业管理系统网站建设
  • 做私人网站医疗网站建设精英
  • 网站建设教程赚找湖南岚鸿认 可苏州婚庆公司网站建设案例
  • 河北住房和城乡建设部网站建筑工程网上报建网站诚信手册