Exemple #1
0
        public ActionResult SendEmailWithAttachment(MailModel objModelMail, HttpPostedFileBase fileUploader
            , HttpPostedFileBase fileUploader2, HttpPostedFileBase fileUploader3, HttpPostedFileBase fileUploader4, HttpPostedFileBase fileUploader5)
        {
            if (ModelState.IsValid)
            {
                var user = UserContext.GetCurrentUser();
                var emailSend = user.EmailSend;
                var passwordSend = SecurityHelper.Decrypt(user.PasswordSend);
                //var type = objModelMail.TypeEmail;
                //type = 4;
                var emails = objModelMail.To.Split(';');

                foreach (var emailTo in emails)
                {
                    using (var mail = new MailMessage(emailSend, emailTo))
                    {
                        mail.Subject = objModelMail.Subject;
                        mail.Body = objModelMail.Body;
                        int type_email = objModelMail.TypeEmail;
                        int id = objModelMail.ContactId;
                        //mail.Attachments.Add(new Attachment(filetem, ck.Text));
                        if (fileUploader != null)
                        {
                            string fileName = Path.GetFileName(fileUploader.FileName);
                            mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
                        }
                        if (fileUploader2 != null)
                        {
                            string fileName = Path.GetFileName(fileUploader2.FileName);
                            mail.Attachments.Add(new Attachment(fileUploader2.InputStream, fileName));
                        }
                        if (fileUploader3 != null)
                        {
                            string fileName = Path.GetFileName(fileUploader3.FileName);
                            mail.Attachments.Add(new Attachment(fileUploader3.InputStream, fileName));
                        }
                        if (fileUploader4 != null)
                        {
                            string fileName = Path.GetFileName(fileUploader4.FileName);
                            mail.Attachments.Add(new Attachment(fileUploader4.InputStream, fileName));
                        }
                        if (fileUploader5 != null)
                        {
                            string fileName = Path.GetFileName(fileUploader5.FileName);
                            mail.Attachments.Add(new Attachment(fileUploader5.InputStream, fileName));
                        }

                        if (type_email == 4)
                        {
                            //string s = Request["anh_041"];
                            //if (Request["anh_041"] == "true")
                            //{
                                string fileName = Path.GetFileName(@"C:\FileAttach\huongdantestphongvan04.png");
                                FileStream fileStream = new FileStream(@"C:\FileAttach\huongdantestphongvan04.png", FileMode.Open);
                                mail.Attachments.Add(new Attachment(fileStream, fileName));
                            //}
                            //if (Request["filedoc_042"] == "true")
                            //{
                                string fileName2 = Path.GetFileName(@"C:\FileAttach\CASEC_Huongdandanhchothisinh4.docx");
                                FileStream fileStream2 = new FileStream(@"C:\FileAttach\CASEC_Huongdandanhchothisinh4.docx", FileMode.Open);
                                mail.Attachments.Add(new Attachment(fileStream2, fileName2));
                            //}
                        }

                        if(type_email == 6)
                        {
                            TestResultLinkSb100Info contactLevelInfos = new TestResultLinkSb100Info();
                            contactLevelInfos = TestResultRepository.GetResultLinkSb100Curent(id);
                            string sb100_chuoi = "";
                            sb100_chuoi = contactLevelInfos.LinkSb100.ToString();
                            string fileName3 = Path.GetFileName(sb100_chuoi);
                            //FileStream fileStream3 = new FileStream(sb100_chuoi, FileMode.Open);
                            //mail.Attachments.Add(new Attachment(fileStream3, fileName3));
                            var request = (HttpWebRequest)WebRequest.Create(sb100_chuoi);

                            var response = request.GetResponse();

                            var responseStream = response.GetResponseStream();

                            mail.Attachments.Add(new Attachment(responseStream, fileName3));

                        }
                        mail.IsBodyHtml = true;
                        mail.BodyEncoding = Encoding.UTF8;
                        var smtp = new SmtpClient {Host = "smtp.gmail.com", EnableSsl = true};
                        //{ Host = "smtp.gmail.com", EnableSsl = true };
                        var networkCredential = new NetworkCredential(emailSend, passwordSend);
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = networkCredential;
                        smtp.Port = 587;
                        smtp.Send(mail);
                        ViewBag.Message = "Sent";
                        //ViewBag.Message = EmailAcc;
                        //////Ghi log gửi email vào đây
                        //var log = new EmailHistory
                        //{
                        //    CreatedDate = DateTime.Now,
                        //    ContactId = objModelMail.ContactId,
                        //    Email = EmailAcc,
                        //    EmailTemplateId = objModelMail.TypeEmail,
                        //    UserId = user.UserID,

                        //};
                        //log.CreatedDate = DateTime.Now;
                        //log.ContactId = objModelMail.ContactId;
                        //log.EmailTemplateId = objModelMail.TypeEmail;
                        //log.Email = EmailAcc;
                        //log.UserId = user.UserID;
                        //TmpLogServiceRepository.Create(log);

                        return View("SendEmailWithAttachment", objModelMail);
                    }
                }
            }
            return View();
        }
