init commit
This commit is contained in:
186
20220330_Vote/Ewide.RoadFlow/Utility/Mail.cs
Normal file
186
20220330_Vote/Ewide.RoadFlow/Utility/Mail.cs
Normal file
@@ -0,0 +1,186 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace RoadFlow.Utility
|
||||
{
|
||||
public class Mail
|
||||
{
|
||||
/// <summary>
|
||||
/// 邮件实体
|
||||
/// </summary>
|
||||
public class MailModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 接收人
|
||||
/// </summary>
|
||||
public string ReceiverName { get; set; }
|
||||
/// <summary>
|
||||
/// 接收邮件地址
|
||||
/// </summary>
|
||||
public string ReceiveMail { get; set; }
|
||||
/// <summary>
|
||||
/// 邮件主题
|
||||
/// </summary>
|
||||
public string Subject { get; set; }
|
||||
/// <summary>
|
||||
/// 邮件内容(可带HTML格式)
|
||||
/// </summary>
|
||||
public string Contents { get; set; }
|
||||
/// <summary>
|
||||
/// 邮件附件绝对地址(多个用逗号隔开)
|
||||
/// </summary>
|
||||
public string Attachments { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 邮件服务器
|
||||
/// </summary>
|
||||
private string _smtpServer;
|
||||
/// <summary>
|
||||
/// 邮件服务器端口
|
||||
/// </summary>
|
||||
private int _port;
|
||||
/// <summary>
|
||||
/// 是否使用SSL
|
||||
/// </summary>
|
||||
private bool _enableSsl;
|
||||
/// <summary>
|
||||
/// 邮件帐号
|
||||
/// </summary>
|
||||
private string _account;
|
||||
/// <summary>
|
||||
/// 邮件密码
|
||||
/// </summary>
|
||||
private string _password;
|
||||
/// <summary>
|
||||
/// 发件人
|
||||
/// </summary>
|
||||
private string _senderName;
|
||||
/// <summary>
|
||||
/// 发件人邮件地址
|
||||
/// </summary>
|
||||
private string _senderMail;
|
||||
public Mail(string smtpServer, int port, bool enableSsl, string account, string password, string senderName, string senderMail)
|
||||
{
|
||||
_smtpServer = smtpServer;
|
||||
_port = port;
|
||||
_enableSsl = enableSsl;
|
||||
_account = account;
|
||||
_password = password;
|
||||
_senderName = senderName;
|
||||
_senderMail = senderMail;
|
||||
}
|
||||
public Mail()
|
||||
{
|
||||
_smtpServer = Config.MailServer;
|
||||
_port = Config.MailPort;
|
||||
_enableSsl = Config.MailEnableSsl;
|
||||
_account = Config.MailAccount;
|
||||
_password = Config.MailPassword;
|
||||
_senderName = Config.MailSenderName;
|
||||
_senderMail = Config.MailSenderMail;
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送邮件
|
||||
/// </summary>
|
||||
/// <param name="receiver">收件人姓名</param>
|
||||
/// <param name="mail">收件人邮箱地址</param>
|
||||
/// <param name="subject">邮件主题</param>
|
||||
/// <param name="content">邮件内容(可带HTML格式)</param>
|
||||
/// <param name="files">邮件附件绝对地址(多个用逗号隔开)</param>
|
||||
public void Send(string receiver, string mail, string subject, string content, string files = "")
|
||||
{
|
||||
MailModel mailModel = new MailModel()
|
||||
{
|
||||
ReceiverName = receiver,
|
||||
ReceiveMail = mail,
|
||||
Subject = subject,
|
||||
Contents = content,
|
||||
Attachments = files
|
||||
};
|
||||
Send(new List<MailModel>() { mailModel });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送邮件
|
||||
/// </summary>
|
||||
/// <param name="mailModel"></param>
|
||||
public void Send(MailModel mailModel)
|
||||
{
|
||||
Send(mailModel);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 一次发多个邮件
|
||||
/// </summary>
|
||||
/// <param name="mails"></param>
|
||||
public void Send(List<MailModel> mails)
|
||||
{
|
||||
if (_smtpServer.IsNullOrWhiteSpace() || _account.IsNullOrWhiteSpace() || _password.IsNullOrWhiteSpace()
|
||||
|| _senderName.IsNullOrWhiteSpace() || _senderMail.IsNullOrWhiteSpace() || mails == null || mails.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 邮件对象
|
||||
MailMessage emailMessage;
|
||||
|
||||
// smtp客户端对象
|
||||
using (SmtpClient client = new SmtpClient())
|
||||
{
|
||||
client.Host = _smtpServer;// 邮件服务器及帐户信息
|
||||
if (_port > 0)
|
||||
{
|
||||
client.Port = _port;
|
||||
}
|
||||
if (_enableSsl)
|
||||
{
|
||||
client.EnableSsl = true;
|
||||
}
|
||||
System.Net.NetworkCredential Credential = new System.Net.NetworkCredential
|
||||
{
|
||||
UserName = _account,
|
||||
Password = _password
|
||||
};
|
||||
client.Credentials = Credential;
|
||||
|
||||
// 发件人
|
||||
MailAddress sendMail = new MailAddress(_senderMail, _senderName);
|
||||
foreach (var mail in mails)
|
||||
{
|
||||
MailAddress receiveMail = new MailAddress(mail.ReceiveMail);
|
||||
emailMessage = new MailMessage(sendMail, receiveMail)
|
||||
{
|
||||
Subject = mail.Subject,
|
||||
Body = mail.Contents,
|
||||
IsBodyHtml = true,
|
||||
SubjectEncoding = System.Text.Encoding.Default,
|
||||
BodyEncoding = System.Text.Encoding.Default
|
||||
};
|
||||
if (!mail.Attachments.IsNullOrWhiteSpace())
|
||||
{
|
||||
foreach (string file in mail.Attachments.Split(','))
|
||||
{
|
||||
if (file.IsNullOrWhiteSpace() || !System.IO.File.Exists(file))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Attachment attachment = new Attachment(File.OpenRead(file), Path.GetFileName(file));
|
||||
emailMessage.Attachments.Add(attachment);
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
client.Send(emailMessage);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user