using Microsoft.Extensions.Configuration;
using Serilog;
using System;
using System.Net;
using System.Net.Mail;
using System.Threading;
namespace SMTP
{
public static class Mailer
{
private static readonly SmtpClient smtpClient;
private static readonly Mutex mailerMutex = new();
private static readonly string mailPassword;
private static readonly string mailUser;
private static readonly string mailServer;
private static readonly int mailPort;
private static readonly bool enableSsl;
private static readonly string smtpFromEmail;
private static readonly bool smtpRedirect;
private static readonly string smtpRedirectEmail;
private static readonly string bccEmail;
private static readonly string ccEmail;
private static readonly string serveDomainName;
static Mailer()
{
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json")
.AddInMemoryCollection()
.Build();
mailPassword = config["Email:SmtpPass"];
mailUser = config["Email:SmtpUser"];
mailServer = config["Email:SmtpServer"];
mailPort = Convert.ToInt32(config["Email:SmtpPort"]);
enableSsl = config["Email:EnableSsl"] != null && Convert.ToBoolean(config["Email:EnableSsl"]);
smtpFromEmail = config["Email:SmtpFromEmail"];
smtpRedirect = config["Email:SmtpRedirect"] != null && Convert.ToBoolean(config["Email:SmtpRedirect"]);
smtpRedirectEmail = config["Email:SmtpRedirectEmail"];
bccEmail = config["Email:BCCEmail"];
ccEmail = config["Email:CCEmail"];
serveDomainName = config["Email:ServerDomainName"];
smtpClient = new SmtpClient(mailServer, mailPort)
{
DeliveryMethod = SmtpDeliveryMethod.Network
};
if (string.IsNullOrEmpty(mailPassword))
smtpClient.UseDefaultCredentials = false;
else
{
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential(mailUser, mailPassword);
smtpClient.EnableSsl = enableSsl;
}
}
public static bool SendMessage(string recipients, string bcc, string cc, string subject, string body)
{
//get mutex
mailerMutex.WaitOne();
bool isSent = false;
Log.Debug("Mailer.SendMessage: ready to send email");
Log.Debug("Mailer.SendMessage:Send email to " + recipients);
string from = smtpFromEmail;
if (string.IsNullOrEmpty(from))
{
Log.Error("No email from address");
return isSent;
}
MailMessage mailMessage = new();
try
{
mailMessage.From = new MailAddress(from);
if (string.IsNullOrEmpty(recipients))
{
Log.Error("No email recipient address");
return isSent;
}
string emailto = recipients;
if (smtpRedirect)
{
emailto = smtpRedirectEmail;
Log.Debug("Redirected to " + emailto);
bcc = bccEmail;
cc = ccEmail;
}
if (string.IsNullOrEmpty(emailto))
{
Log.Error("No email to address ");
return isSent;
}
string[] redirectEmailList = emailto.Split(';');
foreach (string toAddress in redirectEmailList)
{
mailMessage.To.Add(toAddress);
}
//Check if the bcc value is nothing or an empty string
if (!string.IsNullOrEmpty(bcc))
{
string[] bccList = bcc.Split(';');
foreach (string bccAddress in bccList)
{
if (!string.IsNullOrEmpty(bccAddress))
mailMessage.Bcc.Add(bccAddress);
}
}
//Check if the cc value is nothing or an empty string
if (!string.IsNullOrEmpty(cc))
{
string[] ccList = cc.Split(';');
foreach (string ccAddress in ccList)
{
if (!string.IsNullOrEmpty(ccAddress))
mailMessage.CC.Add(ccAddress);
}
}
string ServerDomain = serveDomainName;
if (!string.IsNullOrEmpty(ServerDomain))
{
mailMessage.Subject = subject + " - " + ServerDomain;
}
else
{
//Set the subject of the mail message
mailMessage.Subject = subject;
}
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
mailMessage.Priority = MailPriority.Normal;
Log.Debug("Mailer.SendMessage: begin...");
smtpClient.Send(mailMessage);
Log.Debug("Mailer.SendMessage-email body is " + body + ", email send successfully");
isSent = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
mailMessage.Dispose();
// Release the Mutex.
mailerMutex.ReleaseMutex();
}
return isSent;
}
}
}
c# smtpclient发送邮件
于 2022-11-23 11:23:45 首次发布