Ejemplo n.º 1
0
 public static void ShowEdit(EMailViewModel emailviewmodel)
 {
     _emailViewModel = new EMailViewModel();
     _isCreate       = false;
     _emailViewModel = emailviewmodel;
     Show();
 }
Ejemplo n.º 2
0
 private void FillDataFromEmail(EMailViewModel emailviewmodel)
 {
     txtEmailName.Text         = emailviewmodel.EmailName;
     txtEmailFrom.Text         = emailviewmodel.EmailFrom;
     txtEmailPassword.Password = emailviewmodel.EmailPassword;
     txtEmailTo1.Text          = emailviewmodel.EmailTo1;
     txtEmailTo2.Text          = emailviewmodel.EmailTo2;
     txtEmailTo3.Text          = emailviewmodel.EmailTo3;
     txtEmailTo4.Text          = emailviewmodel.EmailTo4;
     txtEmailTo5.Text          = emailviewmodel.EmailTo5;
     txtSMTPAddress.Text       = emailviewmodel.SMTPAddress;
     txtSMTPPort.Text          = emailviewmodel.SMTPPort.ToString();
 }
Ejemplo n.º 3
0
        internal static List <EMailViewModel> LoadEmailConfiguration()
        {
            List <EMailViewModel> emaillist = new List <EMailViewModel>();
            DataTable             dt        = new DataTable("Reports");

            try
            {
                string CmdString = string.Empty;
                using (MySqlConnection con = new MySqlConnection(connectionString))
                {
                    CmdString = "select mailconfig.id, mailconfig.EmailName, mailconfig.EmailFrom, mailconfig.EmailPassword,mailconfig.SMTPAddress,mailconfig.SMTPPort, mailconfig.EnableSSL,mailconfig.EmailTo1, mailconfig.EmailTo2, mailconfig.EmailTo3, mailconfig.EmailTo4, mailconfig.EmailTo5, mailconfig.EntryDate,emltmplt.EmailSubject,emltmplt.EmailBody from tblemailconfig  mailconfig join tblemailtemplet emltmplt on emltmplt.id = mailconfig.EmailTemplateId";
                    MySqlCommand     cmd = new MySqlCommand(CmdString, con);
                    MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
                    sda.Fill(dt);
                }
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        EMailViewModel emailvm = new EMailViewModel();
                        emailvm.Id            = Convert.ToInt16(dt.Rows[i]["Id"]);
                        emailvm.EmailFrom     = dt.Rows[i]["EmailFrom"].ToString();
                        emailvm.EmailName     = dt.Rows[i]["EmailName"].ToString();
                        emailvm.EmailPassword = dt.Rows[i]["EmailPassword"].ToString();
                        emailvm.SMTPAddress   = dt.Rows[i]["SMTPAddress"].ToString();
                        emailvm.SMTPPort      = Convert.ToInt16(dt.Rows[i]["SMTPPort"]);
                        emailvm.EnableSSL     = Convert.ToBoolean(dt.Rows[i]["SMTPPort"]);
                        emailvm.EntryDate     = Convert.ToDateTime(dt.Rows[i]["EntryDate"].ToString());
                        emailvm.EmailTo1      = dt.Rows[i]["EmailTo1"].ToString();
                        emailvm.EmailTo2      = dt.Rows[i]["EmailTo2"].ToString();
                        emailvm.EmailTo3      = dt.Rows[i]["EmailTo3"].ToString();
                        emailvm.EmailTo4      = dt.Rows[i]["EmailTo4"].ToString();
                        emailvm.EmailTo5      = dt.Rows[i]["EmailTo5"].ToString();
                        emailvm.EmailSubject  = dt.Rows[i]["EmailSubject"].ToString();
                        emailvm.EmailBody     = dt.Rows[i]["EmailBody"].ToString();
                        emaillist.Add(emailvm);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogError("Exception in LoadEmailConfiguration method, Message : " + ex.Message);
                return(null);
            }
            return(emaillist);
        }
Ejemplo n.º 4
0
        public ActionResult SendMail(EMailViewModel model)
        {
            //try
            //{
            //var message = new System.Net.Mail.MailMessage();
            //message.To.Add(new MailAddress("*****@*****.**"));
            //message.Subject =  "Khách hàng '" + model.UserName + "' gửi email";
            //message.Body = "Email của '"+ model.UserName +"': "+ model.UserEmail +" " + "<br/>" +
            //    "Nội dung: " +model.EmailContent;
            //message.IsBodyHtml = true;
            //using (var smtp = new SmtpClient())
            //{
            //    smtp.Send(message);
            //}
            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress("*****@*****.**");
                mail.To.Add(new MailAddress("*****@*****.**"));
                mail.Subject    = "Khách hàng '" + model.UserName + "' gửi email";
                mail.Body       = "Email của '" + model.UserName + "': " + model.UserEmail + " " + "<br/>" + "Nội dung: " + model.EmailContent;
                mail.IsBodyHtml = true;

                using (SmtpClient smtp = new SmtpClient())
                {
                    smtp.Host        = "smtp.gmail.com";
                    smtp.Port        = 587;
                    smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "A@123456A@");
                    smtp.EnableSsl   = true;
                    smtp.Send(mail);
                }
            }
            return(Json(new { success = true }));
            //}
            //catch
            //{
            //    return Json(new { success = false });
            //}
        }
