标签:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Mail; using System.Text.RegularExpressions; public class EmailUtils { #region 验证输入为Email /// <summary> /// 验证输入为Email /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool validateEmail(string str) { return Regex.IsMatch(str, @"\w+([-+.‘]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); } #endregion public static void SendEmail(string txtSend,string txtTo) { try { if (validateEmail(txtSend)) { //设置邮件发送人和接收人 MailMessage message = null; if (txtTo.IndexOf(",") != -1) { string[] strEmail = txtTo.Split(‘,‘); string sumEmail = ""; for (int i = 0; i < strEmail.Length; i++) { sumEmail = strEmail[i]; message = new MailMessage(new MailAddress(txtSend), new MailAddress(sumEmail)); SendEmail(message); } } else { message = new MailMessage(new MailAddress(txtSend), new MailAddress(txtTo)); SendEmail(message); } Log("发送成功"); } } catch { Log("发送失败!"); } } private void SendEmail(MailMessage message) { message.Subject = Base64Encode("testSubject"); //设置发送邮件的主题 message.Body = Base64Encode("testContent"); //设置发送邮件的内容 if (txtAttachment!= "") { if (txtAttachment.IndexOf(",") != 0) { string[] strAttachment = txtAttachment.Split(‘,‘); for (int i = 0; i < strAttachment.Length; i++) { AddFile(strAttachment[i], message); } } else { AddFile(txtAttachment, message); } } //实例化SmtpClient邮件发送类对象 SmtpClient client = new SmtpClient(Dns.GetHostName(), Convert.ToInt32("25")); //设置用于验证发件人身份的凭据 client.Credentials = new System.Net.NetworkCredential("MyTest", "*****"); //发送邮件 client.Send(message); } //对邮件内容进行编码 private static string Base64Encode(string str) { return Convert.ToBase64String(Encoding.UTF8.GetBytes(str)); } private void AddFile(string strFile,MailMessage message) { //为要发送的邮件创建附件信息 Attachment myAttachment = new Attachment(strFile, System.Net.Mime.MediaTypeNames.Application.Octet); //为附件添加时间信息 System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(strFile); disposition.ModificationDate = System.IO.File.GetLastWriteTime(strFile); disposition.ReadDate = System.IO.File.GetLastAccessTime(strFile); message.Attachments.Add(myAttachment); //将创建的附件添加到邮件中 } }
标签:
原文地址:http://www.cnblogs.com/ziranquliu/p/4690756.html