示例#1
0
        public static EmailMessage RunTemplate(this MemoryCache cache, string name, string to, object model, string language = null)
        {
            var key = "RunTemplate_" + name;

            cache.GetOrCreate(key, () =>
            {
                var context       = new MailerContext();
                Template template = null;
                if (language != null)
                {
                    template = context.Templates[name + "." + language, string.Empty];
                }
                if (template == null)
                {
                    template = context.Templates[name, string.Empty];
                }
                Engine.Razor.AddTemplate(template.body, name);
                Engine.Razor.AddTemplate(template.subject, name + "|subject");
                return(string.Empty);
            });
            var body    = Engine.Razor.RunCompile(name, model: model);
            var subject = Engine.Razor.RunCompile(name + "|subject", model: model);

            return(new EmailMessage
            {
                PartitionKey = TimeId.NewSortableId(),
                RowKey = string.Empty,
                Subject = subject,
                Body = body,
                To = to
            });
        }
        public static async Task SendActivationMail(RegisteredUser regUser)
        {
            var content = MailerContext.SelectEmailContent("ss");

            if (content == null)
            {
                content = new EmailTemplate()
                {
                    content = @"Hi [NAME], Your account has been Activated and your username is [USERNAME]. ",
                    subject = "Account Activation Confirmation"
                }
            }
            ;

            else if (string.IsNullOrEmpty(content.content))
            {
                content.content = @"Hi [NAME], Your account has been Activated and your username is [USERNAME].";
                content.subject = content.subject ?? "Account Activation Confirmation";
            }
            //var resetToken = "<a href='" + resetTokenAction + (DateTime.UtcNow.ToFileTimeUtc() + ":" + regUser.username).GenerateResetToken() + "'>Activation Link</a>";

            await Mailer.SendMailAsync(new MailContent()
            {
                ccAddress = "",
                Content   = HttpUtility.UrlDecode(HttpUtility.HtmlDecode(content.content))
                            .Replace("[NAME]", regUser.First_name + regUser.Middle_name + regUser.Last_name)
                            .Replace("[USERNAME]", regUser.Username)
                            .Replace("[LINK]", ""),
                Email      = regUser.Email_address,
                Subject    = content.subject,
                TypeOfMail = 1
            }, regUser.Username, regUser.RegistereduserId);
        }
        public static async Task SendBlockedMail(RegisteredUser regUser, string originIp)
        {
            // OTP p = new OTP();
            // long Datetimenow = DateTime.UtcNow.ToFileTimeUtc();
            //OTP O = new OTP(x, Guid.NewGuid().ToByteArray());
            //long Datetimenow = DateTime.UtcNow.ToFileTimeUtc();
            //string token = O.GetCurrentOTP();
            //UIContext.RegisterToken(token, "originip", regUser.user_identifier);
            long   Datetimenow = DateTime.UtcNow.Ticks;
            string t           = regUser.RegistereduserId.ToString() + Guid.NewGuid().ToString();
            string tokens      = ClsFunction.GenerateResetToken(t + ":" + Datetimenow);
            string new_token   = Regex.Replace(tokens, "[^0-9a-zA-Z]+", "");
            string link        = ConfigurationManager.AppSettings["URL_BASE_ResetPassword"];
            string regLink     = string.Format(link, HttpUtility.UrlEncode(new_token));
            var    resetToken  = "<a href='" + regLink + "'>Activation Link</a>";

            RegistrationContext.ResetPasswordToken(regUser.RegistereduserId, new_token, originIp);

            var content = MailerContext.SelectEmailContent("ss");

            if (content == null)
            {
                content = new EmailTemplate()
                {
                    content = @"Hi [NAME], Your Account has been blocked.
                                <br/> Please use the below link to reset your account.<br/>[LINK]<br/>Please note this link will expire in 24 hours",
                    subject = "Your Account has been blocked"
                }
            }
            ;

            else if (string.IsNullOrEmpty(content.content))
            {
                content.content = @"Hi [USERNAME],Your Account has been blocked.
                                <br/>Please use the below OTP to reset your account.<br/>[LINK]<br/>Please note this OTP will expire in 24 hours";
                content.subject = content.subject == "" || content.subject == null ? "Invalid Logn Attempt" : content.subject;
            }
            //var resetToken = "<a href='" + resetTokenAction + (DateTime.UtcNow.ToFileTimeUtc() + ":" + regUser.username).GenerateResetToken() + "'>Activation Link</a>";

            await Mailer.SendMailAsync(new MailContent()
            {
                ccAddress = "",
                Content   = HttpUtility.UrlDecode(HttpUtility.HtmlDecode(content.content))
                            .Replace("[NAME]", regUser.First_name + regUser.Middle_name + regUser.Last_name)
                            .Replace("[USERNAME]", regUser.Username)
                            .Replace("[LINK]", ""),
                Email      = regUser.Email_address,
                Subject    = content.subject,
                TypeOfMail = 1
            }, regUser.Username, regUser.RegistereduserId);
        }
        public static async Task SendMail(RegisteredUser regUser)
        {
            //OTP otp = new OTP();
            //OTP otp;
            // OTP O = new OTP(x,Guid.NewGuid().ToByteArray());
            long Datetimenow = DateTime.UtcNow.Ticks;
            //string token = O.GetCurrentOTP();//OTPGeneration.GenerateOTP();
            // string t = otp.GetNextOTP();
            string t          = regUser.RegistereduserId.ToString() + Guid.NewGuid().ToString();
            string token      = ClsFunction.GenerateResetToken(t + ":" + Datetimenow);
            string new_token  = Regex.Replace(token, "[^0-9a-zA-Z]+", "");
            string link       = ConfigurationManager.AppSettings["URL_BASE_Register"];
            string regLink    = string.Format(link, HttpUtility.UrlEncode(new_token));
            var    resetToken = "<a href='" + regLink + "'>Activation Link</a>";

            RegistrationContext.RegisterToken(new_token, regUser.Registration_ip, regUser.RegistereduserId);

            var content = MailerContext.SelectEmailContent("ss");

            if (content == null)
            {
                content = new EmailTemplate()
                {
                    content = @"Hi [NAME], Your account has been created and your username is [USERNAME]
                        <br/>Please use the below link to activate your account.<br/>Your LINK is [LINK]<br/>Please note this link will expire in 24 hours",
                    subject = "Your account has been created"
                }
            }
            ;

            else if (string.IsNullOrEmpty(content.content))
            {
                content.content = @"Hi [NAME], Your account has been created and your username is [USERNAME]
                        <br/>Please use the below link to activate your account.<br/>Your LINK is [LINK]<br/>Please note this link will expire in 24 hours";
                content.subject = content.subject ?? "Your account has been created";
            }
            //var resetToken = "<a href='" + resetTokenAction + (DateTime.UtcNow.ToFileTimeUtc() + ":" + regUser.username).GenerateResetToken() + "'>Activation Link</a>";

            await Mailer.SendMailAsync(new MailContent()
            {
                ccAddress = "",
                Content   = HttpUtility.UrlDecode(HttpUtility.HtmlDecode(content.content))
                            .Replace("[NAME]", regUser.First_name + regUser.Middle_name + regUser.Last_name)
                            .Replace("[USERNAME]", regUser.Username)
                            .Replace("[LINK]", resetToken),
                Email      = regUser.Email_address,
                Subject    = content.subject,
                TypeOfMail = 1
            }, regUser.Username, regUser.RegistereduserId);
        }