Ejemplo n.º 5
0
        private void btnAddEmailConfig_Click(object sender, RoutedEventArgs e)
        {
            ErrorMessage errmsg = null;

            if (EmailToValidation())
            {
                EMailViewModel emailviewmodel = new EMailViewModel();
                emailviewmodel.Id            = 0;
                emailviewmodel.EmailName     = txtEmailName.Text;
                emailviewmodel.EmailFrom     = txtEmailFrom.Text;
                emailviewmodel.EmailPassword = txtEmailPassword.Password;
                emailviewmodel.SMTPAddress   = txtSMTPAddress.Text;
                emailviewmodel.SMTPPort      = Convert.ToInt16(txtSMTPPort.Text);
                emailviewmodel.EnableSSL     = cbEnableSsl.IsChecked.Value;
                emailviewmodel.EmailTo1      = txtEmailTo1.Text;
                emailviewmodel.EmailTo2      = txtEmailTo2.Text;
                emailviewmodel.EmailTo3      = txtEmailTo3.Text;
                emailviewmodel.EmailTo4      = txtEmailTo4.Text;
                emailviewmodel.EmailTo5      = txtEmailTo5.Text;
                emailviewmodel.EntryDate     = DateTime.Now;
                if (_isCreate)
                {
                    errmsg = Dalc.ManageEmailConfig(emailviewmodel, CommandTypeEnum.Insert);
                }
                else
                {
                    errmsg = Dalc.ManageEmailConfig(emailviewmodel, CommandTypeEnum.Update);
                }
                if (errmsg.ErrorId == 1 || errmsg.ErrorId == 2)
                {
                    _isSuccess = true;
                }
                MessageBox.Show(errmsg.ErrorDescription);
                this.Close();
            }
        }
        private async Task WorkerAsync()
        {
            _timer.Stop();

            string fromMail = "";

            try
            {
                _emails = EmailService.GetAll().Where(x => !x.IsSent).OrderBy(x => x.Id).ToList();

                if (_emails.Count == 0)
                {
                    _timer.Start(); return;
                }

                foreach (var email in _emails)
                {
                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(_apiUrl);

                        using (var stream = new MemoryStream())
                            using (var bson = new BsonWriter(stream))
                            {
                                var jsonSerializer = new JsonSerializer();

                                fromMail = email.From;

                                _emailAttachments = EmailAttachmentService.GetAllNoTracking().Where(x => x.EmailId == email.Id).ToList();

                                var attachments = _emailAttachments.Select(attachment => new EMailAttachmentViewModel
                                {
                                    File     = attachment.File,
                                    FileName = attachment.FileName,
                                    EmailId  = attachment.EmailId
                                }).ToList();

                                if (email.Retry >= _retryLimitation)
                                {
                                    continue;
                                }
                                var willBeSentAgainEmail = new EMailViewModel
                                {
                                    Id               = email.Id,
                                    From             = email.From,
                                    To               = email.To,
                                    Subject          = email.Subject,
                                    Body             = email.Body,
                                    Bcc              = email.Bcc,
                                    Cc               = email.Cc,
                                    Exception        = email.Exception,
                                    Retry            = ++email.Retry,
                                    SmtpServer       = email.SmtpServer,
                                    IsRead           = false,
                                    IsSent           = false,
                                    LastTryDate      = DateTime.Now,
                                    EmailAttachments = attachments
                                };

                                jsonSerializer.Serialize(bson, willBeSentAgainEmail);

                                client.DefaultRequestHeaders.Accept.Clear();
                                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/bson"));

                                var byteArrayContent = new ByteArrayContent(stream.ToArray());
                                byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/bson");

                                await client?.PostAsync("api/mail/sendemailasync", byteArrayContent, _cancellationToken);
                            }
                    }
                }
                _timer.Start();
            }
            catch (Exception ex)
            {
                _timer.Start();
                #region Write Log
                string address = BaseAddress + ".MailController(IEmail emailService)";
                var    dic     = LogBusiness.GetDictionary();
                dic.Add(LogFieldName.FullyQualifiedFunctionName, address);
                var dicParams = LogBusiness.GetDictionary();
                dicParams.Add(LogFieldName.Token, dicParams);
                dic = LogBusiness.GetDictionary();
                dic.Add(LogFieldName.FullyQualifiedFunctionName, address);
                dic.Add(LogFieldName.ErrorMessage, ex.Message);
                dic.Add(LogFieldName.StackTrace, ex.StackTrace);
                LogBusiness.CustomLog(fromMail, LogEvent.ErrorEvent, dic, dicParams);
                #endregion
            }
            _timer.Start();
        }
