- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration.Install;
- using System.ServiceProcess;
- using System.Collections;
- using System.Threading;
- using System.Xml;
- using System.IO;
- using System.Net.Mail;
- using System.Runtime.Remoting.Channels.Tcp;
- using System.Runtime.Remoting.Channels;
- using System.Runtime.Remoting;
- using Pub.Class;
- using System.Diagnostics;
-
- namespace MailService {
-
- [RunInstaller(true)]
- public partial class MService : ServiceBase {
- public static bool isRun = false;
- public Queue emailQueue = new Queue();
- private Thread readEmailThread;
- private Thread[] sendEmailThread;
- private string[] strList = new string[] { "MailService 启动成功!", "MailService 停止!", "{2} {1} - [{0}] - 发送失败!", "{2} {1} - [{0}] - 发送成功!", "LiveRemotingService 已启动,服务端口6669。", "LiveRemotingService 停止!" };
-
- private struct Config {
- public string Conn;
- public string LogFile;
- public string SmtpServer;
- public string UserName;
- public string Password;
- public string FromAddress;
- public int AmountThread;
- public int RecordCount;
- public int TimeInterval;
- }
- private Config config = new Config();
-
- public MService() {
- System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
- InitializeComponent();
-
- if (!System.Diagnostics.EventLog.SourceExists("MailSource")) System.Diagnostics.EventLog.CreateEventSource("MailSource", "MailServiceLog");
- this.eventLog1.Source = "MailSource";
- this.eventLog1.Log = "MailServiceLog";
- this.eventLog2.Source = "LiveRemotingSource";
- this.eventLog2.Log = "MailServiceLog";
- }
-
- protected override void OnStart(string[] args) {
- try {
- InitConfig();
- this.eventLog1.WriteEntry(strList[0], System.Diagnostics.EventLogEntryType.SuccessAudit);
-
- this.timer1.Interval = config.TimeInterval * 1000;
- this.timer1.Enabled = true;
- sendEmailThread = new Thread[config.AmountThread];
- } catch (Exception e) {
- this.eventLog1.WriteEntry(e.ToString(), System.Diagnostics.EventLogEntryType.Error);
- }
- }
-
- protected override void OnStop() {
- this.eventLog1.WriteEntry(strList[1], System.Diagnostics.EventLogEntryType.SuccessAudit);
- GC.Collect();
- this.timer1.Enabled = false;
- }
-
- private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
- if (isRun) return;
-
- timer1.Enabled = false;
- readEmailThread = new Thread(new ThreadStart(ReadEmailQuque));
- readEmailThread.IsBackground = true;
- readEmailThread.Start();
- }
-
- private void InitConfig(){
- config.Conn = Pub.Class.WebConfig.GetApp("ConnString");
- config.LogFile = Pub.Class.WebConfig.GetApp("logFile");
- config.SmtpServer = Pub.Class.WebConfig.GetApp("SmtpServer");
- config.UserName = Pub.Class.WebConfig.GetApp("UserName");
- config.Password = Pub.Class.WebConfig.GetApp("Password");
- config.FromAddress = Pub.Class.WebConfig.GetApp("FromAddress");
- string amountThread = Pub.Class.WebConfig.GetApp("AmountThread");
- config.AmountThread = amountThread.Equals("") ? 1 : Convert.ToInt32(amountThread);
- config.AmountThread = config.AmountThread < 1 ? 1 : config.AmountThread;
- string recordCount = Pub.Class.WebConfig.GetApp("RecordCount");
- config.RecordCount = recordCount.Equals("") ? 1000 : Convert.ToInt32(recordCount);
- config.RecordCount = config.RecordCount < 1000 ? 1000 : config.RecordCount;
- string timeInterval = Pub.Class.WebConfig.GetApp("TimeInterval");
- config.TimeInterval = timeInterval.Equals("") ? 1000 : Convert.ToInt32(timeInterval);
- config.TimeInterval = config.TimeInterval < 2 ? 2 : config.TimeInterval;
- }
-
- private void ReadEmailQuque(){
- timer1.Enabled = true;
- IList<EC_EmailList> list = EC_EmailListFactory.Instance().SelectListByTop(config.RecordCount);
- if (list.Count == 0) return;
-
- isRun = true;
- for (int i = 0; i < list.Count; i++) {
- emailQueue.Enqueue(list[i]);
- }
-
- for (int i = 0; i < config.AmountThread; i++) {
- sendEmailThread[i] = new Thread(new ThreadStart(DoSendEmail));
- sendEmailThread[i].Name = "Thread" + (i+1).ToString();
- sendEmailThread[i].Start();
- }
- list = null;
- }
-
- private void DoSendEmail(){
- while (true) {
- EC_EmailList objMail;
- lock(this){
- if (emailQueue.Count>0) {
- objMail = (EC_EmailList)emailQueue.Dequeue();
- } else {
- isRun = false;
- return;
- }
- }
-
- int mailID = (int)objMail.EmailID;
- string strTo = objMail.To;
- string strSubject = objMail.Subject;
- string strBody = objMail.Body;
- string strFrom = objMail.From;
- string smtpServer = objMail.SmtpServer;
- string userName = objMail.UserName;
- string password = objMail.Password;
-
- bool isTrue = SendMail(strTo, strSubject, strBody, strFrom, smtpServer, userName, password, "");
-
- EC_EmailListFactory.Instance().DeleteByID(mailID);
-
- }
- }
-
- public bool SendMail(string strTo, string strSubject, string strBody,
- string strFrom, string smtpServer, string userName,
- string password, string attachments) {
- Email email = new Email();
- string strSmtpServer = smtpServer.Length > 0 ? smtpServer : config.SmtpServer.Trim();
- email.SmtpServer = strSmtpServer;
- email.SmtpUserName = userName.Length > 0 ? userName : config.UserName.Trim();
- email.SmtpPassword = password.Length > 0 ? password : config.Password.Trim();
- email.SmtpPort = strSmtpServer.ToLower().Contains("gmail") ? "587" : "25";
- email.EnableSsl = strSmtpServer.ToLower().Contains("gmail") ? true : false;
- email.FromEmail = strFrom.Length > 0 ? strFrom : config.FromAddress.Trim();
- email.Subject = strSubject;
- email.Body = strBody;
- email.Encoding = System.Text.Encoding.UTF8;
- bool isSuccess = email.SmtpClientSend(strTo);
- return isSuccess;
- }
- public void ErrorLog(string strMessage) {
- lock(this){
- StreamWriter sw = new StreamWriter(config.LogFile + "MailLog.txt", true);
- sw.WriteLine(strMessage);
- sw.Flush();
- sw.Close();
- }
- }
- }
- }
posted @
2012-02-23 18:52
therockthe
阅读(
305)
评论()
收藏
举报