public static bool SendStudentImportEmail(string Token, string TO, int UserId, string Url = "", string MemberName = null, int TokenId = 0) { UserTokenEmailService _userTokenEmailService = new UserTokenEmailService(); DataSet ds = _userTokenEmailService.GetImportDetails(UserId); string USERNAME = string.Empty; string PASSWORD = _userImportPassword; if (ds != null) { if (ds.Tables.Count != 0) { USERNAME = ds.Tables[0].Rows[0]["UserName"].ToString().ToLower(); } } bool blnSent = false; MessageTemplateService _MessageTemplateService = new MessageTemplateService(); MessageTemplate messageTemplate = null; Tokenizer _tokenizer = new Tokenizer(false); try { messageTemplate = _MessageTemplateService.GetMessageTemplateByName("Student.Import.Email"); if (messageTemplate == null) { throw new ArgumentNullException("messageTemplate"); } //email account var emailAccount = _MessageTemplateService.GetEmailAccountById(messageTemplate.EmailAccountId); var tokens = new List <Token>(); tokens.Add(new Token("STUDENTUSERNAME", USERNAME)); tokens.Add(new Token("STUDENTPASSWORD", PASSWORD)); tokens.Add(new Token("SITEURL", Url)); var subject = messageTemplate.Subject; var body = messageTemplate.Body; //Replace subject and body tokens var subjectReplaced = _tokenizer.Replace(subject, tokens, false); var bodyReplaced = _tokenizer.Replace(body, tokens, false); QueuedEmail _QueuedEmail = new QueuedEmail { Priority = 1, From = emailAccount.Email, FromName = emailAccount.DisplayName, To = TO, ToName = "",//MemberName, Subject = subjectReplaced, Body = bodyReplaced, CreatedOnUtc = DateTime.UtcNow, EmailAccountId = emailAccount.Id, TokenId = TokenId }; blnSent = new QueuedEmailService().InsertQueuedEmail(_QueuedEmail); } catch (Exception ex) { var log = ex.InnerException + " occured while sending mail to " + TO + " at " + DateTime.Now + "\n"; File.AppendAllText(System.Web.Hosting.HostingEnvironment.MapPath("~\\App_Data\\EmailErrorLog.txt"), log); } finally { _MessageTemplateService = null; messageTemplate = null; _tokenizer = null; } return(blnSent); }
public static bool SendHelpAcknowledgementEmail(string Token, string UserEmail, string Query, string CreatedOn, string Role, string StudentFirstName = "", string StudentLastName = "", string Url = "", string AdminEmail = null, int TokenId = 0) { bool blnSent = false; MessageTemplateService _MessageTemplateService = new MessageTemplateService(); MessageTemplate messageTemplate = null; Tokenizer _tokenizer = new Tokenizer(false); string ToEmail = UserEmail; try { var tokens = new List <Token>(); tokens.Add(new Token("SITEURL", Url)); if (String.IsNullOrEmpty(AdminEmail)) { if (Role.ToLower() == "student") { messageTemplate = _MessageTemplateService.GetMessageTemplateByName("Help.Acknowledgement.Student.Email"); tokens.Add(new Token("STUDENTFIRSTNAME", StudentFirstName)); tokens.Add(new Token("STUDENTLASTNAME", StudentLastName)); } else { messageTemplate = _MessageTemplateService.GetMessageTemplateByName("Help.Acknowledgement.OtherUser.Email"); } } else { messageTemplate = _MessageTemplateService.GetMessageTemplateByName("Help.Acknowledgement.AdminEmail"); ToEmail = AdminEmail; tokens.Add(new Token("USEREMAIL", UserEmail)); } if (messageTemplate == null) { throw new ArgumentNullException("messageTemplate"); } //email account var emailAccount = _MessageTemplateService.GetEmailAccountById(messageTemplate.EmailAccountId); tokens.Add(new Token("REFERENCEID", Token)); tokens.Add(new Token("QUERY", Query)); tokens.Add(new Token("QUERYDATE", CreatedOn)); var subject = messageTemplate.Subject; var body = messageTemplate.Body; //Replace subject and body tokens var subjectReplaced = _tokenizer.Replace(subject, tokens, false); var bodyReplaced = _tokenizer.Replace(body, tokens, false); QueuedEmail _QueuedEmail = new QueuedEmail { Priority = 1, From = emailAccount.Email, FromName = emailAccount.DisplayName, To = ToEmail, Subject = subjectReplaced, Body = bodyReplaced, CreatedOnUtc = DateTime.UtcNow, EmailAccountId = emailAccount.Id, TokenId = TokenId }; blnSent = new QueuedEmailService().InsertQueuedEmail(_QueuedEmail); } catch (Exception ex) { var log = ex.InnerException + " occured while sending mail to " + ToEmail + " at " + DateTime.Now + "\n"; File.AppendAllText(System.Web.Hosting.HostingEnvironment.MapPath("~\\App_Data\\EmailErrorLog.txt"), log); } finally { _MessageTemplateService = null; messageTemplate = null; _tokenizer = null; } return(blnSent); }