Compare commits
6
Commits
594eb23979
...
4d4663e405
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d4663e405 | ||
|
|
ab5d895ca1 | ||
|
|
5a3df648a2 | ||
|
|
5d6023a628 | ||
|
|
0ac267316b | ||
|
|
c72a374f21 |
@@ -35,6 +35,11 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,7 +1,5 @@
|
||||
package cn.hezhaohui.mail;
|
||||
|
||||
import cn.hezhaohui.mail.service.MailService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package cn.hezhaohui.mail.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**") // 应用到所有路径
|
||||
.allowedOrigins("http://localhost:5173") // 允许前端开发服务器的源
|
||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法
|
||||
.allowedHeaders("*") // 允许所有header
|
||||
.allowCredentials(true) // 允许发送凭证信息(如cookies)
|
||||
.maxAge(3600); // 预检请求的有效期,单位为秒
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package cn.hezhaohui.mail.controller;
|
||||
|
||||
import cn.hezhaohui.mail.entity.Response;
|
||||
import cn.hezhaohui.mail.service.MailService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/email")
|
||||
@Slf4j
|
||||
public class MailController {
|
||||
|
||||
@Resource
|
||||
private MailService mailService;
|
||||
|
||||
@PostMapping("/send")
|
||||
public Response sendMail(@RequestParam String to, @RequestParam String subject, @RequestParam String content) {
|
||||
mailService.sendMessage(to, subject, content);
|
||||
log.info("[To]: {}\n[Subject]: {}\n[Content]: {}\n", to, subject, content);
|
||||
return new Response(200, "success");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package cn.hezhaohui.mail.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Response {
|
||||
final private int status;
|
||||
final private String message;
|
||||
|
||||
}
|
||||
@@ -36,26 +36,6 @@
|
||||
/>
|
||||
</el-form-item>
|
||||
|
||||
<!-- 附件 -->
|
||||
<el-form-item label="附件">
|
||||
<el-upload
|
||||
v-model:file-list="fileList"
|
||||
action="/api/upload"
|
||||
:auto-upload="false"
|
||||
:on-change="handleFileChange"
|
||||
:on-remove="handleFileRemove"
|
||||
multiple
|
||||
:limit="5"
|
||||
:on-exceed="handleExceed"
|
||||
>
|
||||
<el-button type="primary">选择文件</el-button>
|
||||
<template #tip>
|
||||
<div class="el-upload__tip">
|
||||
支持格式:.jpg, .png, .pdf, .docx,最多5个文件,每个文件不超过10MB
|
||||
</div>
|
||||
</template>
|
||||
</el-upload>
|
||||
</el-form-item>
|
||||
|
||||
<!-- 提交按钮 -->
|
||||
<el-form-item>
|
||||
@@ -85,7 +65,7 @@
|
||||
|
||||
<script setup>
|
||||
import { ref, reactive } from 'vue'
|
||||
import { ElForm, ElFormItem, ElInput, ElButton, ElUpload, ElAlert } from 'element-plus'
|
||||
import { ElForm, ElFormItem, ElInput, ElButton, ElAlert } from 'element-plus'
|
||||
import axios from 'axios'
|
||||
|
||||
// 表单引用
|
||||
@@ -98,9 +78,6 @@ const emailForm = reactive({
|
||||
content: ''
|
||||
})
|
||||
|
||||
// 文件列表
|
||||
const fileList = ref([])
|
||||
|
||||
// 提交状态
|
||||
const submitting = ref(false)
|
||||
|
||||
@@ -122,26 +99,10 @@ const rules = {
|
||||
]
|
||||
}
|
||||
|
||||
// 处理文件变化
|
||||
const handleFileChange = (file, fileList) => {
|
||||
// 可以在这里添加文件验证逻辑
|
||||
console.log('文件变化:', file, fileList)
|
||||
}
|
||||
|
||||
// 处理文件移除
|
||||
const handleFileRemove = (file, fileList) => {
|
||||
console.log('文件移除:', file, fileList)
|
||||
}
|
||||
|
||||
// 处理文件超出限制
|
||||
const handleExceed = () => {
|
||||
ElMessage.warning('最多只能上传5个文件')
|
||||
}
|
||||
|
||||
// 重置表单
|
||||
const resetForm = () => {
|
||||
formRef.value.resetFields()
|
||||
fileList.value = []
|
||||
message.value = ''
|
||||
}
|
||||
|
||||
@@ -159,16 +120,11 @@ const submitForm = async () => {
|
||||
formData.append('subject', emailForm.subject)
|
||||
formData.append('content', emailForm.content)
|
||||
|
||||
// 添加附件
|
||||
fileList.value.forEach(file => {
|
||||
formData.append('attachments', file.raw)
|
||||
})
|
||||
|
||||
submitting.value = true
|
||||
message.value = ''
|
||||
|
||||
// 发送请求
|
||||
const response = await axios.post('http:localhost:8080/api/email/send', formData, {
|
||||
const response = await axios.post('http://localhost:8080/api/email/send', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user