修改发送邮件

This commit is contained in:
不做码农 2021-12-21 21:29:55 +08:00
parent 08d8c2f28a
commit be870c7d0e
4 changed files with 36 additions and 23 deletions

View File

@ -13,6 +13,10 @@ namespace Infrastructure.Model
public string ToUser { get; set; }
public string Content { get; set; } = "";
public string HtmlContent { get; set; }
/// <summary>
/// 是否发送给自己
/// </summary>
public bool SendMe { get; set; }
public DateTime AddTime { get; set; }
}
}

View File

@ -70,9 +70,14 @@ namespace ZR.Admin.WebApi.Controllers
{
return ToResponse(ApiResult.Error($"请配置邮箱信息"));
}
MailHelper mailHelper = new MailHelper(OptionsSetting.MailOptions.From, OptionsSetting.MailOptions.Smtp, OptionsSetting.MailOptions.Port, OptionsSetting.MailOptions.Password);
MailHelper mailHelper = new(OptionsSetting.MailOptions.From, OptionsSetting.MailOptions.Smtp, OptionsSetting.MailOptions.Port, OptionsSetting.MailOptions.Password);
mailHelper.SendMail(sendEmailVo.ToUser, sendEmailVo.Subject, sendEmailVo.Content, sendEmailVo.FileUrl, sendEmailVo.HtmlContent);
string[] toUsers = sendEmailVo.ToUser.Split(",", StringSplitOptions.RemoveEmptyEntries);
if (sendEmailVo.SendMe)
{
toUsers.Append(mailHelper.FromEmail);
}
mailHelper.SendMail(toUsers, sendEmailVo.Subject, sendEmailVo.Content, sendEmailVo.FileUrl, sendEmailVo.HtmlContent);
logger.Info($"发送邮件{JsonConvert.SerializeObject(sendEmailVo)}");

View File

@ -55,7 +55,7 @@ namespace ZR.Common
public void SendMail(string toAddress, string subject, string text, string path = "", string html = "")
{
IEnumerable<MailboxAddress> mailboxes = new List<MailboxAddress>() {
new MailboxAddress(toAddress)
new MailboxAddress(toAddress, toAddress)
};
SendMail(mailboxes, subject, text, path, html);
@ -73,7 +73,7 @@ namespace ZR.Common
IList<MailboxAddress> mailboxes = new List<MailboxAddress>() { };
foreach (var item in toAddress)
{
mailboxes.Add(new MailboxAddress(item));
mailboxes.Add(new MailboxAddress(item, item));
}
SendMail(mailboxes, subject, text, path, html);
@ -92,19 +92,14 @@ namespace ZR.Common
MimeMessage message = new MimeMessage();
//发件人
message.From.Add(new MailboxAddress(FromEmail, FromEmail));
//收件人
//message.To.Add(new MailboxAddress(toAddress));
//IList<InternetAddress> internets = null;
//internets.Add(new MailboxAddress(toAddress));
message.To.AddRange(toAddress);
message.Subject = subject;
message.Date = DateTime.Now;
//message.Date = DateTime.Now;
//创建附件Multipart
Multipart multipart = new Multipart("mixed");
var alternative = new MultipartAlternative();
//html内容
if (!string.IsNullOrEmpty(html))
{
@ -112,7 +107,7 @@ namespace ZR.Common
{
Text = html
};
multipart.Add(Html);
alternative.Add(Html);
}
//文本内容
if (!string.IsNullOrEmpty(text))
@ -121,7 +116,7 @@ namespace ZR.Common
{
Text = text + "\r\n\n\n" + mailSign
};
multipart.Add(plain);
alternative.Add(plain);
}
//附件
@ -136,12 +131,12 @@ namespace ZR.Common
//文件名字
FileName = Path.GetFileName(path)
};
multipart.Add(attachment);
alternative.Add(attachment);
}
multipart.Add(alternative);
//赋值邮件内容
message.Body = multipart;
//开始发送
using (var client = new SmtpClient())
{

View File

@ -4,6 +4,12 @@
<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-item>
<el-form-item label="邮件主题" prop="subject">
<el-input v-model="form.subject"></el-input>
@ -11,6 +17,9 @@
<el-form-item label="邮件内容" prop="content">
<editor v-model="form.content" :min-height="192" />
</el-form-item>
<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"
@ -44,18 +53,17 @@ export default {
subject: [{ required: true, message: "主题不能为空", trigger: "blur" }],
toUser: [
{ required: true, message: "请输入邮箱地址", trigger: ["blur"] },
{
message: "请输入正确的邮箱地址",
trigger: ["blur", "change"],
type: "email",
},
// {
// message: "",
// trigger: ["blur", "change"],
// type: "email",
// },
],
content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
},
};
},
mounted() {
},
mounted() {},
methods: {
//
reset() {
@ -64,6 +72,7 @@ export default {
content: undefined,
subject: undefined,
fileUrl: undefined,
sendMe: false,
};
this.resetForm("form");
},