Exemple #1
0
        public void AddEmailQueue(EmailQueueDto email)
        {
            if (email != null)
            {
                EmailQueue model = new EmailQueue()
                {
                    ToUserID    = email.ToUserID,
                    Subject     = email.Subject,
                    Email       = email.Email,
                    Recipient   = email.Recipient,
                    CC          = email.CC,
                    Bcc         = email.Bcc,
                    IsSent      = email.IsSent,
                    CreatedDate = email.CreatedDate,
                    SentDate    = email.SentDate
                };

                EmailQueueRepoitory.Add(model);
                EmailQueueRepoitory.Save();
            }
        }
Exemple #2
0
        public void SendEmail(string[] to, string[] cc, string[] bcc, int emailTemplateID, Hashtable param, string userID)
        {
            try
            {
                EmailTemplate emailTemplate = _serviceEmailTemplate.GetById(emailTemplateID);
                param.Add("%TemplateName", emailTemplate.Description);

                string subject   = emailTemplate.Subject;
                string emailBody = emailTemplate.Body;

                //replace the tags with actual values
                if (param != null)
                {
                    foreach (DictionaryEntry p in param)
                    {
                        if (emailTemplate.Subject.Contains(p.Key.ToString()))
                        {
                            subject = subject.Replace(p.Key.ToString(), p.Value.ToString());
                        }

                        if (emailTemplate.Body.Contains(p.Key.ToString()))
                        {
                            emailBody = emailBody.Replace(p.Key.ToString(), p.Value.ToString());
                        }
                    }
                }
                else
                {
                    subject   = emailTemplate.Subject;
                    emailBody = emailTemplate.Body;
                }

                //save the email in the queue table to be sent later on
                if (!string.IsNullOrEmpty(subject) && !string.IsNullOrEmpty(emailBody))
                {
                    EmailQueue emailQueue = new EmailQueue();

                    if (to != null)
                    {
                        if (to.Length > 0)
                        {
                            foreach (string toAddress in to)
                            {
                                emailQueue.Recipient = emailQueue.Recipient + toAddress + ",";
                            }

                            //remove the last comma
                            emailQueue.Recipient = emailQueue.Recipient.Remove(emailQueue.Recipient.LastIndexOf(","));
                        }
                    }

                    if (cc != null)
                    {
                        if (cc.Length > 0)
                        {
                            foreach (string ccAddress in cc)
                            {
                                emailQueue.CC = ccAddress + ",";
                            }

                            //remove the last comma
                            emailQueue.CC = emailQueue.CC.Remove(emailQueue.CC.LastIndexOf(","));
                        }
                    }
                    else
                    {
                        emailQueue.CC = "";
                    }

                    if (bcc != null)
                    {
                        if (bcc.Length > 0)
                        {
                            foreach (string bccAddress in cc)
                            {
                                emailQueue.Bcc = bccAddress + ",";
                            }

                            //remove the last comma
                            emailQueue.Bcc = emailQueue.Bcc.Remove(emailQueue.Bcc.LastIndexOf(","));
                        }
                    }
                    else
                    {
                        emailQueue.Bcc = "";
                    }

                    DateTime currentDateTime = DateTime.Now;
                    emailQueue.ToUserID = userID;
                    emailQueue.Subject  = subject;
                    emailQueue.Email    = emailBody;
                    emailQueue.IsSent   = false;

                    emailQueue.CreatedDate = currentDateTime;
                    emailQueue.SentDate    = currentDateTime;
                    try
                    {
                        if (!emailQueue.IsSent)
                        {
                            SendEmail(emailQueue.Recipient, emailQueue.CC.ToString(), emailQueue.Bcc, emailQueue.Subject, emailQueue.Email);
                            emailQueue.IsSent   = true;
                            emailQueue.SentDate = DateTime.Now;
                            _serviceEmailQueue.Add(emailQueue);
                            _serviceEmailQueue.Save();
                        }
                    }
                    catch (Exception ex)
                    {
                        ApplicationLog log = new ApplicationLog();
                        log.CreatedDate = DateTime.UtcNow;
                        log.Source      = ex.Source;
                        log.Type        = "Error in Sending Email.";
                        log.Description = ex.Message;
                        _serviceAppLog.Add(log);
                        _serviceAppLog.Save();
                    }
                }
            }
            catch (Exception ex)
            {
                ApplicationLog log = new ApplicationLog();
                log.CreatedDate = DateTime.UtcNow;
                log.Source      = ex.Source;
                log.Type        = "Error in Sending Email.";
                log.Description = ex.Message;
                _serviceAppLog.Add(log);
                _serviceAppLog.Save();
            }
        }