📄 email.cs
字号:
using System;
using System.Net.Mail;
//该源码下载自www.51aspx.com(51aspx.com)
namespace SkyiSite.DBUtility
{
public class Email
{
private Email() { }
public static readonly Email Instance = new Email();
public string SendMail( string to, string subject, string body,string filePath)
{
EmailInfo emailInfo = ReadEmail(filePath);
System.Data.DataTable table = new System.Data.DataTable();
SmtpClient mailClient = new SmtpClient();
mailClient.Host = emailInfo.Home;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = new System.Net.NetworkCredential(emailInfo.UserName, emailInfo.PassWord);
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailMessage mailMessage = new MailMessage();
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mailMessage.IsBodyHtml = true;
mailMessage.From = new MailAddress(emailInfo.From);
mailMessage.To.Add(new MailAddress(to));
mailMessage.Subject = subject;
mailMessage.Body = body;
try
{
mailClient.Send(mailMessage);
return emailInfo.SendSuccessMessage;
}
catch (Exception ex)
{
return emailInfo.SendFailMessage + ex.Message;
}
}
public EmailInfo ReadEmail(string filePath)
{
EmailInfo emailInfo = new EmailInfo();
System.Data.DataTable table = SkyiSite.Tooltip.XmlTooltip.Instance.ReadXmlToDataTable(filePath);
emailInfo.UserName = SkyiSite.Tooltip.PassWordHelper.Instance.Decrypt_DES_String(table.Rows[0]["UserName"].ToString());
emailInfo .PassWord= SkyiSite.Tooltip.PassWordHelper.Instance.Decrypt_DES_String(table.Rows[0]["PassWord"].ToString());
emailInfo .From= SkyiSite.Tooltip.PassWordHelper.Instance.Decrypt_DES_String(table.Rows[0]["From"].ToString());
emailInfo.Home = SkyiSite.Tooltip.PassWordHelper.Instance.Decrypt_DES_String(table.Rows[0]["Home"].ToString());
emailInfo.SendSuccessMessage = SkyiSite.Tooltip.PassWordHelper.Instance.Decrypt_DES_String(table.Rows[0]["SendSuccessMessage"].ToString());
emailInfo.SendFailMessage = SkyiSite.Tooltip.PassWordHelper.Instance.Decrypt_DES_String(table.Rows[0]["SendFailMessage"].ToString());
if (table != null) table.Dispose();
return emailInfo;
}
public void WriteEmail(EmailInfo emailInfo, string filePath)
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add(new System.Data.DataColumn("UserName", typeof(string)));
table.Columns.Add(new System.Data.DataColumn("PassWord", typeof(string)));
table.Columns.Add(new System.Data.DataColumn("From", typeof(string)));
table.Columns.Add(new System.Data.DataColumn("Home", typeof(string)));
table.Columns.Add(new System.Data.DataColumn("SendSuccessMessage", typeof(string)));
table.Columns.Add(new System.Data.DataColumn("SendFailMessage", typeof(string)));
System.Data.DataRow row = table.NewRow();
row[0] = SkyiSite.Tooltip.PassWordHelper.Instance.Encrypt_DES_String(emailInfo.UserName);
row[1] = SkyiSite.Tooltip.PassWordHelper.Instance.Encrypt_DES_String(emailInfo.PassWord);
row[2] = SkyiSite.Tooltip.PassWordHelper.Instance.Encrypt_DES_String(emailInfo.From);
row[3] = SkyiSite.Tooltip.PassWordHelper.Instance.Encrypt_DES_String(emailInfo.Home);
row[4] = SkyiSite.Tooltip.PassWordHelper.Instance.Encrypt_DES_String(emailInfo.SendSuccessMessage);
row[5] = SkyiSite.Tooltip.PassWordHelper.Instance.Encrypt_DES_String(emailInfo.SendFailMessage);
table.Rows.Add(row);
SkyiSite.Tooltip.XmlTooltip.Instance.WriteDataTableToXml(filePath, table);
table.Dispose();
}
public EmailInfo InitializationEmail()
{
EmailInfo emailInfo = new EmailInfo();
emailInfo.Home = "smtp.163.com";
emailInfo.From = "gxsme@163.com";
emailInfo.UserName = "gxsme@163.com";
emailInfo.PassWord = "sme.gxsti";
emailInfo.SendSuccessMessage = "邮件发送成功,请注意查收...";
emailInfo.SendFailMessage = "邮件发送失败,请检查...";
return emailInfo;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -