Exemplo n.º 1
0
        /// <summary>
        /// Sends the email.
        /// </summary>
        /// <param name="emailContent">Content of the email.</param>
        public void SendEmail(EmailContentModel emailContent)
        {
            MailMessage mail = new MailMessage();

            if (!string.IsNullOrEmpty(emailContent.ToEmails) || !string.IsNullOrEmpty(emailContent.BCCEmails))
            {
                mail.From       = new MailAddress(emailContent.FromEmail);
                mail.IsBodyHtml = true;
                mail.Subject    = emailContent.Subject;
                mail.Body       = emailContent.Body;

                string[] toEmails = emailContent.ToEmails.Split(';');
                foreach (string multimailid in toEmails)
                {
                    mail.To.Add(new MailAddress(multimailid));
                }

                if (!string.IsNullOrEmpty(emailContent.BCCEmails))
                {
                    foreach (string multimailid1 in toEmails)
                    {
                        mail.Bcc.Add(new MailAddress(multimailid1));
                    }
                }

                SmtpClient smtp = new SmtpClient();
                smtp.Host = emailContent.SMTPHost;
                smtp.Port = emailContent.Port;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials           = new NetworkCredential(
                    emailContent.UserName, emailContent.Password);
                smtp.EnableSsl = emailContent.SSL;
                smtp.Send(mail);
            }
        }
Exemplo n.º 2
0
        public ActionResult ExtractEmailContent(EmailContentModel model)
        {
            if (ModelState.IsValid)
            {
                using (var client = new HttpClient())
                {
                    if (Request.Url != null)
                    {
                        client.BaseAddress = new Uri(Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, string.Empty));
                        var content  = new StringContent(model.EmailContent, Encoding.UTF8, "application/json");
                        var response = client.PostAsync("/api/values/GetXmlLikePortions", content).Result;
                        if (response.IsSuccessStatusCode)
                        {
                            model.XmlLikeContent = response.Content.ReadAsStringAsync().Result;
                        }
                        else
                        {
                            var errorContent = response.Content.ReadAsAsync <HttpError>();
                            ModelState.AddModelError(response.ReasonPhrase, $"{response.ReasonPhrase} {(string)errorContent.Result["Message"]}");
                        }
                    }
                }
            }

            return(View("Index", model));
        }
