public IHttpActionResult RagisterUser(User user)
        {
            if (!ModelState.IsValid)
            {
                return(Content(HttpStatusCode.BadRequest, user));
            }
            if (db.Users.Any(a => a.Email == user.Email))
            {
                return(Content(HttpStatusCode.BadRequest, "Duplicate"));
            }
            if (!db.Rolemasters.Any(a => a.Role == "Agent"))
            {
                db.Rolemasters.Add(new Rolemaster()
                {
                    Role = "Agent"
                });
                db.SaveChanges();
            }
            if (!db.Rolemasters.Any(a => a.Role == "Admin"))
            {
                db.Rolemasters.Add(new Rolemaster()
                {
                    Role = "Admin"
                });
                db.SaveChanges();
            }
            if (!db.Rolemasters.Any(a => a.Role == "Supervisor"))
            {
                db.Rolemasters.Add(new Rolemaster()
                {
                    Role = "Supervisor"
                });
                db.SaveChanges();
            }
            if (db.Users.Count() == 0)
            {
                user.UserRoles = "Admin";
            }
            else
            {
                user.UserRoles = "Agent";
            }
            user.Status = "Active";

            db.Users.Add(user);
            db.SaveChanges();
            MailSetup objmailsetup = new MailSetup();

            objmailsetup.Sendmail(db.Users.Where(a => a.Email == user.Email).Select(m => m.UserID).FirstOrDefault(), db.Users.Where(a => a.Email == user.Email).Select(m => m.Name).FirstOrDefault(), db.Users.Where(a => a.Email == user.Email).Select(m => m.Email).FirstOrDefault());
            return(Content(HttpStatusCode.OK, "Success"));
        }
        public async Task SendEmailAsync(string email, string subject, string htmlMessage)
        {
            try
            {
                MailSetup mailSetup = new MailSetup
                {
                    Server           = "smtp.gmail.com",
                    Port             = 587,
                    EnableSSL        = true,
                    FromMail         = "*****@*****.**",
                    FromMailPassword = "******"
                };


                MailMessage message = new MailMessage();
                message.Subject    = subject;
                message.Body       = htmlMessage;
                message.IsBodyHtml = true;
                message.To.Add(email);

                message.From = new MailAddress(mailSetup.FromMail);

                using (var smtpClient = new SmtpClient())
                {
                    smtpClient.Host = mailSetup.Server;
                    smtpClient.Port = mailSetup.Port;
                    smtpClient.UseDefaultCredentials = true;
                    smtpClient.Credentials           = new NetworkCredential(mailSetup.FromMail, mailSetup.FromMailPassword);
                    smtpClient.EnableSsl             = mailSetup.EnableSSL;
                    smtpClient.DeliveryMethod        = SmtpDeliveryMethod.Network;

                    await smtpClient.SendMailAsync(message);
                }
            }
            catch (SmtpException smtp)
            {
                //throw new SmtpException(smtp.Message);
            }
            catch (Exception ex)
            {
                // throw new ArgumentException(ex.Message);
            }
        }