Exemple #2
0
        public ActionResult SendEmailWithAttachment(MailModel objModelMail, HttpPostedFileBase fileUploader
                                                    , HttpPostedFileBase fileUploader2, HttpPostedFileBase fileUploader3, HttpPostedFileBase fileUploader4, HttpPostedFileBase fileUploader5)
        {
            if (ModelState.IsValid)
            {
                var user      = UserContext.GetCurrentUser();
                var emailSend = user.EmailSend;

                //var passwordSend = SecurityHelper.Decrypt(user.PasswordSend);
                var passwordSendtext = "198uR6IM2naAocEN07w/9g==";
                var passwordSend     = SecurityHelper.Decrypt(passwordSendtext);
                //var type = objModelMail.TypeEmail;
                //type = 4;
                var emails = objModelMail.To.Split(';');

                foreach (var emailTo in emails)
                {
                    using (var mail = new MailMessage(emailSend, emailTo))
                    {
                        mail.Subject = objModelMail.Subject;
                        mail.Body    = objModelMail.Body;
                        int type_email = objModelMail.TypeEmail;
                        int id         = objModelMail.ContactId;
                        if (fileUploader != null)
                        {
                            string fileName = Path.GetFileName(fileUploader.FileName);
                            mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
                        }
                        if (fileUploader2 != null)
                        {
                            string fileName = Path.GetFileName(fileUploader2.FileName);
                            mail.Attachments.Add(new Attachment(fileUploader2.InputStream, fileName));
                        }
                        if (fileUploader3 != null)
                        {
                            string fileName = Path.GetFileName(fileUploader3.FileName);
                            mail.Attachments.Add(new Attachment(fileUploader3.InputStream, fileName));
                        }
                        if (fileUploader4 != null)
                        {
                            string fileName = Path.GetFileName(fileUploader4.FileName);
                            mail.Attachments.Add(new Attachment(fileUploader4.InputStream, fileName));
                        }
                        if (fileUploader5 != null)
                        {
                            string fileName = Path.GetFileName(fileUploader5.FileName);
                            mail.Attachments.Add(new Attachment(fileUploader5.InputStream, fileName));
                        }

                        if (type_email == 4)
                        {
                            try
                            {
                                string path     = Server.MapPath("~/FileAttach/huongdantestphongvan04.png");
                                string fileName = Path.GetFileName(path);
                                //string fileName = Path.GetFileName(@"C:\FileAttach\huongdantestphongvan04.png");
                                FileStream fileStream = new FileStream(path, FileMode.Open);
                                mail.Attachments.Add(new Attachment(fileStream, fileName));

                                string path2     = Server.MapPath("~/FileAttach/CASEC_Huongdandanhchothisinh4.docx");
                                string fileName2 = Path.GetFileName(path2);
                                //string fileName2 = Path.GetFileName(@"C:\FileAttach\CASEC_Huongdandanhchothisinh4.docx");
                                FileStream fileStream2 = new FileStream(path2, FileMode.Open);
                                mail.Attachments.Add(new Attachment(fileStream2, fileName2));
                            }
                            catch { }
                        }

                        if (type_email == 7)
                        {
                            try
                            {
                                string     path       = Server.MapPath("~/FileAttach/huongdantestphongvan07.png");
                                string     fileName   = Path.GetFileName(path);
                                FileStream fileStream = new FileStream(path, FileMode.Open);
                                mail.Attachments.Add(new Attachment(fileStream, fileName));

                                string     path2       = Server.MapPath("~/FileAttach/Huongdansudungbaithilythuyet7.docx");
                                string     fileName2   = Path.GetFileName(path2);
                                FileStream fileStream2 = new FileStream(path2, FileMode.Open);
                                mail.Attachments.Add(new Attachment(fileStream2, fileName2));
                            }
                            catch { }
                        }

                        if (type_email == 6)
                        {
                            TestResultLinkSb100Info contactLevelInfos = new TestResultLinkSb100Info();
                            contactLevelInfos = TestResultRepository.GetResultLinkSb100Curent(id);
                            string sb100_chuoi = "";
                            sb100_chuoi = contactLevelInfos.LinkSb100.ToString();
                            string fileName3      = Path.GetFileName(sb100_chuoi);
                            var    request        = (HttpWebRequest)WebRequest.Create(sb100_chuoi);
                            var    response       = request.GetResponse();
                            var    responseStream = response.GetResponseStream();
                            mail.Attachments.Add(new Attachment(responseStream, fileName3));
                        }
                        mail.IsBodyHtml   = true;
                        mail.BodyEncoding = Encoding.UTF8;
                        var smtp = new SmtpClient {
                            Host = "smtp.gmail.com", EnableSsl = true
                        };
                        //{ Host = "smtp.gmail.com", EnableSsl = true };
                        var networkCredential = new NetworkCredential(emailSend, passwordSend);
                        smtp.UseDefaultCredentials = true;
                        smtp.Credentials           = networkCredential;
                        smtp.Timeout = 3600000;
                        smtp.Port    = 587;
                        smtp.Send(mail);
                        ViewBag.Message = "Sent";
                    }
                }
                return(View("SendEmailWithAttachment", objModelMail));
            }
            return(View());
        }