コード例 #1
0
        /// <summary>
        /// Send the email directly
        /// </summary>
        /// <param name="emailAccount"></param>
        /// <param name="emailModel"></param>
        /// <returns></returns>
        public ResponseModel SendEmailDirectly(EmailAccount emailAccount, EmailModel emailModel)
        {
            var mailSetting = new EmailSetting
            {
                Host      = emailAccount.Host,
                Port      = emailAccount.Port,
                Timeout   = emailAccount.TimeOut,
                EnableSsl = emailAccount.EnableSsl,
                User      = emailAccount.Username,
                Password  = emailAccount.Password
            };

            var mailUtilities = new MailUtilities(mailSetting);

            try
            {
                mailUtilities.SendEmail(emailModel);
                return(new ResponseModel
                {
                    Success = true,
                    Message = T("EmailAccount_Message_SendEmailDirectlySuccessfully")
                });
            }
            catch (Exception exception)
            {
                return(new ResponseModel
                {
                    Success = false,
                    Message = T("EmailAccount_Message_SendEmailDirectlyFailure"),
                    DetailMessage = exception.Message
                });
            }
        }
コード例 #2
0
        public IHttpActionResult PostRelicense([FromBody] RelicenseRequest request)
        {
            logger.Debug("PostRelicense(ReliscenseRequest) entered ...");

            try
            {
                // Check if already listed as a device.
                MobileDevice device = db.MobileDevices.Include("Company").FirstOrDefault(d => d.SerialNo == request.SerialNo);

                if (device == null)
                {
                    logger.InfoFormat("Device with Serial # : {0} not registered", request.SerialNo);

                    return(Ok(new LicenseResponse()
                    {
                        Error = "Device not registered",
                        Result = (int)RelicenceResultCode.NotRegistered
                    }));
                }

#if ENABLE_EMAIL
                // Send email ...
                MailUtilities.SendEmail(db, device, "relicensed");
#endif

                // Return configuration.
                return(Ok(new LicenseResponse()
                {
                    Result = (int)RelicenceResultCode.Success,
                    DeviceNo = device.MobileDeviceId,
                    Url = device.Company.ColossusMobileUrl,
                    ConsignorName = device.Company.ConsignorName,
                    ConsignorAdd1 = device.Company.ConsignorAdd1,
                    ConsignorAdd2 = device.Company.ConsignorAdd2,
                    ConsignorAdd3 = device.Company.ConsignorAdd3,
                    Error = string.Empty
                }));
            }
            catch (Exception ex)
            {
                logger.Error("Error relicensing device", ex);

                return(Ok(new LicenseResponse()
                {
                    Result = (int)RelicenceResultCode.Unknown,
                    Error = "Unknown Error",
                    Url = string.Empty
                }));
            }
            finally
            {
                logger.Debug("PostRelicense(ReliscenseRequest) exited");
            }
        }
コード例 #3
0
        public IHttpActionResult Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var account = Service.CreateCredentials(model.UserName, model.Password, model.Email, model.LegionId,
                                                    model.FirstName, model.LastName);
            var verificationCode = Service.CreateVerificationCode(account);

            MailUtilities.SendEmail(model.Email, verificationCode, GetVerificationPath());
            return(Ok("Verification Email Sent"));
        }
