Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- partial class EmailService : ServiceBase
- {
- private static MessageQueue msgQ = null;
- private static object lockObject = new object();
- private SmtpClient objSmtpClient;
- private MailMessage objMailMsg;
- private string mailFrom="";
- EmailMessage msg;
- Log Log;
- public EmailService()
- {
- InitializeComponent();
- Log l= new Log();
- }
- public void OnStart()
- // protected override void OnStart(string[] args)
- {
- string queuePath = ConfigurationManager.AppSettings["mqPath"];
- mailFrom = ConfigurationManager.AppSettings["notificationemail"];
- string SMTPSSLConfig = ConfigurationManager.AppSettings["smtpclientssl"];
- if (string.IsNullOrEmpty(queuePath))
- {
- throw new Exception("Message queue path not defined in app.config.");
- }
- if (string.IsNullOrEmpty(mailFrom))
- {
- throw new Exception("Sender email used to send notifications is not defined in app.config.");
- }
- if (string.IsNullOrEmpty(SMTPSSLConfig))
- {
- throw new Exception("SMTP SSL config not defined in app.config.");
- }
- objSmtpClient = new SmtpClient();
- objSmtpClient.EnableSsl = Convert.ToBoolean(SMTPSSLConfig);
- //QueueService.InsureQueueExists(queuePath);
- msgQ = new MessageQueue(queuePath);
- msgQ.Formatter = new BinaryMessageFormatter();
- msgQ.MessageReadPropertyFilter.SetAll();
- msgQ.ReceiveCompleted += new ReceiveCompletedEventHandler(msgQ_ReceiveCompleted);
- msgQ.BeginReceive();
- try
- {
- if (!MessageQueue.Exists(queuePath))
- {
- MessageQueue.Create(queuePath);
- Log.WriteMessageQueueInitialParamsLog(System.DateTime.Now.ToString()+" :Message queue successfully created at following location:"+queuePath);
- }
- else
- {
- Log.WriteMessageQueueInitialParamsLog(System.DateTime.Now.ToString() + ": Message Queue Path: " + msgQ.Path);
- }
- }
- catch (Exception ex)
- {
- Log.WriteCommonLog(System.DateTime.Now.ToString() + " :Error checking message queue existence:\n"+GetExceptionMessageString(ex));
- }
- //eventLog1.WriteEntry(System.DateTime.Now.ToString()+" :EmailService started successfully." );
- Log.WriteMessageQueueInitialParamsLog(System.DateTime.Now.ToString()+" :Message queue started successfully.");
- }
- private void msgQ_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
- {
- try
- {
- lock (lockObject)
- {
- msg = (EmailMessage)e.Message.Body;
- Log.WriteMessageQueueLog(System.DateTime.Now.ToString() + " :Message retrieved from message Queue: From:" + msg.From + " To:" + msg.To + " Subject:" + msg.Subject);
- SendEmail(msg);
- }
- // Listen for the next message.
- msgQ.BeginReceive();
- }
- catch(Exception ex)
- {
- Log.WriteCommonLog(GetExceptionMessageString(ex));
- }
- }
- protected override void OnStop()
- {
- msgQ.Close();
- //eventLog1.WriteEntry(System.DateTime.Now.ToString()+" :EmailService stopped.");
- Log.WriteMessageQueueInitialParamsLog(System.DateTime.Now.ToString()+" :Message queue stopped successfully.");
- }
- private void SendEmail(EmailMessage objMail)
- {
- try
- {
- if(string.IsNullOrEmpty(mailFrom) || string.IsNullOrEmpty(objMail.To))
- throw new Exception("Either sender email address is null or receiver email address is null.");
- objMailMsg = new MailMessage(new MailAddress(mailFrom), new MailAddress(objMail.To));
- objMailMsg.ReplyTo = new MailAddress(mailFrom);
- objMailMsg.IsBodyHtml = true;
- objMailMsg.Subject = objMail.Subject;
- objMailMsg.Body = objMail.Message;
- objSmtpClient.Send(objMailMsg);
- Log.WriteEmailLog("Email Sent: DateTime:" + System.DateTime.Now.ToString() + " To:" + objMail.To + " From:" + mailFrom + " Subject:" + objMail.Subject);
- System.Threading.Thread.Sleep(1000);
- }
- catch (System.Net.Mail.SmtpException se)
- {
- if (se.StatusCode == SmtpStatusCode.MailboxBusy)
- {
- new QueueService().QueueMessage(objMail);
- Log.WriteCommonLog("SMTP Exception:" + GetExceptionMessageString(se));
- }
- else
- {
- Log.WriteCommonLog("SMTP Exception:" + GetExceptionMessageString(se));
- }
- }
- catch (System.Net.WebException we)
- {
- if ((we.Status == System.Net.WebExceptionStatus.ConnectFailure) || (we.Status == System.Net.WebExceptionStatus.ConnectionClosed) || (we.Status == System.Net.WebExceptionStatus.ReceiveFailure) || (we.Status == System.Net.WebExceptionStatus.Timeout) || (we.Status == System.Net.WebExceptionStatus.NameResolutionFailure))
- {
- new QueueService().QueueMessage(objMail);
- Log.WriteCommonLog("Web Exception:" + GetExceptionMessageString(we));
- }
- else
- {
- Log.WriteCommonLog("Web Exception:" + GetExceptionMessageString(we));
- }
- }
- catch (Exception ex)
- {
- Log.WriteCommonLog("Web Exception:" + GetExceptionMessageString(ex));
- }
- }
- private string GetExceptionMessageString(Exception exc)
- {
- return "\nException thrown by EmailService\n" + System.DateTime.Now.ToString() + "\nInner Exception:" + exc.InnerException + "\nException Message:" + exc.Message + "\nSource:" + exc.Source + "\nStack Trace:" + exc.StackTrace;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement