Beispiel #1
0
 public DataEntities.Model.User GetUserByUserName(string UserName)
 {
     DataEntities.Model.User result = null;
     try
     {
         result = _userRepo.FindAllBy(u => u.AspNetUser.UserName.Trim().ToLower() == UserName.Trim().ToLower()).FirstOrDefault();
     }
     catch (Exception e)
     {
         Commons.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "UserName = " + UserName);
     }
     return(result);
 }
Beispiel #2
0
 /// <summary>
 /// Return the specified user
 /// </summary>
 /// <param name="UserId"></param>
 /// <returns></returns>
 public DataEntities.Model.User GetUserById(int UserId)
 {
     DataEntities.Model.User result = null;
     try
     {
         result = _userRepo.Get(UserId);
     }
     catch (Exception e)
     {
         result = null;
         Commons.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "UserId = " + UserId);
     }
     return(result);
 }
Beispiel #3
0
        /// <summary>
        /// Send a mail to a user
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="EMailTypeId"></param>
        /// <returns></returns>
        public bool SendEMailToUser(int UserId, int EMailTypeId)
        {
            bool result = false;

            try
            {
                DataEntities.Model.User UserMail = _userRepo.Get(UserId);
                if (UserMail != null)
                {
                    Email Email = new Email();
                    Email.UserId          = UserId;
                    Email.EMailTypeId     = EMailTypeId;
                    Email.RootPathDefault = FileHelper.GetRootPathDefault() + @"\";
                    string baseUrl = WebsiteURL + "/" + Languages.ToString(UserMail.LanguageId);

                    List <Tuple <string, string> > EmailContent = new List <Tuple <string, string> >();
                    switch (EMailTypeId)
                    {
                    case CommonsConst.EmailTypes.Forgotpassword:
                        string ResetPasswordUrl = baseUrl + "/ResetPassword/" + UserMail.Id + "/" + Commons.HashHelpers.HashEncode(UserMail.ResetPasswordToken);
                        EmailContent.Add(new Tuple <string, string>("#ResetPasswordUrl_watcher#", ResetPasswordUrl));
                        break;

                    case CommonsConst.EmailTypes.UserWelcome:
                        string ConfirmEmailUrl = baseUrl + "/ConfirmEmail/" + UserMail.Id + "/" + Commons.HashHelpers.HashEncode(UserMail.EmailConfirmationToken);
                        EmailContent.Add(new Tuple <string, string>("#ConfirmEmailUrl_watcher#", ConfirmEmailUrl));
                        break;

                    case CommonsConst.EmailTypes.ResetPassword:
                        string ChangePasswordUrl = baseUrl + "/MyProfile/password";
                        EmailContent.Add(new Tuple <string, string>("#ChangePasswordUrl_watcher#", ChangePasswordUrl));
                        break;
                    }
                    Email.EmailContent = EmailContent;


                    result = SendMail(Email);
                }
            }
            catch (Exception e)
            {
                result = false;
                Commons.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "EMailTypeId = " + EMailTypeId + " and UserId =" + UserId);
            }
            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Send a mail to a user
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="EMailTypeId"></param>
        /// <returns></returns>
        public bool SendEMailToUser(string UserName, int EMailTypeId)
        {
            bool result = false;

            try
            {
                DataEntities.Model.User UserMail = _userRepo.FindAllBy(u => u.AspNetUser.UserName.Trim().ToLower() == UserName.Trim().ToLower()).FirstOrDefault();
                if (UserMail != null)
                {
                    int UserId = UserMail.Id;
                    result = SendEMailToUser(UserId, EMailTypeId);
                }
            }
            catch (Exception e)
            {
                result = false;
                Commons.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "EMailTypeId = " + EMailTypeId + " and UserName =" + UserName);
            }
            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// Methode used to send an email
        /// </summary>
        /// <param name="Email"></param>
        /// <returns></returns>
        public bool SendMail(Email Email)
        {
            bool result = false;

            try
            {
                int    LanguageId = CommonsConst.Languages.English;
                string LangTag    = CommonsConst.Languages.ToString(LanguageId);

                if (String.IsNullOrWhiteSpace(Email.RootPathDefault))
                {
                    Email.RootPathDefault = FileHelper.GetRootPathDefault() + @"\";
                }


                if (Email.EmailContent == null)
                {
                    Email.EmailContent = new List <Tuple <string, string> >();
                }
                DataEntities.Model.User User = null;
                if (Email.UserId != null && Email.UserId.Value > 0)
                {
                    User = _userRepo.Get(Email.UserId.Value);
                }

                if (User != null && User.Id > 0)
                {
                    LangTag    = User.Language?.Code ?? CommonsConst.Const.DefaultCulture;
                    LanguageId = User.LanguageId;
                    if (String.IsNullOrWhiteSpace(Email.ToEmail))
                    {
                        Email.ToEmail = User.AspNetUser.Email;
                    }
                    Email.EmailContent.Add(new Tuple <string, string>("#UserFirstName#", User.FirstName));
                    Email.EmailContent.Add(new Tuple <string, string>("#UserFullName#", User.LastName));
                }
                else
                {
                    Email.EmailContent.Add(new Tuple <string, string>("#UserFirstName#", "user"));
                    Email.EmailContent.Add(new Tuple <string, string>("#UserFullName#", "user"));
                }
                Email.EmailContent.Add(new Tuple <string, string>("#WebSiteURL#", Utils.Website));
                Email.LanguageId = LanguageId;

                EmailTypeLanguage EMailTypeLanguage = GetEMailTypeLanguage(Email.EMailTypeId, Email.LanguageId);
                if (EMailTypeLanguage == null && Email.LanguageId != Languages.English)
                {
                    EMailTypeLanguage = GetEMailTypeLanguage(Email.EMailTypeId, Languages.English);
                }

                if (EMailTypeLanguage != null && !String.IsNullOrWhiteSpace(Email.ToEmail))
                {
                    if (!Utils.IsProductionWebsite())
                    {
                        Email.EmailContent.Add(new Tuple <string, string>("#RealUserEMail#", "Real mail : " + Email.ToEmail));
                        Email.ToEmail = CommonsConst.Const.EMailDev;
                    }
                    else
                    {
                        Email.EmailContent.Add(new Tuple <string, string>("#RealUserEMail#", "&nbsp;"));
                    }

                    Email.EMailTypeLanguageId = EMailTypeLanguage.Id;
                    Email.EMailTemplate       = EMailTypeLanguage.TemplateName;
                    Email.BasePathFile        = Email.RootPathDefault + Const.BasePathTemplateEMails.Replace("~/", "\\");

                    if (String.IsNullOrWhiteSpace(Email.Subject))
                    {
                        Email.Subject = EMailTypeLanguage.Subject;
                    }
                    if (String.IsNullOrWhiteSpace(Email.FromEmail))
                    {
                        Email.FromEmail = EMailHelper.MailAdress;
                    }
                    Email.AttachmentsMails = new List <System.Net.Mail.Attachment>();
                    if (Email.Attachments != null)
                    {
                        foreach (string file in Email.Attachments)
                        {
                            try
                            {
                                string FileName = FileHelper.GetStorageRoot(file);
                                byte[] BitesTab = FileHelper.GetFileToDownLoad(FileName);
                                if (BitesTab != null)
                                {
                                    Email.AttachmentsMails.Add(new System.Net.Mail.Attachment(new MemoryStream(BitesTab), Path.GetFileName(FileName)));
                                }
                            }
                            catch (Exception e)
                            {
                                result = false;
                                Commons.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "file = " + file);
                            }
                        }
                    }
                    Task.Factory.StartNew(() => SendMailAsync(Email));
                    result = true;
                }
                else
                {
                    result = false;
                    Commons.Logger.GenerateInfo("No emailtypelanguage found : UserMail = " + Email.ToEmail + " and Language = " + Email.LanguageId + " and EMailTypeId = " + Email.EMailTypeId);
                }
            }
            catch (Exception e)
            {
                result = false;
                Commons.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "UserMail = " + Email.ToEmail + " and subject = " + Email.Subject + " and EMailTypeId = " + Email.EMailTypeId);
            }
            return(result);
        }