C#簡略郵件群發通用類。本站提示廣大學習愛好者:(C#簡略郵件群發通用類)文章只能為提供參考,不一定能成為您想要的結果。以下是C#簡略郵件群發通用類正文
本文實例為年夜家引見了C#郵件群發通用類,供年夜家參考,詳細內容以下
public static class Email
{
/// <summary>
/// 發件人
/// </summary>
public static string mailFrom { get; set; }
/// <summary>
/// 收件人
/// </summary>
public static string[] mailToArray { get; set; }
/// <summary>
/// 抄送
/// </summary>
public static string[] mailCcArray { get; set; }
/// <summary>
/// 題目
/// </summary>
public static string mailSubject { get; set; }
/// <summary>
/// 注釋
/// </summary>
public static string mailBody { get; set; }
/// <summary>
/// 發件人暗碼
/// </summary>
public static string mailPwd { get; set; }
/// <summary>
/// SMTP郵件辦事器
/// </summary>
public static string host { get; set; }
/// <summary>
/// 郵件辦事器端口
/// </summary>
public static int port { get; set; }
/// <summary>
/// 注釋能否是html格局
/// </summary>
public static bool isbodyHtml { get; set; }
/// <summary>
/// 附件
/// </summary>
public static string[] attachmentsPath { get; set; }
public static bool Send()
{
//應用指定的郵件地址初始化MailAddress實例
MailAddress maddr = new MailAddress(mailFrom);
//初始化MailMessage實例
MailMessage myMail = new MailMessage();
//向收件人地址聚集添加郵件地址
if (mailToArray != null)
{
for (int i = 0; i < mailToArray.Length; i++)
{
myMail.To.Add(mailToArray[i].ToString());
}
}
//向抄送收件人地址聚集添加郵件地址
if (mailCcArray != null)
{
for (int i = 0; i < mailCcArray.Length; i++)
{
myMail.CC.Add(mailCcArray[i].ToString());
}
}
//發件人地址
myMail.From = maddr;
//電子郵件的題目
myMail.Subject = mailSubject;
//電子郵件的主題內容應用的編碼
myMail.SubjectEncoding = Encoding.UTF8;
//電子郵件注釋
myMail.Body = mailBody;
//電子郵件注釋的編碼
myMail.BodyEncoding = Encoding.Default;
//電子郵件優先級
myMail.Priority = MailPriority.High;
//電子郵件不是html格局
myMail.IsBodyHtml = isbodyHtml;
//在有附件的情形下添加附件
try
{
if (attachmentsPath != null && attachmentsPath.Length > 0)
{
Attachment attachFile = null;
foreach (string path in attachmentsPath)
{
attachFile = new Attachment(path);
myMail.Attachments.Add(attachFile);
}
}
}
catch (Exception err)
{
throw new Exception("在添加附件時有毛病:" + err.Message);
}
SmtpClient client = new SmtpClient();
//指定發件人的郵件地址和暗碼以驗證發件人身份
client.Credentials = new NetworkCredential(mailFrom, mailPwd);
//設置SMTP郵件辦事器
//client.Host = "smtp." + myMail.From.Host;
client.Host = host;
//SMTP郵件辦事器端口
client.Port = port;
//能否應用平安銜接
//client.EnableSsl = true;
try
{
//將郵件發送到SMTP郵件辦事器
client.Send(myMail);
return true;
}
catch (SmtpException ex)
{
string msg = ex.Message;
return false;
}
}
願望本文所述對年夜家進修C#法式設計有所贊助。