ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

个人简单的邮箱网站(SpringBoot环境搭建)

2021-12-01 12:02:00  阅读:137  来源: 互联网

标签:SpringBoot 登录 发送 mail 邮箱 页面 com 搭建


一个个人邮箱网站(基于SpringBoot环境搭建)

目前只能达到发送一些简单的内容,如果有啥好的建议可以发评论!!

本人还在学习中

源码地址:https://gitee.com/mehao123/sendMail/tree/master

效果

登录前的主界面

在这里插入图片描述

登录界面

在这里插入图片描述

登录后的主界面

在这里插入图片描述

发送邮箱界面

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

项目的概述

1.这个项目目前只能实现固定的发送邮箱,接受邮箱者是可以改变的。

2.目前功能只有发送一些简单的标题、内容这样子后续我会慢慢加点功能上去。

3.这个项目是在网站上实现的,界面布局和效果你们都可以随意按照你们的想法去改。

4.核心的代码主要是在后台这一块,有SpringBoot+SpringSecurity、Springboot+Mybatis、Springboot+thymeleaf、异步任务、Druid。

5.可以实现发送邮件的简单日志,还有一个带附件发送的半成品的一些代码。

核心功能代码

后端

数据库的连接

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/meweb?serverTimezone=UTC&userUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

#下面这一块是一些Durid的设置不设置也是可以的
    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

mybatis配置

# 配置mybatis
# 扫描包的位置
mybatis.type-aliases-package=com.me.pojo
# 实现mapper接口配置mapper和接口的绑定
mybatis.mapper-locations=classpath:mapper/*.xml

邮箱的一些配置

# 邮箱
spring.mail.username=2336164407@qq.com  
# 邮箱密码的明文	
spring.mail.password=xxxxxxxxxx		
# 我使用的是QQ邮箱
spring.mail.host=smtp.qq.com			
# 开启加密验证
spring.mail.properties..mail.smtp.ssl.enable=true  # QQ邮箱独有的加密验证

发送邮箱的业务

// 封装成了一个方法
@Service
public class SendMailService {

    @Autowired
    JavaMailSenderImpl mailSender;

    @Async
    public void sendMail(String subject, String text, String to, String from) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);

        simpleMailMessage.setTo(to);
        simpleMailMessage.setFrom(from);
        mailSender.send(simpleMailMessage);
    }

}

SpringSecurity这块的功能代码

@EnableWebSecurity // 用来开启SpringSecurity的注解
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/","index").permitAll()    // permitAll() 这个代表谁都可以访问
                .antMatchers("/mail/**").hasAnyRole("mail");// hasAnyRole() 这个代表要有指定的角色才可以访问的,比如有mail这个角色

        // 这一块是用于自定义的登录页面
        http.formLogin()
                .usernameParameter("username")  // 里面的username必须和前端表单传递的name要一致
                .passwordParameter("password")	// 里面的password必须和前端表单传递的name要一致
                .loginPage("/toLogin")			// 设置登录页面的地址
                .loginProcessingUrl("/login");	// 登录页面表单提交的地址
        http.csrf().disable();					// 禁用csrf
        http.logout().logoutSuccessUrl("/");	// 设置当注销用户时跳转的页面
        http.rememberMe().rememberMeParameter("remeber");	// 设置当前用户是否记住我默认保留14天
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        // 我使用的是通过在代码中硬性设置账号和密码来实现的,如果要通过数据库的来实现账号和密码验证的话可以在网上找找资料!
        // passwordEncoder()密码加密方式,我这里用到的是 new BCryptPasswordEncoder()加密
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) 
                .withUser("me").password(new BCryptPasswordEncoder().encode("123456")).roles("mail")
                .and()
                .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("mail");
    }

}

前端

主页面代码

<!DOCTYPE html>
<html lang="zh_cn" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" th:href="@{/css/index.css}">
    <!--这个是用来设置浏览器访问的小图标的-->
    <link rel="icon" th:href="@{/image/1.png}" sizes="16x16"> 
    <script th:src="@{/js/index.js}"></script>
    <title>ME中心</title>
</head>
<body>
    <div id="index">  
        <div ><h1>欢迎来到ME的零度空间<span id="time" ></span></h1>
            <div>
                <div sec:authorize="!isAuthenticated()">
                    <a th:href="@{/toLogin}" class="zhuxiao" >登录</a>
                </div>
                <div sec:authorize="isAuthenticated()">
                    <a th:href="@{/logout}" class="zhuxiao" >注销</a>
                </div>
            </div>
            <a th:href="@{/mail/mailView}" class="but" style="text-align: center;">发送邮件</a>
            <a th:href="@{/mail/mailView}" class="but" style="text-align: center;">发送带附件的邮件</a>
            <a href="" class="but" style="text-align: center;pointer-events: none;">待开发</a>
            <a href="" class="but" style="text-align: center;pointer-events: none;">待开发</a>
            <a href="" class="but" style="text-align: center;pointer-events: none;">待开发</a>
            <a href="" class="but" style="text-align: center;pointer-events: none;">待开发</a>
            <a href="" class="but" style="text-align: center;pointer-events: none;">...</a>
        </div>
    </div>
</body>
</html>

登录页面代码

<!DOCTYPE html>  
<html lang="zh_cn" xmlns:th="http://www.thymeleaf.org">
<head>  
    <meta charset="UTF-8">  
    <title>登录</title>  
    <link rel="stylesheet" th:href="@{/css/login.css}">
    <link rel="icon" th:href="@{/css/login.css}" sizes="16x16">
</head>  
<body>  
    <div id="login">  
        <h1>登录</h1>  
        <form th:action="@{/login}"  method="post">
            <input type="text" required="required" placeholder="用户名" name="username"></input>
            <input type="password" required="required" placeholder="密码" name="password"></input>
            <span class="remember">记住我</span><input type="checkbox" name="remember" style="width: 50px"></input>
            <button class="but" type="submit">登录</button>
        </form>  
    </div>  
</body>  
</html> 

发送邮件页面代码

<!DOCTYPE html>
<html lang="zh_cn" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" th:href="@{/css/sendMail.css}">
    <link rel="icon" th:href="@{/image/1.png}" sizes="16x16">
    <title>发送邮件</title>
</head>
<body>
    <div id="mail">  
        <h1>发送邮件</h1>  
        <div ><span class="msg" th:text="${msg}"></span><a th:href="@{/index}"  class="fanhuei">返回主页</a></div>
        <form th:action="@{/mail/snedMail}" method="post">
            <div><span class="text">标题:</span><input type="text" required="required" placeholder="标题" name="subject"></input></div>  
            <div><span class="text">内容:</span><input type="text" required="required" placeholder="内容" name="text"></input></div>  
            <div><span class="text">接受者邮箱:</span><input type="email" required="required" placeholder="接受者邮箱" name="to"></input></div>  
            <div><span class="text">发送者邮箱:</span><input type="email" required="required" placeholder="发送者邮箱" name="from"></input></div>   
            <button class="but" type="submit">发送</button>  
        </form>  
    </div>  
</body>
</html>

项目的制作原因

1.本人还处于后端的学习阶段虽然学了差差不多有一年多的时间了。

2.目前都是在学习SpringBoot、SpringCloud,微服务这一块的。

3.其实我一开始就是好玩的去做,也是因为在学到了在SpringBoot基础上实现邮件的发送产生了好奇。

3.就去想着结合我现在学的一些内容去整合的做一个网站但是在做的过程中去巩固关于mysql数据库的连接,使用SpringSecurity以及thymeleaf的使用

4.页面在一开始的时候也就是一个from表单这样子外加几个h1标签,目前这个页面是我在网上找的一些模板然后我去改了一下

项目总结

上面用到的核心功能在我的博客的笔记中也有,那些笔记是看狂神大佬的教学视频做的笔记

SpringSecurity配置:https://blog.csdn.net/yuran06/article/details/121561188?spm=1001.2014.3001.5501

Mybatis配置:https://blog.csdn.net/yuran06/article/details/121566373?spm=1001.2014.3001.5501

Thymeleaf配置:https://blog.csdn.net/yuran06/article/details/121566135?spm=1001.2014.3001.5501

关于邮箱的配置:https://blog.csdn.net/yuran06/article/details/121560041?spm=1001.2014.3001.5501

最后我希望我这样子因为好玩而去做了一个简单的个人邮件发送的小项目能对大家提供一些帮助,尤其是和我一样还在学习的小伙伴!!

标签:SpringBoot,登录,发送,mail,邮箱,页面,com,搭建
来源: https://blog.csdn.net/yuran06/article/details/121609347

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有