public void RemindRecipients(int surveyId)
		{
			while (true)
			{
				IEmailing mailService = EmailingFactory.Create();
				EmailingMessage message = new EmailingMessage();
				message.FromEmail = "*****@*****.**";
				message.Subject = DateTime.Now.ToString();
				message.Body = "ok";
				message.ToEmail = "*****@*****.**";
				mailService.SendEmail(message);
				System.Threading.Thread.Sleep(10000); // 10 seconds wait
			}


		}
        /// <summary>
        /// Sends the given message
        /// </summary>
        public void SendEmail(EmailingMessage mail)
        {
            MailMessage objMessage = new MailMessage();

            SmtpClient smtp = new SmtpClient(EmailConfig.SmtpServer);

            // Check if server requires authentication
            if (EmailConfig.NSurveySMTPServerAuthUserName != null)
            {
                smtp.Credentials = new NetworkCredential(EmailConfig.NSurveySMTPServerAuthUserName, EmailConfig.NSurveySMTPServerAuthPassword);
            }

            objMessage.To.Add(mail.ToEmail);
            objMessage.From    = new MailAddress((mail.FromName == null) ? mail.FromEmail : "\"" + mail.FromName + "\" <" + mail.FromEmail + ">");
            objMessage.Subject = mail.Subject;

            ContentType htmlMimeType = new System.Net.Mime.ContentType("text/html");

            // Construct the body as HTML.
            string bodyHtml = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";

            bodyHtml += "<html><head><meta http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">";
            bodyHtml += "</head><body><div><font size=\"2\" face=\"arial\">" + mail.Body;
            bodyHtml += "</font></div></body></html>";

            AlternateView alternateHtml = AlternateView.CreateAlternateViewFromString(bodyHtml, null, "text/html");

            alternateHtml.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
            objMessage.AlternateViews.Add(alternateHtml);


            ContentType mimeType = new System.Net.Mime.ContentType("text/plain");
            // Construct the body as TEXT.
            string bodyPlain = StripHTML(mail.Body);

            AlternateView alternatePlain = AlternateView.CreateAlternateViewFromString(bodyPlain, null, "text/plain");

            alternatePlain.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
            objMessage.AlternateViews.Add(alternatePlain);

            objMessage.BodyEncoding = System.Text.Encoding.UTF8;

            smtp.Port      = Int32.Parse(EmailConfig.SmtpServerPort);
            smtp.EnableSsl = Boolean.Parse(EmailConfig.SmtpServerEnableSSL);
            smtp.Send(objMessage);
            System.Threading.Thread.Sleep(500);
        }
 /// <summary>
 /// Generates and sends a report and fill the message body
 /// depending on the notification mode chosen
 /// </summary>
 public void SendReport(NotificationMode notificationMode, string surveyTitle, string emailFrom, string emailTo, string emailSubject)
 {
     IEmailing emailing = EmailingFactory.Create();
     EmailingMessage mail = new EmailingMessage();
     mail.FromEmail = emailFrom;
     if ((notificationMode == NotificationMode.EntryReport) || (notificationMode == NotificationMode.OnlyAnswersReport))
     {
         mail.Body = this.GenerateTextReport(notificationMode == NotificationMode.OnlyAnswersReport);
     }
     else
     {
         mail.Body = ResourceManager.GetString("ShortNotificationMessage");
     }
     mail.Subject = (emailSubject == null) ? string.Format(ResourceManager.GetString("NotificationSubject"), surveyTitle) : emailSubject;
     mail.ToEmail = emailTo;
     emailing.SendEmail(mail);
 }
        // Notify System Operators about an exception
        public static void NotifySystemOps(Exception exc)
        {
        //    // Include code for notifying IT system operators

            IEmailing emailing = EmailingFactory.Create();
            EmailingMessage mail = new EmailingMessage();
            mail.FromEmail = EmailConfig.NSurveySMTPServerAuthUserName;

            mail.Body = "Survey Project Warning Message: Global Page Error details."
                + "<br /><br />Inner Exception Type: " + exc.InnerException.GetType().ToString()
                + "<br /><br />Inner Exception: " + exc.InnerException.Message
                + "<br /><br />Inner Source: " + exc.InnerException.Source
                + "<br /><br />Inner Stack Trace: " + exc.InnerException.StackTrace
                + "<br /><br />Exception Type: " + exc.GetType().ToString()
                + "<br /><br />Exception: " + exc.Message
                + "<br /><br />Stack Trace: " + exc.StackTrace;

            mail.Subject = "Survey Project Warning Message: Global Page Error";
            mail.ToEmail = EmailConfig.NSurveySMTPServerAuthUserName;
            emailing.SendEmail(mail);

        }
        private void SendInvitationButton_Click(object sender, System.EventArgs e)
        {
            //Init Async call
            ASyncMailing mail = new ASyncMailing();
            ASyncMailingState mailingState = new ASyncMailingState(mail, Session.SessionID);
            AsyncCallback cb = new AsyncCallback(MailingCallback);

            // Build message to send
            EmailingMessage message;
            message = new EmailingMessage();
            message.FromEmail = FromTextBox.Text;
            message.FromName = FromNameTextbox.Text;
            message.Subject = SubjectTextBox.Text;
            message.Body = HttpUtility.HtmlDecode(MailingCKEditor.Text);
            message.Format = EmailFormat.Html;

            // Dummy session variable to init the session in case
            // it was not initialized
            Session["IamADummy"] = "StartIt";

            // Start the mailing
            mail.BeginMailIt(Session.SessionID, SurveyId,
                AnonymousEntriesCheckBox.Checked, MailingListTextBox.Text,
                message, cb, mailingState);

            // Open the poll page while the mailing continues
            UINavigator.NavigateToMailingPoll(SurveyId, MenuIndex);
        }
		/// <summary>
		/// Sends the given message
		/// </summary>
		public void SendEmail(EmailingMessage mail)
		{
			MailMessage objMessage = new MailMessage();

			SmtpClient smtp = new SmtpClient(EmailConfig.SmtpServer);			

			// Check if server requires authentication
			if (EmailConfig.NSurveySMTPServerAuthUserName != null)
			{
				smtp.Credentials = new NetworkCredential(EmailConfig.NSurveySMTPServerAuthUserName, EmailConfig.NSurveySMTPServerAuthPassword);
			}

			objMessage.To.Add(mail.ToEmail);
			objMessage.From = new MailAddress ((mail.FromName == null) ? mail.FromEmail : "\""+mail.FromName+"\" <" +mail.FromEmail+ ">");
			objMessage.Subject = mail.Subject;
			
			ContentType htmlMimeType = new System.Net.Mime.ContentType("text/html");
			
			// Construct the body as HTML.
			string bodyHtml = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
			bodyHtml += "<html><head><meta http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">";
			bodyHtml += "</head><body><div><font size=\"2\" face=\"arial\">" + mail.Body;
			bodyHtml += "</font></div></body></html>";

            AlternateView alternateHtml = AlternateView.CreateAlternateViewFromString(bodyHtml, null, "text/html");
            alternateHtml.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
            objMessage.AlternateViews.Add(alternateHtml);


            ContentType mimeType = new System.Net.Mime.ContentType("text/plain");
            // Construct the body as TEXT.
			string bodyPlain = StripHTML(mail.Body);

            AlternateView alternatePlain = AlternateView.CreateAlternateViewFromString(bodyPlain, null, "text/plain");
            alternatePlain.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
            objMessage.AlternateViews.Add(alternatePlain);	
			
			objMessage.BodyEncoding = System.Text.Encoding.UTF8;

			smtp.Port = Int32.Parse(EmailConfig.SmtpServerPort);
            smtp.EnableSsl = Boolean.Parse(EmailConfig.SmtpServerEnableSSL);
			smtp.Send(objMessage );
			System.Threading.Thread.Sleep(500);
		}
  /// <summary>
  /// description: ......
  /// </summary>
  /// <param name="sessionId">description .....</param>
  /// <param name="surveyId">description ....</param>
  /// <param name="anonymousEntries">description ..</param>
  /// <param name="mailingEmails">description .......</param>
  /// <param name="message">description ......</param>
  public void MailIt(string sessionId, int surveyId, bool anonymousEntries, string mailingEmails, EmailingMessage message)
  {
   IEmailing mailService = EmailingFactory.Create();
   string guid = null,
     taggedMessage = message.Body,
     email;
   string[] mailingList = mailingEmails.Split(',');

   SyncDataStore.SetRecords(sessionId + ":" + "Progress", 0);
   SyncDataStore.SetRecords(sessionId + ":" + "FailedEmails", 0);

   int failedEmails = 0;
    
   // Send the invitation email to the list
   for (int i = 0; i < mailingList.Length; i++)
   {
    email = mailingList[i].Trim();
    guid = null;
    try
    {
     guid = new Voter().GenerateVoterInvitationUId(surveyId, email, anonymousEntries);
     message.Body = message.Body.Replace("[--invitationid-]", guid);
     message.ToEmail = email;
     mailService.SendEmail(message);
     message.Body = taggedMessage;
    }
    catch (Exception ex)
    {
     try
     {
      if (guid != null)
      {
       // rollback db state if an email has been saved
       new Voter().DeleteVoterInvitation(surveyId, email);
      }

      InvitationLogData invitationLogs = new InvitationLogData();
      InvitationLogData.InvitationLogsRow invitationLog = invitationLogs.InvitationLogs.NewInvitationLogsRow();
      invitationLog.ExceptionMessage = (ex.InnerException != null && ex.InnerException.InnerException != null) ? 
       ex.InnerException.InnerException.Message : ex.Message.ToString();
      invitationLog.ExceptionType = (ex.InnerException != null && ex.InnerException.InnerException != null) ? 
       ex.InnerException.InnerException.GetType().ToString() : ex.GetType().ToString();
      invitationLog.SurveyId = surveyId;
      invitationLog.Email = email;
      invitationLog.ErrorDate = DateTime.Now;
      invitationLogs.InvitationLogs.AddInvitationLogsRow(invitationLog);
     
      // Log error in the database
      new Voter().LogInvitationError(invitationLogs);
      failedEmails++;
     }
     catch (Exception e)
     { 
      SyncDataStore.SetRecords(sessionId + ":" + "Error", e);
     }
    }

    SyncDataStore.UpdateRecords(sessionId + ":" + "Progress", (i * 100) / mailingList.Length);
    SyncDataStore.UpdateRecords(sessionId + ":" + "FailedEmails", failedEmails);
   }
  }
 /// <summary>
 /// description: Begins an asynchronous call
 /// </summary>
 /// <param name="sessionId">description .....</param>
 /// <param name="surveyId">description ....</param>
 /// <param name="anonymousEntries">description ..</param>
 /// <param name="mailingEmails">description .... ...</param>
 /// <param name="message">description ......</param>
 /// <param name="callback">description .. .....</param>
 /// <param name="state">description ..... .</param>
 /// <returns>Description returned value.</returns>
 public System.IAsyncResult BeginMailIt(string sessionId, int surveyId, bool anonymousEntries, string mailingEmails, EmailingMessage message, System.AsyncCallback callback, object state)
 {
  return this.mailItDelegate.BeginInvoke(sessionId, surveyId, anonymousEntries, mailingEmails, message, callback, state);
 }