コード例 #4
0
        public IHttpActionResult PostLicense([FromBody] LicenseRequest request)
        {
            logger.Debug("PostLicense(LicenseRequest) entered ...");

            try
            {
                // Validate Company PIN format, if invalid return an error.
                if (request.CompanyPIN.Length != 8)
                {
                    logger.InfoFormat("PIN is incorrect length : {0}, should be 8 characters", request.CompanyPIN.Length);

                    return(Ok(new LicenseResponse()
                    {
                        Error = "PIN must be 8 digits",
                        Result = (int)LicenceResultCode.PinIncorrectLength
                    }));
                }

                // Attempt to find Company from the PIN.
                Company company = db.Companies.FirstOrDefault(c => c.Pin == request.CompanyPIN);

                // If not found return an error.
                if (company == null)
                {
                    logger.InfoFormat("Failed to find Company with PIN : {0}", request.CompanyPIN);

                    return(Ok(new LicenseResponse()
                    {
                        Error = "PIN is invalid",
                        Result = (int)LicenceResultCode.PinNotFound
                    }));
                }

                // Check if already listed as a device.
                MobileDevice device = db.MobileDevices.FirstOrDefault(d => d.SerialNo == request.SerialNo);

                // If the device exists return error showing already registered
                if (device != null)
                {
                    logger.InfoFormat("Device with Serial # : {0} already registered", request.SerialNo);

                    return(Ok(new LicenseResponse()
                    {
                        Error = "Device already registered",
                        Result = (int)LicenceResultCode.DeviceAlreadyRegistered
                    }));
                }

                // Create new device.
                device = new MobileDevice()
                {
                    Company   = company,
                    SerialNo  = request.SerialNo,
                    Created   = DateTime.Now,
                    CompanyId = company.CompanyId,
                };

                // Add to the collection of Mobile Devices ...
                device = db.MobileDevices.Add(device);
                db.SaveChanges();

                logger.DebugFormat("New device with Id : {0} created", device.MobileDeviceId);

                // Send email ...
                int numberOfDevices = db.MobileDevices.Count(d => d.CompanyId == device.MobileDeviceId);
                MailUtilities.SendEmail(numberOfDevices, device, "licensed");

                // Return configuration.
                return(Ok(new LicenseResponse()
                {
                    Result = (int)LicenceResultCode.Success,
                    DeviceNo = device.MobileDeviceId,
                    Url = device.Company.ColossusMobileUrl,
                    ConsignorName = device.Company.ConsignorName,
                    ConsignorAdd1 = device.Company.ConsignorAdd1,
                    ConsignorAdd2 = device.Company.ConsignorAdd2,
                    ConsignorAdd3 = device.Company.ConsignorAdd3,
                    Error = string.Empty
                }));
            }
            catch (Exception ex)
            {
                logger.Error("Error licensing device", ex);

                return(Ok(new LicenseResponse()
                {
                    Result = (int)LicenceResultCode.Unknown,
                    Error = "Unknown Error",
                    Url = string.Empty
                }));
            }
            finally
            {
                logger.Debug("PostLicense(LicenseRequest) exited");
            }
        }
コード例 #5
0
ファイル: EmailTask.cs プロジェクト: levanvunam/EasyCMS
        public void Run(BackgroundTaskExecuteContext context)
        {
            var logger = HostContainer.GetInstance <ILogger>();

            if (Interlocked.CompareExchange(ref _hasActiveTask, 1, 0) == 0)
            {
                var countEmails = 0;
                try
                {
                    // Update the background task last running time
                    var backgroundTaskService = HostContainer.GetInstance <IEzBackgroundTaskService>();
                    backgroundTaskService.UpdateLastRunningTimeTask(GetType());

                    var emailLogService     = HostContainer.GetInstance <IEmailLogService>();
                    var emailAccountService = HostContainer.GetInstance <IEmailAccountService>();
                    var emailAccounts       = emailAccountService.GetAll().ToList();
                    var emailLogs           = emailLogService.GetEmailSendingQueues();
                    countEmails = emailLogs.Count();
                    foreach (var emailLog in emailLogs)
                    {
                        var emailAccount = emailAccounts.FirstOrDefault(e => e.Id == emailLog.EmailAccountId);
                        if (emailAccount != null)
                        {
                            var emailSetting = new EmailSetting
                            {
                                Host      = emailAccount.Host,
                                Port      = emailAccount.Port,
                                User      = emailAccount.Username,
                                Password  = emailAccount.Password,
                                EnableSsl = emailAccount.EnableSsl,
                                Timeout   = emailAccount.TimeOut
                            };
                            var mailUtilities = new MailUtilities(emailSetting);
                            var logs          = new List <EmailSendingLog>();
                            try
                            {
                                if (!string.IsNullOrEmpty(emailLog.Message))
                                {
                                    logs = SerializeUtilities.Deserialize <List <EmailSendingLog> >(emailLog.Message);
                                }

                                mailUtilities.SendEmail(emailLog.From ?? emailAccount.Email, emailLog.FromName ?? emailAccount.DisplayName, emailLog.To, emailLog.CC, emailLog.Bcc,
                                                        true, emailLog.Subject, emailLog.Body);
                                emailLog.SentOn = DateTime.UtcNow;

                                logs.Add(new EmailSendingLog
                                {
                                    Time    = DateTime.Now,
                                    Message = string.Format("Mail sent at {0} UTC Time", DateTime.UtcNow)
                                });
                            }
                            catch (Exception exception)
                            {
                                logs.Add(new EmailSendingLog
                                {
                                    Time    = DateTime.Now,
                                    Message = string.Format("Error: {0}", exception.Message)
                                });
                            }

                            emailLog.SentTries++;
                            emailLog.Message = SerializeUtilities.Serialize(logs);
                            emailLogService.UpdateMail(emailLog);
                        }
                    }
                }
                catch (Exception exception)
                {
                    logger.Error(string.Format("[{0}]", EzCMSContants.EmailTaskName), exception);
                }
                if (countEmails > 0)
                {
                    logger.Info(string.Format("[{0}] Send {1} email(s) in queues", EzCMSContants.EmailTaskName, countEmails));
                }
                Interlocked.Exchange(ref _hasActiveTask, 0);
            }
        }