Exemplo n.º 3
0
        /// <summary>
        /// add email to database
        /// </summary>
        /// <returns></returns>
        public static int AddEmailToDbByEmailModel(int taskId, EmailModel emailModel)
        {
            try
            {
                using (var db = new FCCEmailAgentEntities())
                {
                    EmailContentModel content   = new EmailContentModel(emailModel.EmailContentLine);
                    var attachmentsContentModel = content.Elements.Find(x => x.Name == "#attachmentslist#");

                    Emails email = new Emails();
                    email.Subject        = emailModel.Subject;
                    email.FromEmail      = emailModel.FromEmail;
                    email.ReceiverUserId = UsersHelper.CheckExistsUserByEmail(emailModel.ToEmails);
                    email.StateId        = 1;
                    email.TaskId         = taskId;
                    email.CcResponseTo   = string.IsNullOrWhiteSpace(emailModel.CcResponseTo)?"": emailModel.CcResponseTo;
                    email.TypeId         = emailModel.TypeId;
                    email.Host           = Program.Agent.ServiceSettings.SMTPSettings.Server;
                    email.Port           = Program.Agent.ServiceSettings.SMTPSettings.Port;
                    email.Body           = EmailGeneratorHelper.GenerateEmailBody(emailModel.TypeId, content.Elements);

                    if (attachmentsContentModel != null)
                    {
                        email.Attachments = EmailGeneratorHelper.GenerateStringAttachments(attachmentsContentModel.Value);
                    }
                    else
                    {
                        email.Attachments = EmailGeneratorHelper.GenerateStringAttachments("");
                    }

                    db.Emails.Add(email);
                    db.SaveChanges();

                    EmailTransactionsHelper.AddEmailTransaction(email.Id);
                    return(email.Id);
                }
            }
            catch (Exception exception)
            {
                string innerException = exception.InnerException == null ? "" : exception.InnerException.Message;
                string methodName     = System.Reflection.MethodBase.GetCurrentMethod().Name;
                LogHelper.AddLog("Error in method: " + methodName + "; Exception: " + exception.Message + " Innner Exception: " +
                                 innerException);
                return(-1);
            }
        }
        /// <summary>
        /// Prepares the content of the email.
        /// </summary>
        /// <param name="subscriptionID">The subscription identifier.</param>
        /// <param name="planGuId">The plan gu identifier.</param>
        /// <param name="processStatus">The process status.</param>
        /// <param name="planEventName">Name of the plan event.</param>
        /// <param name="subscriptionStatus">The subscription status.</param>
        /// <returns>
        /// Email Content Model.
        /// </returns>
        /// <exception cref="Exception">Error while sending an email, please check the configuration.
        /// or
        /// Error while sending an email, please check the configuration.</exception>
        public EmailContentModel PrepareEmailContent(Guid subscriptionID, Guid planGuId, string processStatus, string planEventName, string subscriptionStatus)
        {
            EmailContentModel emailContent = new EmailContentModel();
            string            body         = this.emailTemplateRepository.GetEmailBodyForSubscription(subscriptionID, processStatus);
            var subscriptionEvent          = this.eventsRepository.GetByName(planEventName);
            var emailTemplateData          = this.emailTemplateRepository.GetTemplateForStatus(subscriptionStatus);

            if (processStatus == "failure")
            {
                emailTemplateData = this.emailTemplateRepository.GetTemplateForStatus("Failed");
            }

            string subject = string.Empty;

            bool   copyToCustomer = false;
            bool   isActive       = false;
            string toReceipents   = string.Empty;
            string ccReceipents   = string.Empty;
            string bccReceipents  = string.Empty;

            string fromMail = this.applicationConfigRepository.GetValueByName("SMTPFromEmail");
            string password = this.applicationConfigRepository.GetValueByName("SMTPPassword");
            string username = this.applicationConfigRepository.GetValueByName("SMTPUserName");
            bool   smtpSsl  = bool.Parse(this.applicationConfigRepository.GetValueByName("SMTPSslEnabled"));
            int    port     = int.Parse(this.applicationConfigRepository.GetValueByName("SMTPPort"));
            string smtpHost = this.applicationConfigRepository.GetValueByName("SMTPHost");

            var eventData = this.planEventsMappingRepository.GetPlanEvent(planGuId, subscriptionEvent.EventsId);

            if (eventData != null)
            {
                toReceipents   = eventData.SuccessStateEmails;
                copyToCustomer = Convert.ToBoolean(eventData.CopyToCustomer);
            }

            if (string.IsNullOrEmpty(toReceipents))
            {
                throw new Exception(" Error while sending an email, please check the configuration. ");
            }

            if (emailTemplateData != null)
            {
                if (!string.IsNullOrEmpty(toReceipents) && !string.IsNullOrEmpty(emailTemplateData.Cc))
                {
                    ccReceipents = emailTemplateData.Cc;
                }

                if (!string.IsNullOrEmpty(emailTemplateData.Bcc))
                {
                    bccReceipents = emailTemplateData.Bcc;
                }

                subject = emailTemplateData.Subject;
            }

            emailContent.BCCEmails      = bccReceipents;
            emailContent.CCEmails       = ccReceipents;
            emailContent.ToEmails       = toReceipents;
            emailContent.Body           = body;
            emailContent.Subject        = subject;
            emailContent.CopyToCustomer = copyToCustomer;
            emailContent.IsActive       = isActive;
            emailContent.FromEmail      = fromMail;
            emailContent.Password       = password;
            emailContent.SSL            = smtpSsl;
            emailContent.UserName       = username;
            emailContent.Port           = port;
            emailContent.SMTPHost       = smtpHost;

            return(emailContent);
        }