示例#5
0
 public static long Info(string TxRefId, string description, string shortDescription, string msgFunction, string msgType, string userId)
 {
     return(MailerContext.AddLog(new tb_logs()
     {
         description = string.Format(description, userId),
         short_description = shortDescription,
         msg_function = msgFunction,
         msg_type = msgType,
         status = "S",
         user_Identifier = userId,
         transaction_ref_id = TxRefId,
         created_on = DateTime.UtcNow
     }));
 }
示例#6
0
        public async Task <ActionResult> SendToAll()
        {
            MailerContext db        = new MailerContext();
            var           Receivers = db.Receivers.ToList();


            foreach (var receiver in Receivers)
            {
                var message = await GetEmailTemplate("WelcomeTemplate");

                message = message.Replace("@ViewBag.FirstName", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(receiver.FirstName));
                MessageService.SendEmailAsync(receiver.EmailAddress, "Welcome", message);
                return(View("EmailSent"));
            }
            return(new HttpNotFoundResult("Couldn't send Messages"));
        }
示例#7
0
 public static bool EditSmtpSetting(SmtpSettingModel model)
 {
     return(MailerContext.EditSmtpSetting(new tb_smtp_settings()
     {
         active = model.IsActive,
         address = model.Address,
         from_address = model.FromAddress,
         password = model.Password,
         last_modified_by = model.LastModifiedBy,
         port = model.Port,
         smtp_type = model.Type,
         smtp_setting_id = model.SMTPSettingId,
         use_anonymous = model.UseAnonymous,
         username = model.UserName
     }));
 }