Exemplo n.º 3
0
        public Publipostages(string dbUrl, MailSetup mailSetup) : base(dbUrl)
        {
            this.mailSetup = mailSetup;

            // API only available to authenticated users
            BeforeAsync = async(p, c) => await c.EnsureIsAuthenticatedAsync();

            GetAsync["/{id:int}/pdf"] = async(p, c) =>
            {
                var publi = new Publipostage {
                    id = (int)p["id"]
                };
                using (DB db = await DB.CreateAsync(dbUrl))
                {
                    if (await publi.LoadAsync(db, true))
                    {
                        // only the admin of the publipostage have right to see the PDF
                        await publi.EnsureRightAsync(c, Right.Create, null);

                        c.Response.StatusCode          = 302;
                        c.Response.Headers["location"] = "pdf/" + HttpUtility.UrlEncode(publi.descriptif) + ".pdf?" +
                                                         HttpUtility.QueryStringToString(c.Request.QueryString);
                    }
                }
            };

            GetAsync["/{id:int}/pdf/{filename}"] = async(p, c) =>
            {
                var pageSize = PageSize.A4;
                if (c.Request.QueryString.ContainsKey("pageSize"))
                {
                    Enum.TryParse(c.Request.QueryString["pageSize"], out pageSize);
                }

                var html  = new StringBuilder();
                var publi = new Publipostage {
                    id = (int)p["id"]
                };
                using (DB db = await DB.CreateAsync(dbUrl))
                {
                    if (await publi.LoadAsync(db, true))
                    {
                        // only the admin of the publipostage have right to see the PDF
                        await publi.EnsureRightAsync(c, Right.Create, null);

                        html.Append("<!DOCTYPE html>\n<html>\n<head>\n");
                        html.Append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n");
                        html.Append("<style>* { font-family: sans-serif; }</style>");
                        html.Append("</head>\n");

                        var recipients = await GetRecipientsMessagesAsync(db, publi);

                        bool first = true;
                        foreach (var recipient in recipients)
                        {
                            if (first)
                            {
                                html.Append("<div>");
                            }
                            else
                            {
                                html.Append("<div style=\"page-break-before: always;\">");
                            }
                            html.Append(recipient.message);
                            html.Append("</div>\n");
                            first = false;
                        }
                        html.Append("</html>\n");

                        c.Response.StatusCode = 200;
                        c.Response.Headers["content-type"] = "application/pdf";
                        c.Response.Content = HtmlToPdf(html.ToString(), pageSize);
                    }
                }
            };

            PostAsync["/preview"] = async(p, c) =>
            {
                var publi = Model.CreateFromJson <Publipostage>(await c.Request.ReadAsJsonAsync());
                using (DB db = await DB.CreateAsync(dbUrl))
                {
                    await publi.EnsureRightAsync(c, Right.Read, null as Publipostage);

                    var recipientsMessages = await GetRecipientsMessagesAsync(db, publi);

                    foreach (var recipient in recipientsMessages)
                    {
                        if (recipient.user != null)
                        {
                            await recipient.user.EnsureRightAsync(c, Right.Read, null as User);
                        }
                        if (recipient.group != null)
                        {
                            await recipient.group.EnsureRightAsync(c, Right.Read, null as User);
                        }
                        if (recipient.child != null)
                        {
                            await recipient.child.EnsureRightAsync(c, Right.Read, null as User);
                        }
                    }
                    c.Response.StatusCode = 200;
                    c.Response.Content    = recipientsMessages;
                }
            };
        }
Exemplo n.º 4
0
 public CustomList <MailSetup> GetAllHOD()
 {
     return(MailSetup.GetAllHOD());
 }
Exemplo n.º 5
0
 public CustomList <MailSetup> GetAllSupervisor()
 {
     return(MailSetup.GetAllSupervisor());
 }
Exemplo n.º 6
0
 public CustomList <MailSetup> GetAllMailSetup(Int32 reportID)
 {
     return(MailSetup.GetAllMailSetup(reportID));
 }
