C# 简单邮件群发通用类
2016-03-16 17:17
579 查看
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#之DataSet和DataTable
- C#解leetcode 18. 4Sum
- C#使用DevExpress与panel的加载
- C# 获取某月的第一天和最后一天
- C#成魔之路<15> 注册表技术
- C# 性能优化
- C#循环使用File.Copy造成电脑死机
- C# DataTable转实体 通用方法
- c# 操作xml之xmlReader
- C#中MessageBox用法大全
- 【笔记】《C#大学教程》- 第11章 异常处理
- C#中枚举
- C# 截取字符串某个字符分割的最后一部分
- C#:引用类型和值类型的相等比较
- c# 中的只读属性
- c#鼠标点击TextBox控件后清空默认字体-完美解决方案
- Visual C# 2008 调试技巧
- C#基础数据类型与字节数组(内存中的数据格式)相互转换(BitConverter 类)
- C# 函数
- C# 数组