internal static void SendMail(string Mail, MailObject passobj) { MailMessage message = new MailMessage(Mail, passobj.MailSender); message.Subject = passobj.MailSubject; message.Body = passobj.MailTemplate; message.IsBodyHtml = true; SmtpClient client = new SmtpClient(passobj.MailSMTPServer); client.UseDefaultCredentials = true; client.Send(message); }
public bool ResetPassword(MailObject Mailobj) { try { ISTAT.SingleSignON.Service.Model.DBUserEntities cnn = new ISTAT.SingleSignON.Service.Model.DBUserEntities(); User newut = cnn.Users.FirstOrDefault(u => Mailobj.EmailUser != null && u.Email.Trim() == Mailobj.EmailUser.Trim()); if (newut == null) { throw new Exception("User not found"); } string OLDPass = newut.Password; //Genera nuova password string newPass = Utils.RandomPassword(); //Salva Nuova Password newut.Password = Utils.CriptaPassword(newPass); var errors = cnn.GetValidationErrors(); if (errors != null && errors.Count(e => !e.IsValid) > 0) { throw new Exception(string.Format("Change Password Error: {0}", errors.FirstOrDefault(e => !e.IsValid).ValidationErrors.FirstOrDefault().ErrorMessage)); } int ris = cnn.SaveChanges(); try { //InviaMail Mailobj.MailTemplate = Mailobj.MailTemplate.Replace("##NEWPASSWORD##", newPass); Utils.SendMail(newut.Email, Mailobj); } catch (Exception ex) { newut.Password = OLDPass; errors = cnn.GetValidationErrors(); if (errors != null && errors.Count(e => !e.IsValid) > 0) { throw new Exception(string.Format("Change Password Error: {0}", errors.FirstOrDefault(e => !e.IsValid).ValidationErrors.FirstOrDefault().ErrorMessage)); } ris = cnn.SaveChanges(); throw new Exception(string.Format("SendMail error: {0}", ex.Message)); } return(ris > 0); } catch (Exception ex) { throw new Exception(string.Format("Change Password Error: {0}", ex)); } }