public void ResetPassword(long siteId, string email, string redirectLink) { awSite site = new SiteLibrary().Get(siteId); if (site == null || !site.isEnabled) { throw new Exception(ErrorLibrary.ErrorMessage(ErrorLibrary.SITE.DOES_NOT_EXIST)); } if (site.userResetPasswordEmailTemplateId == null) { throw new Exception(ErrorLibrary.ErrorMessage(ErrorLibrary.SITE.RESET_PASSWORD_TEMPLATE_DOES_NOT_EXIST)); } //GET THE USER awUser user = (from l in _context.awUsers where l.email.Equals(email) && l.isEnabled != false select l).FirstOrDefault(); if (user == null) { throw new Exception(ErrorLibrary.ErrorMessage(ErrorLibrary.USER.DOES_NOT_EXIST)); } //CREATE NEW PASSWORD const int PASSWORD_LENGTH = 6; string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789"; Random randNum = new Random(); char[] chars = new char[PASSWORD_LENGTH]; int allowedCharCount = _allowedChars.Length; for (int i = 0; i < PASSWORD_LENGTH; i++) { chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())]; } string password = new string(chars); //FIRST SEND EMAIL BEFORE RESETING THE PASSWORD AWAPI_BusinessLibrary.library.EmailTemplateLib emailLib = new EmailTemplateLib(); awEmailTemplate template = emailLib.Get(site.userResetPasswordEmailTemplateId.Value); if (template == null) { throw new Exception(ErrorLibrary.ErrorMessage(ErrorLibrary.SITE.RESET_PASSWORD_TEMPLATE_DOES_NOT_EXIST)); } emailLib.Send(site.userResetPasswordEmailTemplateId.Value, email, "firstname|" + user.firstName, "lastname|" + user.lastName, "password|" + password, "link|" + redirectLink, "date|" + DateTime.Now.ToString()); //UPDATE PASSWORD UpdatePassword(user.userId, password); }
/// <summary> /// /// </summary> /// <param name="siteId"></param> /// <param name="userId"></param> /// <param name="title"></param> /// <param name="description"></param> /// <param name="emailFrom"></param> /// <param name="emailSubject"></param> /// <param name="emailBody"></param> /// <returns></returns> public long Add(long siteId, long userId, string title, string description, string emailFrom, string emailSubject, string emailBody) { long id = AWAPI_Common.library.MiscLibrary.CreateUniqueId(); awEmailTemplate template = new awEmailTemplate(); template.emailTemplateId = id; template.siteId = siteId; template.title = title; template.description = description; template.emailFrom = emailFrom; template.emailSubject = emailSubject; template.emailBody = emailBody; template.userId = userId; template.lastBuildDate = DateTime.Now; template.createDate = DateTime.Now; _context.awEmailTemplates.InsertOnSubmit(template); _context.SubmitChanges(); return(id); }
/// <summary> /// /// </summary> /// <param name="emailTemplateId"></param> /// <param name="userId"></param> /// <param name="title"></param> /// <param name="description"></param> /// <param name="emailFrom"></param> /// <param name="emailSubject"></param> /// <param name="emailBody"></param> /// <returns></returns> public bool Update(long emailTemplateId, long userId, string title, string description, string emailFrom, string emailSubject, string emailBody) { awEmailTemplate template = _context.awEmailTemplates. FirstOrDefault(st => st.emailTemplateId.Equals(emailTemplateId)); if (template == null) { return(false); } template.title = title; template.description = description; template.emailFrom = emailFrom; template.emailSubject = emailSubject; template.emailBody = emailBody; template.userId = userId; template.lastBuildDate = DateTime.Now; _context.SubmitChanges(); return(true); }
/// <summary> /// args: /// example: /// Hello {Name}, we contacted you on {Date}, etc.... /// /// {Name} and {Date} can be replaced by the args. /// We set these values from by args values. Value and the parameter name /// are seperated with pipe '|' /// Name|Omer Yesil, date|02/03/2005 /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="subject"></param> /// <param name="templatePath">example: /en/mail/contestregister.html</param> /// <param name="args">commenter|This is a test</param> public bool Send(long emailTemplateId, string to, params string[] args) { awEmailTemplate t = Get(emailTemplateId); if (t == null || String.IsNullOrEmpty(t.emailFrom) || String.IsNullOrEmpty(t.emailSubject) || String.IsNullOrEmpty(t.emailBody)) { return(false); } string subject = t.emailSubject; string body = t.emailBody; if (args != null && args.Length > 0) { foreach (string arg in args) { if (String.IsNullOrEmpty(arg)) { continue; } string[] parts = arg.Split('|'); string valueName = ""; string value = ""; if (parts.Length > 0) { valueName = parts[0]; if (parts.Length > 1) { value = parts[1]; } subject = Regex.Replace(subject, "{" + valueName + "}", value, RegexOptions.IgnoreCase); body = Regex.Replace(body, "{" + valueName + "}", value, RegexOptions.IgnoreCase); } } } SmtpClient client = new SmtpClient(ConfigurationLibrary.Config.smtpServer); MailMessage mm = new MailMessage(t.emailFrom, to); mm.IsBodyHtml = true; mm.Subject = subject; mm.SubjectEncoding = System.Text.Encoding.UTF8; mm.Body = body; mm.BodyEncoding = System.Text.Encoding.UTF8; try { client.ServicePoint.MaxIdleTime = 1; client.Send(mm); } catch (Exception ex) { string log = System.Web.HttpContext.Current.Server.MapPath("~/log.txt"); System.IO.TextWriter tw = new System.IO.StreamWriter(log); tw.WriteLine("MAIL ERROR ---------------------"); tw.WriteLine("SMTP:" + ConfigurationLibrary.Config.smtpServer); tw.WriteLine("ERROR:" + ex.Message); if (ex.InnerException != null) { tw.WriteLine("DETAIL:" + ex.InnerException.Message); } tw.Close(); throw ex; } finally { mm.Dispose(); } return(true); }