/// <summary>
/// 发送邮件的方法
/// </summary>
public OperateResult<int> SendMail(MailModel mails)
{
var resultData = new OperateResult<int>();
if (mails.to == null || !mails.to.Any())
{
resultData.Status = OperateStatus.Failure;
resultData.Description = "收件人地址不能为空";
return resultData;
}
var mailConfig = App.GetConfig<MailConfigOptions>("applicationsettings:mailConfig");
if (mailConfig == null)
{
resultData.Status = OperateStatus.Failure;
resultData.Description = "邮件配置信息无效";
return resultData;
}
//将发件人邮箱带入MailAddress中初始化
MailAddress mailAddress = new MailAddress("邮件地址");
//创建Email的Message对象
MailMessage mailMessage = new MailMessage();
//判断收件人数组中是否有数据
if (mails.to != null && mails.to.Any())
{
//循环添加收件人地址
foreach (var item in mails.to)
{
if (!string.IsNullOrEmpty(item) && item.IsEmail())
mailMessage.To.Add(item.ToString());
}
}
if (mailMessage.To.Count <= 0)
{
resultData.Status = OperateStatus.Failure;
resultData.Description = "收件人地址无效";
return resultData;
}
//判断抄送地址数组是否有数据
if (mails.cc != null && mails.cc.Any())
{
//循环添加抄送地址
foreach (var item in mails.cc)
{
if (!string.IsNullOrEmpty(item) && item.IsEmail())
mailMessage.CC.Add(item.ToString());
}
}
//发件人邮箱
mailMessage.From = mailAddress;
//标题
mailMessage.Subject = mails.title;
//编码
mailMessage.SubjectEncoding = Encoding.UTF8;
//正文
mailMessage.Body = mails.body;
//正文编码
mailMessage.BodyEncoding = Encoding.Default;
//邮件优先级
mailMessage.Priority = MailPriority.High;
//正文是否是html格式
mailMessage.IsBodyHtml = mails.isHtml;
#region 添加附件
if (mails.files != null && mails.files.Any())
{
//取得Web根目录和内容根目录的物理路径
string webRootPath = App.WebHostEnvironment.WebRootPath;
foreach (var fileItem in mails.files)
{
路径拼接
//webRootPath = _hostingEnvironment.WebRootPath + "\\" + "upload-file" + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetFileNameWithoutExtension(item.FileName) + Path.GetExtension(item.FileName).ToLower();
创建文件流
//using (var FileStream = new FileStream(webRootPath, FileMode.Create))
//{
拷贝文件流
//await item.CopyToAsync(FileStream);
释放缓存
//FileStream.Flush();
//}
//再根据路径打开文件,得到文件流
//FileStream fileStream = File.OpenWrite(filePath);
//using (fileStream)
//{
// Attachment attachment = new Attachment(fileStream, filePath);
// mailMessage.Attachments.Add(attachment);
//}
try
{
string pathUrl = $"{webRootPath}/{fileItem.FilePath}/{fileItem.FileObjectName}";
Attachment attachment = new Attachment(new FileStream(pathUrl, FileMode.Open), fileItem.FileOriginName, "text/plain");
mailMessage.Attachments.Add(attachment);
}
catch (Exception ex)
{
}
//using (Stream stream = new FileStream(filePath, FileMode.Open))
//{
// mailMessage.Attachments.Add(new Attachment(stream, Path.GetFileName(filePath), "text/plain"));
//}
//添加至附件中
//mailMessage.Attachments.Add(new Attachment(stream, item.FileName));
//mailMessage.Attachments.Add(new Attachment(item.OpenReadStream(),item.FileName));
}
}
#endregion
try
{
//实例化一个Smtp客户端
SmtpClient smtp = new SmtpClient(ip地址, 端口号);
//将发件人的邮件地址和客户端授权码带入以验证发件人身份
//smtp.Credentials = new System.Net.NetworkCredential(邮件地址, SMTP服务授权码);
//指定SMTP邮件服务器
//smtp.Host = GetMailHost(邮件地址);
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new System.Net.NetworkCredential(邮件地址, SMTP服务授权码); //邮箱账号 密码
smtp.Timeout = 6000; //6秒超时
//邮件发送到SMTP服务器
smtp.Send(mailMessage);
resultData.Status = OperateStatus.Success;
resultData.Data = 1;
}
catch (Exception ex)
{
resultData.Status = OperateStatus.Failure;
resultData.Description = "邮件发送异常!" + ex.Message;
}
return resultData;
}