Exemplo n.º 7
0
        protected void btnEmail_Click(object sender, EventArgs e)
        {
            try
            {
                string    checkedRequiredField = "";
                DataTable dt           = new DataTable();
                string    subject      = "";
                string    fileName     = "";
                string    body         = "";
                Boolean   isSubjectYM  = false;
                Boolean   isFileNameYM = false;
                errorList = new CustomList <ErrorList>();
                MailSetupManager       manager   = new MailSetupManager();
                CustomList <MailSetup> mailSetup = manager.GetAllMailSetup(Convert.ToInt32(Session["ReportID"]));
                MailSetup mS = mailSetup.Find(f => f.Subject.IsNotNullOrEmpty());
                if (mS.IsNull())
                {
                    return;
                }
                else
                {
                    subject      = mS.Subject;
                    fileName     = mS.FileName;
                    body         = mS.Body;
                    isSubjectYM  = mS.IsSubjectYM;
                    isFileNameYM = mS.IsFileNameYM;
                }
                #region
                if (isSubjectYM)
                {
                    FilterSets month         = FilterSetList.Find(f => f.ColumnName == "MonthNo");
                    FilterSets year          = FilterSetList.Find(f => f.ColumnName == "YearNo");
                    string     monthYearName = "";
                    if (month.IsNotNull())
                    {
                        monthYearName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month.ColumnActualValue.ToInt()).Substring(0, 3) + "' " + year.ColumnActualValue.Substring(2, 2);
                    }

                    if (monthYearName == "")
                    {
                        FilterSets fromDate = FilterSetList.Find(f => f.ColumnName == "FromDate");
                        if (fromDate.IsNotNull())
                        {
                            monthYearName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(fromDate.ColumnValue.Substring(0, 2).ToInt()).Substring(0, 3) + "' " + fromDate.ColumnValue.Substring(8, 2);
                        }
                    }
                    if (monthYearName == "")
                    {
                        FilterSets workDate = FilterSetList.Find(f => f.ColumnName == "Workdate");
                        if (workDate.IsNotNull())
                        {
                            monthYearName = Convert.ToDateTime(workDate.ColumnValue).ToString("dd MMM yy");
                        }
                    }
                    subject = subject + "_" + monthYearName;
                }
                if (isFileNameYM)
                {
                    FilterSets month         = FilterSetList.Find(f => f.ColumnName == "MonthNo");
                    FilterSets year          = FilterSetList.Find(f => f.ColumnName == "YearNo");
                    string     monthYearName = "";
                    if (month.IsNotNull())
                    {
                        monthYearName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month.ColumnActualValue.ToInt()).Substring(0, 3) + "' " + year.ColumnActualValue.Substring(2, 2);
                    }

                    if (monthYearName == "")
                    {
                        FilterSets fromDate = FilterSetList.Find(f => f.ColumnName == "FromDate");
                        if (fromDate.IsNotNull())
                        {
                            monthYearName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(fromDate.ColumnValue.Substring(0, 2).ToInt()).Substring(0, 3) + "' " + fromDate.ColumnValue.Substring(8, 2);
                        }
                    }
                    if (monthYearName == "")
                    {
                        FilterSets workDate = FilterSetList.Find(f => f.ColumnName == "Workdate");
                        if (workDate.IsNotNull())
                        {
                            monthYearName = Convert.ToDateTime(workDate.ColumnValue).ToString("dd MMM yy");
                        }
                    }
                    fileName = fileName + "_" + monthYearName;
                }
                string    reportPath = Session["reportPath"].ToString();
                DataTable dt1        = new DataTable();
                if (!mS.IsIndividual)
                {
                    Report.LoadSourceDataSet(ref checkedRequiredField, ref dt1);
                }
                FilterSets empCode = FilterSetList.Find(f => f.ColumnName == "EmpCode");
                if (empCode.ColumnActualValue.IsNotNullOrEmpty())
                {
                    mailSetup = mailSetup.FindAll(f => f.EmpKey.ToString() == empCode.ColumnActualValue);
                }
                #endregion
                foreach (MailSetup m in mailSetup)
                {
                    //if (mS.IsIndividual)
                    //    Report.LoadSourceDataSet(m.EmpKey.ToString(), ref dt1);

                    //SentEmail sm = new SentEmail();
                    //string message = sm.EmailUtil(m.EmailAddress, reportPath, dt1, "", subject, fileName, body);
                    //if (message.IsNotNullOrEmpty())
                    //{
                    //    ErrorList eL = new ErrorList();
                    //    eL.EmpName = m.EmpName.Trim();
                    //    eL.Error = message;
                    //    errorList.Add(eL);
                    //}
                }
                ((PageBase)this.Page).SuccessMessage = "Mail sent successfully!";
            }
            catch (Exception ex)
            {
                ((PageBase)this.Page).ErrorMessage = (ExceptionHelper.getExceptionMessage(ex));
            }
        }