Update MailHelper

This commit is contained in:
2021-05-18 09:32:25 +08:00
parent 7c0b4f37af
commit 31b5a1f5b7
6 changed files with 105 additions and 13 deletions

View File

@@ -1,15 +1,59 @@
using System.Net.Mail;
using Furion;
using Furion.FriendlyException;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using System;
using System.Net.Mail;
using System.Text;
namespace Ewide.Core.Util
{
public class MailHelper
{
public string Message { set; get; }
public string UserMailAddress { set; get; }
/// <summary>
/// 邮箱类
/// </summary>
/// <param name="message">发送的信息</param>
/// <param name="userMailAddress">用户的地址</param>
public MailHelper(string message, string userMailAddress)
{
//MailHelper mail = new MailHelper("第一次发送邮箱测试", "591410538@qq.com");
//mail.Send();
Message = message;
UserMailAddress = userMailAddress;
}
public void Send()
{
MailMessage msg = null;
//smtpClient.Credentials = new System.Net.NetworkCredential(mail.from.Send_Address.Address, mail.from.password);//设置发件人身份的票据
//smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
//smtpClient.Host = "smtp." + mail.from.Send_Address.Host;
string account = App.Configuration["Mail:Account"];
string passWord = App.Configuration["Mail:PassWord"];
//var options = Options.Create(options: new MemoryCacheOptions());
//IMemoryCache cache = new MemoryCache(options);
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Host = App.Configuration["Mail:Host"];
smtpClient.Credentials = new System.Net.NetworkCredential(account, passWord);
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
MailMessage mailMessage = new MailMessage();
MailAddress fromAddr = new MailAddress(account);
mailMessage.From = fromAddr;
mailMessage.To.Add(UserMailAddress);
mailMessage.Subject = App.Configuration["Mail:Subject"];
mailMessage.BodyEncoding = Encoding.UTF8;
mailMessage.IsBodyHtml = true;
mailMessage.Priority = MailPriority.Low;
mailMessage.Body = Message;
try
{
smtpClient.Send(mailMessage);
}
catch
{
throw Oops.Oh(ErrorCode.xg1100);
}
}
}
}