1、邮件发送代码
1 using System.Text; 2 using System.Net; 3 using System.Net.Mail; 4 using System.Reflection; 5 using System.IO; 6 7 namespace ConsoleApplication1 8 { 9 class Class1 10 { 11 public void set() 12 { 13 SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient 14 smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network 15 smtp.EnableSsl = false;//smtp服务器是否启用SSL加密 16 smtp.Host = "smtp.163.com"; //指定 smtp 服务器地址 17 smtp.Port = 25; //指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去 18 //如果你的SMTP服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了 19 smtp.UseDefaultCredentials = true; 20 //如果需要认证,则用下面的方式 21 smtp.Credentials = new NetworkCredential("邮箱帐号@163.com", "邮箱密码"); 22 MailMessage mm = new MailMessage(); //实例化一个邮件类 23 mm.Priority = MailPriority.High; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可 24 mm.From = new MailAddress("邮箱帐号@163.com", "真有意思", Encoding.GetEncoding(936)); 25 //收件方看到的邮件来源; 26 //第一个参数是发信人邮件地址 27 //第二参数是发信人显示的名称 28 //第三个参数是 第二个参数所使用的编码,如果指定不正确,则对方收到后显示乱码 29 //936是简体中文的codepage值 30 //注:上面的邮件来源,一定要和你登录邮箱的帐号一致,否则会认证失败 31 mm.ReplyTo = new MailAddress("[email protected]", "我的接收邮箱", Encoding.GetEncoding(936)); 32 //ReplyTo 表示对方回复邮件时默认的接收地址,即:你用一个邮箱发信,但却用另一个来收信 33 //上面后两个参数的意义, 同 From 的意义 34 mm.CC.Add("[email protected],[email protected],[email protected]"); 35 //邮件的抄送者,支持群发,多个邮件地址之间用 半角逗号 分开 36 //当然也可以用全地址,如下: 37 mm.CC.Add(new MailAddress("[email protected]", "抄送者A", Encoding.GetEncoding(936))); 38 mm.CC.Add(new MailAddress("[email protected]", "抄送者B", Encoding.GetEncoding(936))); 39 mm.CC.Add(new MailAddress("[email protected]", "抄送者C", Encoding.GetEncoding(936))); 40 mm.Bcc.Add("[email protected],[email protected]"); 41 //邮件的密送者,支持群发,多个邮件地址之间用 半角逗号 分开 42 //当然也可以用全地址,如下: 43 mm.CC.Add(new MailAddress("[email protected]", "密送者D", Encoding.GetEncoding(936))); 44 mm.CC.Add(new MailAddress("[email protected]", "密送者E", Encoding.GetEncoding(936))); 45 mm.Sender = new MailAddress("[email protected]", "邮件发送者", Encoding.GetEncoding(936)); 46 //可以任意设置,此信息包含在邮件头中,但并不会验证有效性,也不会显示给收件人 47 //说实话,我不知道有啥实际作用,大家可不理会,也可不写此项 48 mm.To.Add("[email protected],[email protected]"); 49 //邮件的接收者,支持群发,多个地址之间用 半角逗号 分开 50 //当然也可以用全地址添加 51 mm.To.Add(new MailAddress("[email protected]", "接收者g", Encoding.GetEncoding(936))); 52 mm.To.Add(new MailAddress("[email protected]", "接收者h", Encoding.GetEncoding(936))); 53 mm.Subject = "这是邮件标题"; //邮件标题 54 mm.SubjectEncoding = Encoding.GetEncoding(936); 55 // 这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。 56 // 936是简体中文的pagecode,如果是英文标题,这句可以忽略不用 57 mm.IsBodyHtml = true; //邮件正文是否是HTML格式 58 mm.BodyEncoding = Encoding.GetEncoding(936); 59 //邮件正文的编码, 设置不正确, 接收者会收到乱码 60 61 mm.Body = "邮件测试,呵呵"; 62 #region 可以使用预定义好的html模板进行邮件发送 63 //string assemblyFilePath = Assembly.GetExecutingAssembly().Location; 64 //string assemblyDirPath = Path.GetDirectoryName(assemblyFilePath); 65 //string bodyPath = assemblyDirPath + "\\email.htm"; 66 //StreamReader read = new StreamReader(bodyPath, Encoding.GetEncoding("GB2312")); 67 //string mailBody = read.ReadToEnd(); 68 //read.Close(); 69 //string dateStr = DateTime.Now.ToString("yyyy年M月d号"); 70 //mailBody = mailBody.Replace("当前时间", dateStr); 71 //mailBody = mailBody.Replace("文件路径", ""); 72 //mm.Body = mailBody; 73 #endregion 74 //邮件正文 75 mm.Attachments.Add(new Attachment(@"d:a.doc", System.Net.Mime.MediaTypeNames.Application.Rtf)); 76 //添加附件,第二个参数,表示附件的文件类型,可以不用指定 77 //可以添加多个附件 78 mm.Attachments.Add(new Attachment(@"d:b.doc")); 79 smtp.Send(mm); //发送邮件,如果不返回异常, 则大功告成了。 80 } 81 } 82 }
2、windows服务的编写,是参考别人的代码实现的,自己做了部分的修改,设计到路径的读取问题,具体操作不厚可以见原帖,地址:http://www.cnblogs.com/tuyile006/archive/2006/11/27/573654.html,下面是源代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Diagnostics; 6 using System.Linq; 7 using System.ServiceProcess; 8 using System.Text; 9 using System.IO; 10 using System.Timers; 11 using Microsoft.Win32; 12 13 namespace MailTest 14 { 15 public partial class Service1 : ServiceBase 16 { 17 public Service1() 18 { 19 InitializeComponent(); 20 } 21 private Timer time; 22 private static readonly string CurrentPath = GetWindowsServiceInstallPath("每日统计台账推送服务") + "//"; 23 string today = ""; 24 protected override void OnStart(string[] args) 25 { 26 // TODO: 在此处添加代码以启动服务。 27 Log("start-stop", "推送服务启"); 28 time = new Timer(5000); 29 time.Enabled = true; 30 time.Elapsed += this.time_Elapsed; 31 time.Start(); 32 } 33 34 void time_Elapsed(object sender, ElapsedEventArgs e) 35 { 36 int intHour = e.SignalTime.Hour; 37 if (intHour == 17 && today != DateTime.Now.ToString("yyyyMMdd")) 38 { 39 try 40 { 41 string workPath = CurrentPath + "Log//work.log"; 42 MailSend send = new MailSend(); 43 send.send("测试路径"); 44 today = DateTime.Now.ToString("yyyyMMdd"); 45 Log("work", "执行完成,时间"); 46 } 47 catch (Exception ex) 48 { 49 Log("error", "运行出现异常:" + ex.ToString()); 50 } 51 } 52 } 53 54 protected override void OnStop() 55 { 56 // TODO: 在此处添加代码以执行停止服务所需的关闭操作。 57 time.Enabled = false; 58 Log("start-stop", "推送服务停止"); 59 60 } 61 public void Log(string type, string message) 62 { 63 //string path = CurrentPath + "Log//" + type + ".log"; 64 string path = @"C:\Users\sd\Documents\visual studio 2010\Projects\MailTest\MailTest\bin\Debug\Log\log.log"; 65 if (!File.Exists(path)) 66 { 67 FileStream create = File.Create(path); 68 create.Close(); 69 } 70 FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write); 71 StreamWriter sw = new StreamWriter(fs); 72 sw.WriteLine(message + "---" + DateTime.Now.ToString()); 73 sw.Flush(); 74 sw.Close(); 75 fs.Close(); 76 } 77 /// <summary> 78 /// 获取服务所在安装路径 4 /// </summary> 79 /// <param name="ServiceName">服务名</param> 80 /// <returns>服务安装路径</returns> 81 public static string GetWindowsServiceInstallPath(string ServiceName) 82 { 83 string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName; 84 string path = Registry.LocalMachine.OpenSubKey(key).GetValue("ImagePath").ToString(); 85 path = path.Replace("\"", string.Empty);//替换掉双引号 86 FileInfo fi = new FileInfo(path); 87 return fi.Directory.ToString(); 88 } 89 } 90 }