优化发送邮件
This commit is contained in:
parent
cf27c7115c
commit
e88d4d24c3
@ -96,11 +96,12 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="formFile"></param>
|
/// <param name="formFile"></param>
|
||||||
/// <param name="fileDir">存储目录</param>
|
/// <param name="fileDir">存储目录</param>
|
||||||
|
/// <param name="uploadType">上传类型 1、发送邮件</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost()]
|
[HttpPost()]
|
||||||
[Verify]
|
[Verify]
|
||||||
[ActionPermissionFilter(Permission = "common")]
|
[ActionPermissionFilter(Permission = "common")]
|
||||||
public IActionResult UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileDir = "uploads")
|
public IActionResult UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileDir = "uploads", int uploadType = 0)
|
||||||
{
|
{
|
||||||
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
||||||
string fileExt = Path.GetExtension(formFile.FileName);
|
string fileExt = Path.GetExtension(formFile.FileName);
|
||||||
@ -137,7 +138,7 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
long fileId = SysFileService.InsertFile(file);
|
long fileId = SysFileService.InsertFile(file);
|
||||||
return ToResponse(ResultCode.SUCCESS, new
|
return ToResponse(ResultCode.SUCCESS, new
|
||||||
{
|
{
|
||||||
url = accessPath,
|
url = uploadType == 1 ? finalFilePath : accessPath,
|
||||||
fileName,
|
fileName,
|
||||||
fileId
|
fileId
|
||||||
});
|
});
|
||||||
|
|||||||
@ -30,7 +30,7 @@ namespace ZR.Common
|
|||||||
/// 是否使用SSL协议
|
/// 是否使用SSL协议
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool UseSsl { get; set; } = false;
|
public bool UseSsl { get; set; } = false;
|
||||||
public string mailSign = @"";
|
public string mailSign = @"邮件来自C# 程序发送";
|
||||||
private readonly MailOptions mailOptions = new();
|
private readonly MailOptions mailOptions = new();
|
||||||
|
|
||||||
public MailHelper()
|
public MailHelper()
|
||||||
@ -93,9 +93,10 @@ namespace ZR.Common
|
|||||||
/// 发送邮件
|
/// 发送邮件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="toAddress"></param>
|
/// <param name="toAddress"></param>
|
||||||
/// <param name="subject"></param>
|
/// <param name="subject">主题</param>
|
||||||
/// <param name="text"></param>
|
/// <param name="text"></param>
|
||||||
/// <param name="path">附件url地址</param>
|
/// <param name="path">附件url地址</param>
|
||||||
|
/// <param name="html">网页HTML内容</param>
|
||||||
private void SendMail(IEnumerable<MailboxAddress> toAddress, string subject, string text, string path = "", string html = "")
|
private void SendMail(IEnumerable<MailboxAddress> toAddress, string subject, string text, string path = "", string html = "")
|
||||||
{
|
{
|
||||||
MimeMessage message = new MimeMessage();
|
MimeMessage message = new MimeMessage();
|
||||||
@ -131,39 +132,40 @@ namespace ZR.Common
|
|||||||
//附件
|
//附件
|
||||||
if (!string.IsNullOrEmpty(path))
|
if (!string.IsNullOrEmpty(path))
|
||||||
{
|
{
|
||||||
MimePart attachment = new MimePart()
|
string[] files = path.Split(",");
|
||||||
|
foreach (var file in files)
|
||||||
{
|
{
|
||||||
Content = new MimeContent(File.OpenRead(path), ContentEncoding.Default),
|
MimePart attachment = new()
|
||||||
//读取文件,只能用绝对路径
|
{
|
||||||
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
|
Content = new MimeContent(File.OpenRead(file), ContentEncoding.Default),
|
||||||
ContentTransferEncoding = ContentEncoding.Base64,
|
//读取文件,只能用绝对路径
|
||||||
//文件名字
|
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
|
||||||
FileName = Path.GetFileName(path)
|
ContentTransferEncoding = ContentEncoding.Base64,
|
||||||
};
|
//文件名字
|
||||||
alternative.Add(attachment);
|
FileName = Path.GetFileName(path)
|
||||||
|
};
|
||||||
|
alternative.Add(attachment);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
multipart.Add(alternative);
|
multipart.Add(alternative);
|
||||||
//赋值邮件内容
|
//赋值邮件内容
|
||||||
message.Body = multipart;
|
message.Body = multipart;
|
||||||
|
|
||||||
//开始发送
|
//开始发送
|
||||||
using (var client = new SmtpClient())
|
using var client = new SmtpClient();
|
||||||
{
|
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
||||||
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
|
||||||
|
|
||||||
//Smtp服务器
|
//Smtp服务器
|
||||||
//client.Connect("smtp.qq.com", 587, false);
|
//client.Connect("smtp.qq.com", 587, false);
|
||||||
client.Connect(Smtp, Port, true);
|
client.Connect(Smtp, Port, true);
|
||||||
//登录,发送
|
//登录,发送
|
||||||
//特别说明,对于服务器端的中文相应,Exception中有编码问题,显示乱码了
|
//特别说明,对于服务器端的中文相应,Exception中有编码问题,显示乱码了
|
||||||
client.Authenticate(FromEmail, FromPwd);
|
client.Authenticate(FromEmail, FromPwd);
|
||||||
|
|
||||||
client.Send(message);
|
client.Send(message);
|
||||||
//断开
|
//断开
|
||||||
client.Disconnect(true);
|
client.Disconnect(true);
|
||||||
Console.WriteLine($"发送邮件成功{DateTime.Now}");
|
Console.WriteLine($"发送邮件成功{DateTime.Now}");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ namespace ZR.Tasks
|
|||||||
//{ "quartz.jobStore.driverDelegateType", "Quartz.Impl.AdoJobStore.MySQLDelegate, Quartz"},
|
//{ "quartz.jobStore.driverDelegateType", "Quartz.Impl.AdoJobStore.MySQLDelegate, Quartz"},
|
||||||
//{ "quartz.jobStore.useProperties", "true"},
|
//{ "quartz.jobStore.useProperties", "true"},
|
||||||
//{ "quartz.jobStore.dataSource", "myDS" },
|
//{ "quartz.jobStore.dataSource", "myDS" },
|
||||||
//{ "quartz.dataSource.myDS.connectionString", @"server=118.24.27.111;port=3306;database=zrryAdmin;uid=zrry;pwd=@zrry1993^Hz;Charset=utf8;"},
|
//{ "quartz.dataSource.myDS.connectionString", @"server=xxx.xxx.xxx.xxx;port=3306;database=Admin;uid=zrry;pwd=********;Charset=utf8;"},
|
||||||
//{ "quartz.dataSource.myDS.provider", "MySql" },
|
//{ "quartz.dataSource.myDS.provider", "MySql" },
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -277,7 +277,7 @@ namespace ZR.Tasks
|
|||||||
//防止创建时存在数据问题 先移除,然后在执行创建操作
|
//防止创建时存在数据问题 先移除,然后在执行创建操作
|
||||||
await _scheduler.Result.DeleteJob(jobKey);
|
await _scheduler.Result.DeleteJob(jobKey);
|
||||||
}
|
}
|
||||||
await AddTaskScheduleAsync(tasksQz);
|
//await AddTaskScheduleAsync(tasksQz);
|
||||||
return ApiResult.Success("修改计划成功");
|
return ApiResult.Success("修改计划成功");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@ -11,7 +11,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
|
|
||||||
<ProjectReference Include="..\ZR.Model\ZR.Model.csproj" />
|
<ProjectReference Include="..\ZR.Model\ZR.Model.csproj" />
|
||||||
<ProjectReference Include="..\ZR.Service\ZR.Service.csproj" />
|
<ProjectReference Include="..\ZR.Service\ZR.Service.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@ -156,12 +156,12 @@ export default {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.msgSuccess("上传成功");
|
this.msgSuccess("上传成功");
|
||||||
this.fileList.push({ name: res.data.fileName, url: res.data.url });
|
this.fileList.push({ name: res.data.fileName, url: res.data.url, path: res.data.path });
|
||||||
this.$emit("input", this.column, this.listToString(this.fileList));
|
this.$emit("input", this.column, this.listToString(this.fileList));
|
||||||
},
|
},
|
||||||
// 上传进度
|
// 上传进度
|
||||||
uploadProcess(event, file, fileList) {
|
uploadProcess(event, file, fileList) {
|
||||||
console.log("上传进度" + file.percentage);
|
// console.log("上传进度" + file.percentage);
|
||||||
},
|
},
|
||||||
// 删除文件
|
// 删除文件
|
||||||
handleDelete(index) {
|
handleDelete(index) {
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
</el-col>
|
</el-col>
|
||||||
<el-col :xs="24" :md="12" :style="{height: '350px'}">
|
<el-col :xs="24" :md="12" :style="{height: '350px'}">
|
||||||
<div class="avatar-upload-preview">
|
<div class="avatar-upload-preview">
|
||||||
<img :src="previews.url" :style="previews.img" />
|
<el-image :src="previews.url" :style="previews.img" />
|
||||||
</div>
|
</div>
|
||||||
</el-col>
|
</el-col>
|
||||||
</el-row>
|
</el-row>
|
||||||
|
|||||||
@ -1,15 +1,12 @@
|
|||||||
<template>
|
<template>
|
||||||
<el-row>
|
<el-row>
|
||||||
<el-form class="mt10" ref="form" :model="form" label-width="100px" :rules="rules" style="width:600px">
|
<el-form class="mt10" ref="form" :model="form" label-width="110px" :rules="rules" style="width:600px">
|
||||||
<el-form-item label="收件邮箱" prop="toUser">
|
<el-form-item v-for="(domain, index) in form.toEmails" :prop="'toEmails.' + index + '.value'" :label="'收件邮箱' + (index === 0 ? '': index)"
|
||||||
<el-input v-model="form.toUser">
|
:key="domain.key"
|
||||||
</el-input>
|
:rules="[{ required: true, message: '邮箱不能为空', trigger: 'blur' }, { message: '请输入正确的邮箱地址', trigger: ['blur', 'change'], type: 'email', }]">
|
||||||
<span slot="label">
|
<el-input v-model="domain.value" style="width:300px"></el-input>
|
||||||
<el-tooltip content="多个用','隔开" placement="top">
|
<el-button class="ml10" @click="addDomain" icon="el-icon-plus" />
|
||||||
<i class="el-icon-question"></i>
|
<el-button class="ml10" @click.prevent="removeDomain(domain)" icon="el-icon-minus" />
|
||||||
</el-tooltip>
|
|
||||||
收件邮箱
|
|
||||||
</span>
|
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="邮件主题" prop="subject">
|
<el-form-item label="邮件主题" prop="subject">
|
||||||
<el-input v-model="form.subject"></el-input>
|
<el-input v-model="form.subject"></el-input>
|
||||||
@ -20,23 +17,18 @@
|
|||||||
<el-form-item label="发送自己" prop="sendMe">
|
<el-form-item label="发送自己" prop="sendMe">
|
||||||
<el-switch v-model="form.sendMe" active-text="是" inactive-text="否"></el-switch>
|
<el-switch v-model="form.sendMe" active-text="是" inactive-text="否"></el-switch>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
|
||||||
<el-form-item label="附件">
|
<el-form-item label="附件">
|
||||||
<el-upload name="file" ref="upload" :data="{savetype: form.saveType, filePath: form.filePath}" :headers="headers" :auto-upload="false"
|
<UploadFile v-model="form.fileUrl" :limit="5" :fileSize="15" :data="{ 'fileDir' : 'email', 'uploadType': 1}" column="fileUrl"
|
||||||
:on-success="uploadSuccess" :action="uploadActionUrl">
|
@input="uploadSuccess" />
|
||||||
<el-button slot="trigger" size="mini" icon="el-icon-upload">选择文件</el-button>
|
|
||||||
<el-button style="margin-left: 10px;" size="mini" type="primary" @click="submitUpload">上传到服务器</el-button>
|
|
||||||
</el-upload>
|
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item>
|
<el-form-item>
|
||||||
<el-button type="primary" size="mini" @click="formSubmit">立即提交</el-button>
|
<el-button type="primary" size="mini" @click="formSubmit">发送邮件</el-button>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-form>
|
</el-form>
|
||||||
</el-row>
|
</el-row>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
import { sendEmail } from "@/api/common";
|
import { sendEmail } from "@/api/common";
|
||||||
import { getToken } from "@/utils/auth";
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "sendEmail",
|
name: "sendEmail",
|
||||||
@ -44,26 +36,19 @@ export default {
|
|||||||
return {
|
return {
|
||||||
form: {
|
form: {
|
||||||
fileUrl: "",
|
fileUrl: "",
|
||||||
|
toEmails: [
|
||||||
|
{
|
||||||
|
value: "",
|
||||||
|
},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
headers: {
|
uploadActionUrl: process.env.VUE_APP_BASE_API + "common/uploadFile",
|
||||||
Authorization: "Bearer " + getToken(),
|
|
||||||
},
|
|
||||||
uploadActionUrl: process.env.VUE_APP_BASE_API + "upload/SaveFile",
|
|
||||||
rules: {
|
rules: {
|
||||||
subject: [{ required: true, message: "主题不能为空", trigger: "blur" }],
|
subject: [{ required: true, message: "主题不能为空", trigger: "blur" }],
|
||||||
toUser: [
|
|
||||||
{ required: true, message: "请输入邮箱地址", trigger: ["blur"] },
|
|
||||||
// {
|
|
||||||
// message: "请输入正确的邮箱地址",
|
|
||||||
// trigger: ["blur", "change"],
|
|
||||||
// type: "email",
|
|
||||||
// },
|
|
||||||
],
|
|
||||||
content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
|
content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
mounted() {},
|
|
||||||
methods: {
|
methods: {
|
||||||
// 表单重置
|
// 表单重置
|
||||||
reset() {
|
reset() {
|
||||||
@ -73,24 +58,22 @@ export default {
|
|||||||
subject: undefined,
|
subject: undefined,
|
||||||
fileUrl: undefined,
|
fileUrl: undefined,
|
||||||
sendMe: false,
|
sendMe: false,
|
||||||
|
toEmails: [
|
||||||
|
{
|
||||||
|
value: "",
|
||||||
|
},
|
||||||
|
],
|
||||||
};
|
};
|
||||||
this.resetForm("form");
|
this.resetForm("form");
|
||||||
},
|
},
|
||||||
submitUpload() {
|
|
||||||
this.$refs.upload.submit();
|
|
||||||
},
|
|
||||||
// 上传成功
|
// 上传成功
|
||||||
uploadSuccess(response, file, fileList) {
|
uploadSuccess(columnName, filelist) {
|
||||||
console.log(response);
|
this.form[columnName] = filelist;
|
||||||
if (response.code == 200) {
|
|
||||||
this.$message.success("上传成功");
|
|
||||||
this.form.fileUrl = response.data;
|
|
||||||
} else {
|
|
||||||
this.$message.error(response.msg);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* 提交
|
||||||
|
*/
|
||||||
formSubmit() {
|
formSubmit() {
|
||||||
console.log(JSON.stringify(this.form));
|
|
||||||
this.$refs["form"].validate((valid) => {
|
this.$refs["form"].validate((valid) => {
|
||||||
//开启校验
|
//开启校验
|
||||||
if (valid) {
|
if (valid) {
|
||||||
@ -100,14 +83,21 @@ export default {
|
|||||||
spinner: "el-icon-loading",
|
spinner: "el-icon-loading",
|
||||||
background: "rgba(0, 0, 0, 0.7)",
|
background: "rgba(0, 0, 0, 0.7)",
|
||||||
});
|
});
|
||||||
|
var emails = [];
|
||||||
|
this.form.toEmails.filter((x) => {
|
||||||
|
emails.push(x.value);
|
||||||
|
});
|
||||||
|
var p = {
|
||||||
|
...this.form,
|
||||||
|
toUser: emails.toString(),
|
||||||
|
};
|
||||||
// 如果校验通过,请求接口,允许提交表单
|
// 如果校验通过,请求接口,允许提交表单
|
||||||
sendEmail(this.form).then((res) => {
|
sendEmail(p).then((res) => {
|
||||||
this.open = false;
|
this.open = false;
|
||||||
if (res.code == 200) {
|
if (res.code == 200) {
|
||||||
this.$message.success("发送成功");
|
this.$message.success("发送成功");
|
||||||
this.reset();
|
this.reset();
|
||||||
}
|
}
|
||||||
this.$refs.upload.clearFiles();
|
|
||||||
loading.close();
|
loading.close();
|
||||||
});
|
});
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@ -120,6 +110,23 @@ export default {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
removeDomain(item) {
|
||||||
|
var index = this.form.toEmails.indexOf(item);
|
||||||
|
if (index !== -1 && this.form.toEmails.length !== 1) {
|
||||||
|
this.form.toEmails.splice(index, 1);
|
||||||
|
} else {
|
||||||
|
this.$message({
|
||||||
|
message: "请至少保留一位联系人",
|
||||||
|
type: "warning",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
addDomain() {
|
||||||
|
this.form.toEmails.push({
|
||||||
|
value: "",
|
||||||
|
key: Date.now(),
|
||||||
|
});
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user