Ejemplo n.º 7
0
        internal static ErrorMessage ManageEmailConfig(EMailViewModel EmailVM, CommandTypeEnum commandtype)
        {
            ErrorMessage errmsg           = new ErrorMessage();
            string       connectionString = ConfigurationManager.ConnectionStrings["myCon"].ConnectionString;

            try
            {
                using (MySqlConnection con = new MySqlConnection(connectionString))
                {
                    con.Open();
                    MySqlCommand cmd = new MySqlCommand("spEmailConfig", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    MySqlParameter pId = new MySqlParameter();
                    pId.ParameterName = "in_Id";
                    pId.Value         = EmailVM.Id;
                    cmd.Parameters.Add(pId);

                    MySqlParameter pEmailName = new MySqlParameter();
                    pEmailName.ParameterName = "in_EmailName";
                    pEmailName.Value         = EmailVM.EmailName;
                    cmd.Parameters.Add(pEmailName);

                    MySqlParameter pEmailFrom = new MySqlParameter();
                    pEmailFrom.ParameterName = "in_EmailFrom";
                    pEmailFrom.Value         = EmailVM.EmailFrom;
                    cmd.Parameters.Add(pEmailFrom);

                    MySqlParameter pEmailPassword = new MySqlParameter();
                    pEmailPassword.ParameterName = "in_EmailPassword";
                    pEmailPassword.Value         = EmailVM.EmailPassword;
                    cmd.Parameters.Add(pEmailPassword);

                    MySqlParameter pSMTPAddress = new MySqlParameter();
                    pSMTPAddress.ParameterName = "in_SMTPAddress";
                    pSMTPAddress.Value         = EmailVM.SMTPAddress;
                    cmd.Parameters.Add(pSMTPAddress);

                    MySqlParameter pSMTPPort = new MySqlParameter();
                    pSMTPPort.ParameterName = "in_SMTPPort";
                    pSMTPPort.Value         = EmailVM.SMTPPort;
                    cmd.Parameters.Add(pSMTPPort);

                    MySqlParameter pEnableSSL = new MySqlParameter();
                    pEnableSSL.ParameterName = "in_EnableSSL";
                    pEnableSSL.Value         = EmailVM.EnableSSL;
                    cmd.Parameters.Add(pEnableSSL);

                    MySqlParameter pEmailTo1 = new MySqlParameter();
                    pEmailTo1.ParameterName = "in_EmailTo1";
                    pEmailTo1.Value         = EmailVM.EmailTo1;
                    cmd.Parameters.Add(pEmailTo1);

                    MySqlParameter pEmailTo2 = new MySqlParameter();
                    pEmailTo2.ParameterName = "in_EmailTo2";
                    pEmailTo2.Value         = EmailVM.EmailTo2;
                    cmd.Parameters.Add(pEmailTo2);

                    MySqlParameter pEmailTo3 = new MySqlParameter();
                    pEmailTo3.ParameterName = "in_EmailTo3";
                    pEmailTo3.Value         = EmailVM.EmailTo3;
                    cmd.Parameters.Add(pEmailTo3);

                    MySqlParameter pEmailTo4 = new MySqlParameter();
                    pEmailTo4.ParameterName = "in_EmailTo4";
                    pEmailTo4.Value         = EmailVM.EmailTo4;
                    cmd.Parameters.Add(pEmailTo4);

                    MySqlParameter pEmailTo5 = new MySqlParameter();
                    pEmailTo5.ParameterName = "in_EmailTo5";
                    pEmailTo5.Value         = EmailVM.EmailTo5;
                    cmd.Parameters.Add(pEmailTo5);

                    MySqlParameter pEntryDate = new MySqlParameter();
                    pEntryDate.ParameterName = "in_EntryDate";
                    pEntryDate.Value         = EmailVM.EntryDate;
                    cmd.Parameters.Add(pEntryDate);

                    MySqlParameter pCommandType = new MySqlParameter();
                    pCommandType.ParameterName = "in_CommandType";
                    pCommandType.Value         = commandtype.ToString();
                    cmd.Parameters.Add(pCommandType);

                    MySqlParameter outPutErrorId = new MySqlParameter();
                    outPutErrorId.ParameterName = "out_ErrorId";
                    outPutErrorId.Direction     = System.Data.ParameterDirection.Output;
                    cmd.Parameters.Add(outPutErrorId);

                    MySqlParameter outPutErrorMessage = new MySqlParameter();
                    outPutErrorMessage.ParameterName = "out_ErrorMessage";
                    outPutErrorMessage.Direction     = System.Data.ParameterDirection.Output;
                    cmd.Parameters.Add(outPutErrorMessage);

                    cmd.ExecuteNonQuery();
                    errmsg.ErrorId          = (int)outPutErrorId.Value;
                    errmsg.ErrorDescription = outPutErrorMessage.Value.ToString();
                }
            }
            catch (Exception ex)
            {
                errmsg.ErrorId          = 0;
                errmsg.ErrorDescription = ex.Message;
            }
            return(errmsg);
        }
Ejemplo n.º 8
0
/// <summary>
///
/// </summary>
/// <param name="ServerName"></param>
/// <param name="EmailVM"></param>
        public static int AddEmailToDatabase(int Id, MachinesViewModel SelectedMachine, EMailViewModel EmailVM)
        {
            int result = -1;

            try
            {
                using (MySqlConnection con = new MySqlConnection(connectionString))
                {
                    con.Open();
                    MySqlCommand cmd = new MySqlCommand("spAddEmailLog", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    MySqlParameter pId = new MySqlParameter();
                    pId.ParameterName = "in_Id";
                    pId.Value         = Id;
                    cmd.Parameters.Add(pId);

                    MySqlParameter pEmailFrom = new MySqlParameter();
                    pEmailFrom.ParameterName = "in_EmailFrom";
                    pEmailFrom.Value         = EmailVM.EmailFrom;
                    cmd.Parameters.Add(pEmailFrom);

                    MySqlParameter pServerName = new MySqlParameter();
                    pServerName.ParameterName = "in_ServerName";
                    pServerName.Value         = SelectedMachine.MachineName;
                    cmd.Parameters.Add(pServerName);

                    MySqlParameter pEmailTo = new MySqlParameter();
                    pEmailTo.ParameterName = "in_EmailTo";
                    pEmailTo.Value         = SelectedMachine.EmailTo;
                    cmd.Parameters.Add(pEmailTo);

                    MySqlParameter pEmailTo1 = new MySqlParameter();
                    pEmailTo1.ParameterName = "in_EmailTo1";
                    pEmailTo1.Value         = EmailVM.EmailTo1;
                    cmd.Parameters.Add(pEmailTo1);

                    MySqlParameter pEmailTo2 = new MySqlParameter();
                    pEmailTo2.ParameterName = "in_EmailTo2";
                    pEmailTo2.Value         = EmailVM.EmailTo2;
                    cmd.Parameters.Add(pEmailTo2);

                    MySqlParameter pEmailTo3 = new MySqlParameter();
                    pEmailTo3.ParameterName = "in_EmailTo3";
                    pEmailTo3.Value         = EmailVM.EmailTo3;
                    cmd.Parameters.Add(pEmailTo3);

                    MySqlParameter pEmailTo4 = new MySqlParameter();
                    pEmailTo4.ParameterName = "in_EmailTo4";
                    pEmailTo4.Value         = EmailVM.EmailTo4;
                    cmd.Parameters.Add(pEmailTo4);
                    MySqlParameter pEmailTo5 = new MySqlParameter();
                    pEmailTo5.ParameterName = "in_EmailTo5";
                    pEmailTo5.Value         = EmailVM.EmailTo5;
                    cmd.Parameters.Add(pEmailTo5);

                    MySqlParameter pEmailSubject = new MySqlParameter();
                    pEmailSubject.ParameterName = "in_EmailSubject";
                    pEmailSubject.Value         = EmailVM.EmailSubject;
                    cmd.Parameters.Add(pEmailSubject);

                    MySqlParameter pEmailBody = new MySqlParameter();
                    pEmailBody.ParameterName = "in_EmailBody";
                    pEmailBody.Value         = EmailVM.EmailBody;
                    cmd.Parameters.Add(pEmailBody);

                    MySqlParameter pEntryDate = new MySqlParameter();
                    pEntryDate.ParameterName = "in_EntryDate";
                    pEntryDate.Value         = DateTime.Now;
                    cmd.Parameters.Add(pEntryDate);

                    result = cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                result = -1;
                logger.LogError("Exception in AddEmailToDatabase method,Message : " + ex.Message);
            }
            return(result);
        }
Ejemplo n.º 9
0
 internal static ErrorMessage ManageEmailConfig(EMailViewModel emailviewmodel, CommandTypeEnum commandtype)
 {
     return(Dalc.Dalc.ManageEmailConfig(emailviewmodel, commandtype));
 }
Ejemplo n.º 10
0
 public static void ShowCreate()
 {
     _emailViewModel = new EMailViewModel();
     _isCreate       = true;
     Show();
 }