public void MailingCallback(IAsyncResult ar) { // Restore orginial state ASyncMailingState mailingState = (ASyncMailingState)ar.AsyncState; //retrieve the object on which End needs to be called ASyncMailing mailing = mailingState.AsyncMailing; mailing.EndMailIt(ar); // Let the world know that mailing is finished SyncDataStore.SetRecords(mailingState.SessionId, true); }
/// <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); } }