首页 > 代码库 > Log日志 发送邮件功能

Log日志 发送邮件功能

技术分享
  1 public class MySerialObject  2 {  3 //发送者邮件地址  4 private string _form;  5 //发送者邮件密码  6 private string _fromPwd;  7 //接受者邮件  8 private string _to;  9 //抄送者 10 private string _cc; 11 //附件路径 12 private string _attachment; 13 //发送者邮件的smtp服务器主机名 14 private string _smtpHost; 15 //邮件主题 16 private string _subject; 17 //邮件正文 18 private string _body; 19 //是否识别为html格式 20 private bool _isHtmlStyle; 21  22 public bool IsHtmlStyle 23 { 24 get { return _isHtmlStyle; } 25 set { _isHtmlStyle = value; } 26 } 27 public string Body 28 { 29 get { return _body; } 30 set { _body = value; } 31 }  32 public string Subject 33 { 34 get { return _subject; } 35 set { _subject = value; } 36 } 37 public string SmtpHost 38 { 39 get { return _smtpHost; } 40 set { _smtpHost = value; } 41 } 42 public string Attachment 43 { 44 get { return _attachment; } 45 set { _attachment = value; } 46 } 47 public string Cc 48 { 49 get { return _cc; } 50 set { _cc = value; } 51 } 52 public string To 53 { 54 get { return _to; } 55 set { _to = value; } 56 }  57 public string FormPwd 58 { 59 get { return _fromPwd; } 60 set { _fromPwd = value; } 61 }  62 public string Form 63 { 64 get { return _form; } 65 set { _form = value; } 66 } 67  68 public MySerialObject(string form, string fromPwd, string to, string cc, string attachments, string smtpHost, string subject, string body) 69 { 70 _form = form; 71 _fromPwd = fromPwd; 72 _to = to; 73 _cc = cc; 74 _attachment = attachments; 75 _smtpHost = smtpHost; 76 _subject = subject; 77 _body = body; 78 } 79 public MySerialObject() 80 { 81  82 } 83 public bool SendMail() 84 { 85 List<MailAddress> mailaddrs = new List<MailAddress>(); 86 if (_cc != null && _cc != "") 87 { 88 foreach (var item in _cc.Split(,)) 89 { 90 MailAddress mailaddr = new MailAddress(item); 91 mailaddrs.Add(mailaddr); 92 } 93 } 94 List<Attachment> attachments = new List<Attachment>(); 95 if (_attachment != null & _attachment != "") 96 { 97 foreach (var item in _attachment.Split(,)) 98 { 99 Attachment att = new Attachment(item);100 //MIME协议下的一个对象,用以设置附件的创建时间,修改时间以及读取时间 101 //System.Net.Mime.ContentDisposition disposition = att.ContentDisposition;102 //disposition.CreationDate = System.IO.File.GetCreationTime(file);103 //disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);104 //disposition.ReadDate = System.IO.File.GetLastAccessTime(file);105 attachments.Add(att);106 }107 }108 return SendMail(_form, _fromPwd, _to, _smtpHost, _subject, _body);109 }110 /// <summary>111 /// 发送邮件,返回true表示发送成功112 /// </summary>113 /// <param name="form">发件人邮箱地址;发件人用户名</param>114 /// <param name="fromPwd">密码</param>115 /// <param name="to">接受者邮箱地址</param>116 /// <param name="smtpHost">服务器的主机名</param>117 /// <param name="subject">邮件主题行</param>118 /// <param name="body">邮件主体正文</param>119 /// <returns></returns>120 private bool SendMail(string form, string fromPwd, string to, string smtpHost, string subject, string body)121 {122 System.Net.Mail.SmtpClient client = new SmtpClient();123 client.Host = smtpHost;124 client.UseDefaultCredentials = true;125 client.Credentials = new System.Net.NetworkCredential(form, fromPwd);126 client.DeliveryMethod = SmtpDeliveryMethod.Network;127 128 try129 {130 System.Net.Mail.MailMessage message = new MailMessage(form, to);131 message.Subject = subject;132 message.Body = body;133 message.BodyEncoding = System.Text.Encoding.UTF8;134 message.IsBodyHtml = true;135 client.Send(message);136 return true;137 }138 catch (Exception)139 {140 //wirte log141 return false;142 }143 }144 /// <summary>145 /// 发送邮件,带抄送者,返回true表示发送成功146 /// </summary>147 /// <param name="form">发件人邮箱地址;发件人用户名</param>148 /// <param name="fromPwd">密码</param>149 /// <param name="to">接受者邮箱地址</param>150 /// <param name="cc">抄送者邮箱地址</param>151 /// <param name="smtpHost">服务器的主机名</param>152 /// <param name="subject">邮件主题行</param>153 /// <param name="body">邮件主体正文</param>154 /// <returns></returns>155 private bool SendMail(string form, string fromPwd, string to, string cc, string smtpHost, string subject, string body)156 {157 System.Net.Mail.SmtpClient client = new SmtpClient();158 client.Host = smtpHost;159 client.UseDefaultCredentials = true;160 161 client.Credentials = new System.Net.NetworkCredential(form, fromPwd);162 client.DeliveryMethod = SmtpDeliveryMethod.Network;163 164 try165 {166 System.Net.Mail.MailMessage message = new MailMessage(form, to);167 message.Subject = subject;168 message.CC.Add(cc);169 message.Body = body;170 message.BodyEncoding = System.Text.Encoding.UTF8;171 message.IsBodyHtml = true;172 client.Send(message);173 return true;174 }175 catch (Exception)176 {177 //write log178 return false;179 }180 }181 182 /// <summary>183 /// 发送邮件,带抄送者,带附件,返回true表示发送成功184 /// </summary>185 /// <param name="form">发件人邮箱地址;发件人用户名</param>186 /// <param name="fromPwd">密码</param>187 /// <param name="to">接受者邮箱地址</param>188 /// <param name="cc">抄送者邮箱地址</param>189 /// <param name="smtpHost">服务器的主机名</param>190 /// <param name="subject">邮件主题行</param>191 /// <param name="body">邮件主体正文</param>192 /// <param name="attachments">附件</param>193 /// <returns></returns>194 private bool SendMail(string form, string fromPwd, string to, List<MailAddress> cc, string smtpHost, string subject, string body, List<Attachment> attachments)195 {196 System.Net.Mail.SmtpClient client = new SmtpClient();197 client.Host = smtpHost;198 client.UseDefaultCredentials = true;199 200 client.Credentials = new System.Net.NetworkCredential(form, fromPwd);201 client.DeliveryMethod = SmtpDeliveryMethod.Network;202 203 try204 {205 System.Net.Mail.MailMessage message = new MailMessage(form, to);206 message.Subject = subject;207 foreach (var ccItem in cc)208 {209 message.CC.Add(ccItem);210 }211 foreach (var attItem in attachments)212 {213 message.Attachments.Add(attItem);214 }215 message.Body = body;216 message.BodyEncoding = System.Text.Encoding.UTF8;217 message.IsBodyHtml = _isHtmlStyle;218 client.Send(message);219 return true;220 }221 catch (Exception)222 {223 //write log224 return false;225 }226 }227 }
View Code
技术分享
 1 class Program 2     { 3         static void Main(string[] args) 4         {            5             /*注意: 6             *发送邮箱,必须开通smtp服务,具体开发 7                 多个邮件/多个路径,可用逗号[,]隔开 8             * QQsmtp服务器名:smtp.qq.com 9             * 163:smtp.163.com10             *///引用实例11             MySerialObject m = new MySerialObject12             {13                 Form = "835745731@qq.com",14                 FormPwd = "123456a",15                 SmtpHost = "smtp.qq.com",16                 Body = "邮件内容",17                 To = "1839467491@qq.com,835745731@qq.com,329955168@qq.com",      18                 IsHtmlStyle = true,19                 Subject = "邮件标题"20             };21             m.SendMail();22         } 23     } 
View Code

 

Log日志 发送邮件功能