优化发送邮件
This commit is contained in:
parent
cf27c7115c
commit
e88d4d24c3
@ -74,7 +74,7 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
{
|
||||
return ToResponse(ApiResult.Error($"请配置邮箱信息"));
|
||||
}
|
||||
|
||||
|
||||
MailHelper mailHelper = new();
|
||||
|
||||
string[] toUsers = sendEmailVo.ToUser.Split(",", StringSplitOptions.RemoveEmptyEntries);
|
||||
@ -85,7 +85,7 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
mailHelper.SendMail(toUsers, sendEmailVo.Subject, sendEmailVo.Content, sendEmailVo.FileUrl, sendEmailVo.HtmlContent);
|
||||
|
||||
logger.Info($"发送邮件{JsonConvert.SerializeObject(sendEmailVo)}");
|
||||
|
||||
|
||||
return SUCCESS(true);
|
||||
}
|
||||
|
||||
@ -96,11 +96,12 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
/// </summary>
|
||||
/// <param name="formFile"></param>
|
||||
/// <param name="fileDir">存储目录</param>
|
||||
/// <param name="uploadType">上传类型 1、发送邮件</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost()]
|
||||
[Verify]
|
||||
[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, "上传文件不能为空");
|
||||
string fileExt = Path.GetExtension(formFile.FileName);
|
||||
@ -137,7 +138,7 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
long fileId = SysFileService.InsertFile(file);
|
||||
return ToResponse(ResultCode.SUCCESS, new
|
||||
{
|
||||
url = accessPath,
|
||||
url = uploadType == 1 ? finalFilePath : accessPath,
|
||||
fileName,
|
||||
fileId
|
||||
});
|
||||
@ -168,7 +169,7 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
{
|
||||
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + (MaxContentLength / 1024).ToString() + " MB");
|
||||
}
|
||||
|
||||
|
||||
(bool, string, string) result = SysFileService.SaveFile(fileDir, formFile);
|
||||
long fileId = SysFileService.InsertFile(new SysFile()
|
||||
{
|
||||
|
||||
@ -30,7 +30,7 @@ namespace ZR.Common
|
||||
/// 是否使用SSL协议
|
||||
/// </summary>
|
||||
public bool UseSsl { get; set; } = false;
|
||||
public string mailSign = @"";
|
||||
public string mailSign = @"邮件来自C# 程序发送";
|
||||
private readonly MailOptions mailOptions = new();
|
||||
|
||||
public MailHelper()
|
||||
@ -93,9 +93,10 @@ namespace ZR.Common
|
||||
/// 发送邮件
|
||||
/// </summary>
|
||||
/// <param name="toAddress"></param>
|
||||
/// <param name="subject"></param>
|
||||
/// <param name="subject">主题</param>
|
||||
/// <param name="text"></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 = "")
|
||||
{
|
||||
MimeMessage message = new MimeMessage();
|
||||
@ -131,39 +132,40 @@ namespace ZR.Common
|
||||
//附件
|
||||
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),
|
||||
//读取文件,只能用绝对路径
|
||||
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
|
||||
ContentTransferEncoding = ContentEncoding.Base64,
|
||||
//文件名字
|
||||
FileName = Path.GetFileName(path)
|
||||
};
|
||||
alternative.Add(attachment);
|
||||
MimePart attachment = new()
|
||||
{
|
||||
Content = new MimeContent(File.OpenRead(file), ContentEncoding.Default),
|
||||
//读取文件,只能用绝对路径
|
||||
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
|
||||
ContentTransferEncoding = ContentEncoding.Base64,
|
||||
//文件名字
|
||||
FileName = Path.GetFileName(path)
|
||||
};
|
||||
alternative.Add(attachment);
|
||||
}
|
||||
}
|
||||
multipart.Add(alternative);
|
||||
//赋值邮件内容
|
||||
message.Body = multipart;
|
||||
|
||||
|
||||
//开始发送
|
||||
using (var client = new SmtpClient())
|
||||
{
|
||||
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
||||
|
||||
//Smtp服务器
|
||||
//client.Connect("smtp.qq.com", 587, false);
|
||||
client.Connect(Smtp, Port, true);
|
||||
//登录,发送
|
||||
//特别说明,对于服务器端的中文相应,Exception中有编码问题,显示乱码了
|
||||
client.Authenticate(FromEmail, FromPwd);
|
||||
using var client = new SmtpClient();
|
||||
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
||||
|
||||
client.Send(message);
|
||||
//断开
|
||||
client.Disconnect(true);
|
||||
Console.WriteLine($"发送邮件成功{DateTime.Now}");
|
||||
}
|
||||
//Smtp服务器
|
||||
//client.Connect("smtp.qq.com", 587, false);
|
||||
client.Connect(Smtp, Port, true);
|
||||
//登录,发送
|
||||
//特别说明,对于服务器端的中文相应,Exception中有编码问题,显示乱码了
|
||||
client.Authenticate(FromEmail, FromPwd);
|
||||
|
||||
client.Send(message);
|
||||
//断开
|
||||
client.Disconnect(true);
|
||||
Console.WriteLine($"发送邮件成功{DateTime.Now}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -55,7 +55,7 @@ namespace ZR.Tasks
|
||||
//{ "quartz.jobStore.driverDelegateType", "Quartz.Impl.AdoJobStore.MySQLDelegate, Quartz"},
|
||||
//{ "quartz.jobStore.useProperties", "true"},
|
||||
//{ "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" },
|
||||
};
|
||||
|
||||
@ -277,7 +277,7 @@ namespace ZR.Tasks
|
||||
//防止创建时存在数据问题 先移除,然后在执行创建操作
|
||||
await _scheduler.Result.DeleteJob(jobKey);
|
||||
}
|
||||
await AddTaskScheduleAsync(tasksQz);
|
||||
//await AddTaskScheduleAsync(tasksQz);
|
||||
return ApiResult.Success("修改计划成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@ -11,7 +11,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
|
||||
<ProjectReference Include="..\ZR.Model\ZR.Model.csproj" />
|
||||
<ProjectReference Include="..\ZR.Service\ZR.Service.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -156,12 +156,12 @@ export default {
|
||||
return;
|
||||
}
|
||||
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));
|
||||
},
|
||||
// 上传进度
|
||||
uploadProcess(event, file, fileList) {
|
||||
console.log("上传进度" + file.percentage);
|
||||
// console.log("上传进度" + file.percentage);
|
||||
},
|
||||
// 删除文件
|
||||
handleDelete(index) {
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
</el-col>
|
||||
<el-col :xs="24" :md="12" :style="{height: '350px'}">
|
||||
<div class="avatar-upload-preview">
|
||||
<img :src="previews.url" :style="previews.img" />
|
||||
<el-image :src="previews.url" :style="previews.img" />
|
||||
</div>
|
||||
</el-col>
|
||||
</el-row>
|
||||
|
||||
@ -1,15 +1,12 @@
|
||||
<template>
|
||||
<el-row>
|
||||
<el-form class="mt10" ref="form" :model="form" label-width="100px" :rules="rules" style="width:600px">
|
||||
<el-form-item label="收件邮箱" prop="toUser">
|
||||
<el-input v-model="form.toUser">
|
||||
</el-input>
|
||||
<span slot="label">
|
||||
<el-tooltip content="多个用','隔开" placement="top">
|
||||
<i class="el-icon-question"></i>
|
||||
</el-tooltip>
|
||||
收件邮箱
|
||||
</span>
|
||||
<el-form class="mt10" ref="form" :model="form" label-width="110px" :rules="rules" style="width:600px">
|
||||
<el-form-item v-for="(domain, index) in form.toEmails" :prop="'toEmails.' + index + '.value'" :label="'收件邮箱' + (index === 0 ? '': index)"
|
||||
:key="domain.key"
|
||||
:rules="[{ required: true, message: '邮箱不能为空', trigger: 'blur' }, { message: '请输入正确的邮箱地址', trigger: ['blur', 'change'], type: 'email', }]">
|
||||
<el-input v-model="domain.value" style="width:300px"></el-input>
|
||||
<el-button class="ml10" @click="addDomain" icon="el-icon-plus" />
|
||||
<el-button class="ml10" @click.prevent="removeDomain(domain)" icon="el-icon-minus" />
|
||||
</el-form-item>
|
||||
<el-form-item label="邮件主题" prop="subject">
|
||||
<el-input v-model="form.subject"></el-input>
|
||||
@ -20,23 +17,18 @@
|
||||
<el-form-item label="发送自己" prop="sendMe">
|
||||
<el-switch v-model="form.sendMe" active-text="是" inactive-text="否"></el-switch>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="附件">
|
||||
<el-upload name="file" ref="upload" :data="{savetype: form.saveType, filePath: form.filePath}" :headers="headers" :auto-upload="false"
|
||||
:on-success="uploadSuccess" :action="uploadActionUrl">
|
||||
<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>
|
||||
<UploadFile v-model="form.fileUrl" :limit="5" :fileSize="15" :data="{ 'fileDir' : 'email', 'uploadType': 1}" column="fileUrl"
|
||||
@input="uploadSuccess" />
|
||||
</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>
|
||||
</el-row>
|
||||
</template>
|
||||
<script>
|
||||
import { sendEmail } from "@/api/common";
|
||||
import { getToken } from "@/utils/auth";
|
||||
|
||||
export default {
|
||||
name: "sendEmail",
|
||||
@ -44,26 +36,19 @@ export default {
|
||||
return {
|
||||
form: {
|
||||
fileUrl: "",
|
||||
toEmails: [
|
||||
{
|
||||
value: "",
|
||||
},
|
||||
],
|
||||
},
|
||||
headers: {
|
||||
Authorization: "Bearer " + getToken(),
|
||||
},
|
||||
uploadActionUrl: process.env.VUE_APP_BASE_API + "upload/SaveFile",
|
||||
uploadActionUrl: process.env.VUE_APP_BASE_API + "common/uploadFile",
|
||||
rules: {
|
||||
subject: [{ required: true, message: "主题不能为空", trigger: "blur" }],
|
||||
toUser: [
|
||||
{ required: true, message: "请输入邮箱地址", trigger: ["blur"] },
|
||||
// {
|
||||
// message: "请输入正确的邮箱地址",
|
||||
// trigger: ["blur", "change"],
|
||||
// type: "email",
|
||||
// },
|
||||
],
|
||||
content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
|
||||
},
|
||||
};
|
||||
},
|
||||
mounted() {},
|
||||
methods: {
|
||||
// 表单重置
|
||||
reset() {
|
||||
@ -73,24 +58,22 @@ export default {
|
||||
subject: undefined,
|
||||
fileUrl: undefined,
|
||||
sendMe: false,
|
||||
toEmails: [
|
||||
{
|
||||
value: "",
|
||||
},
|
||||
],
|
||||
};
|
||||
this.resetForm("form");
|
||||
},
|
||||
submitUpload() {
|
||||
this.$refs.upload.submit();
|
||||
},
|
||||
// 上传成功
|
||||
uploadSuccess(response, file, fileList) {
|
||||
console.log(response);
|
||||
if (response.code == 200) {
|
||||
this.$message.success("上传成功");
|
||||
this.form.fileUrl = response.data;
|
||||
} else {
|
||||
this.$message.error(response.msg);
|
||||
}
|
||||
uploadSuccess(columnName, filelist) {
|
||||
this.form[columnName] = filelist;
|
||||
},
|
||||
/**
|
||||
* 提交
|
||||
*/
|
||||
formSubmit() {
|
||||
console.log(JSON.stringify(this.form));
|
||||
this.$refs["form"].validate((valid) => {
|
||||
//开启校验
|
||||
if (valid) {
|
||||
@ -100,14 +83,21 @@ export default {
|
||||
spinner: "el-icon-loading",
|
||||
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;
|
||||
if (res.code == 200) {
|
||||
this.$message.success("发送成功");
|
||||
this.reset();
|
||||
}
|
||||
this.$refs.upload.clearFiles();
|
||||
loading.close();
|
||||
});
|
||||
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>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user