示例#8
0
 static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
 {
     if (e.Error != null)
     {
         MailerContext.AddException(new tb_logs()
         {
             description        = e.Error.Message,
             error_code         = "999",
             error_description  = e.Error.Data != null ? JsonConvert.SerializeObject(e.Error.Data) : "",
             function_name      = "MAILER",
             msg_function       = "MAILER",
             msg_type           = "SEND MAIL",
             short_description  = "Error while sending mail",
             stack_trace        = e.Error.StackTrace,
             status             = "F",
             trace_data         = e.Error.InnerException == null ? "" : e.Error.InnerException.ToString(),
             transaction_ref_id = DateTime.UtcNow.Ticks.ToString(),
             user_Identifier    = null ///e.Result
         });
     }
 }
示例#9
0
 public static void Error <Req, Res>(Exception ex, string userId, string referenceId, string functionName, string messageFunction,
                                     string messageType, string shortDesc, Req Request, Res Response, string errorcode = "999", long logId = 0)
 {
     MailerContext.AddException(new tb_logs()
     {
         // Holds the response data
         description        = JsonConvert.SerializeObject(Request),
         error_code         = errorcode,
         error_description  = ex.ToString(),
         function_name      = functionName,
         msg_function       = messageFunction,
         msg_type           = messageType,
         short_description  = shortDesc,
         stack_trace        = ex.StackTrace,
         status             = "F",
         trace_data         = Response == null ? "" : JsonConvert.SerializeObject(Response),
         transaction_ref_id = referenceId,
         user_Identifier    = userId,
         log_id             = logId,
         created_on         = DateTime.UtcNow,
     });
 }
示例#10
0
        private static void StartMailer(string email, string ccAddress, string subject, string content, string txnId, int mailType, string username, long user_id, int type)
        {
            mailType = 1;
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += (s, e) =>
            {
                tb_email_logs log = new tb_email_logs();
                try
                {
                    log = MailerContext.LogEmail(new tb_email_logs()
                    {
                        cc               = ccAddress ?? "",
                        content          = HttpUtility.UrlDecode(content ?? ""),
                        error            = "",
                        has_attachement  = false,
                        subject          = subject ?? "",
                        to               = email ?? "",
                        log_entered_date = DateTime.UtcNow,
                        user_identifier  = user_id.ToString()
                    });
                }
                catch (Exception)
                {
                }

                try
                {
                    var smtp = SmtpProvider.GetSmtpSettingFor(mailType);
                    // TODO : Fetch the details from the Web-Admin
                    MailMessage message = new MailMessage();
                    if (email.Contains(";"))
                    {
                        message.From = new MailAddress(smtp.FromAddress);
                        foreach (var item in email.Split(';'))
                        {
                            if (string.IsNullOrEmpty(item))
                            {
                                continue;
                            }
                            message.To.Add(new MailAddress(item));
                        }
                        message.Subject = subject;
                        message.Body    = HttpUtility.UrlDecode(content);
                    }
                    else if (email.Contains(","))
                    {
                        message.From = new MailAddress(smtp.FromAddress);
                        foreach (var item in email.Split(','))
                        {
                            if (string.IsNullOrEmpty(item))
                            {
                                continue;
                            }
                            message.To.Add(new MailAddress(item));
                        }
                        message.Subject = subject;
                        message.Body    = HttpUtility.UrlDecode(content);
                    }
                    else
                    {
                        message = new MailMessage(smtp.FromAddress, email, subject, HttpUtility.UrlDecode(content));
                    }

                    message.IsBodyHtml = true;
                    //message.Attachments.Add(attachment);
                    SmtpClient cnt = new SmtpClient(smtp.Address, Convert.ToInt16(smtp.Port));
                    cnt.Timeout = 2000000;
                    cnt.UseDefaultCredentials = false;
                    cnt.EnableSsl             = true;
                    //cnt.Host = smtp.Address;

                    if (!smtp.UseAnonymous)
                    {
                        cnt.Credentials = new System.Net.NetworkCredential(smtp.UserName, smtp.Password);
                    }
                    cnt.Send(message);
                }
                catch (Exception ex)
                {
                    try
                    {
                        e.Result  = email;
                        log.error = ex.ToString();
                        MailerContext.UpdateEmailLog(log);
                    }
                    catch (Exception)
                    {
                    }
                    //throw ex;
                }
            };
            worker.RunWorkerAsync();
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        }
示例#11
0
 public static SmtpSettingModel GetSmtpSettingFor(int smtpType)
 {
     return(MailerContext.GetSmtpFor(smtpType));
 }
示例#12
0
 public static SmtpSettingModel GetSmtp()
 {
     return(MailerContext.GetSmtp());
 }