Exemplo n.º 1
0
        public ActionResult Index(SendMailViewModel svm)
        {
            if (ModelState.IsValid)
            {
                string key = ConfigurationManager.AppSettings["Sendgrid.Key"];
                SendGridEmailService messageSvc = new SendGridEmailService(key);

                string htmlBody = $@"<ul>
                                <li>Name: {svm.Name}</li>    
                                <li>Email: {svm.Email}</li>
                                <li>Phone Number: {svm.Phone}</li>
                                <li>Message Deatails: {svm.Message}</li>
                            </ul>";

                EmailMessage msg = new EmailMessage()
                {
                    Body      = htmlBody,
                    Subject   = "Cyberspace Test",
                    From      = svm.Name,
                    Recipient = "*****@*****.**"
                };

                string       envPath         = HttpRuntime.AppDomainAppPath;
                string       fileName        = $"{envPath}\\Log4net\\log-file.txt";
                byte[]       imageData       = null;
                FileInfo     fileInfo        = new FileInfo(fileName);
                long         imageFileLength = fileInfo.Length;
                FileStream   fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                imageData = br.ReadBytes((int)imageFileLength);

                messageSvc.SendMail(msg, true, fileName, imageData);
            }

            return(View());
        }
Exemplo n.º 2
0
 public async Task <HttpResponseMessage> SendMail(HttpRequestMessage request, SendMailViewModel model)
 {
     return(await CreateHttpResponse(request, () =>
     {
         string Body = "";
         if (model.toEmail == null || string.IsNullOrEmpty(model.Content.Trim()) || string.IsNullOrEmpty(model.Subject.Trim()))
         {
             return request.CreateErrorResponse(HttpStatusCode.BadRequest, nameof(model.toEmail) + MessageSystem.NoValues + nameof(model.ccToEmail) + MessageSystem.NoValues + nameof(model.Content) + MessageSystem.NoValues + nameof(model.Subject) + MessageSystem.NoValues);
         }
         else
         {
             Body = _humanService.getBodyMail(MailConsstants.TemplateSendMailToMember, model.Content);
             var rs = _humanService.SendMail(model.toEmail, model.ccToEmail, model.Subject, Body, model.attackFile);
             if (rs == true)
             {
                 return request.CreateResponse(HttpStatusCode.OK, model);
             }
             else
             {
                 return request.CreateErrorResponse(HttpStatusCode.NotFound, CommonConstants.SEND_MAIL_NO_SPACE);
             }
         }
     }));
 }
Exemplo n.º 3
0
        public IActionResult SendMail([FromBody] SendMailViewModel listEmployeeId)
        {
            var result = _mailContentRepository.Sendmail(listEmployeeId.listId);

            return(Ok(result));
        }
Exemplo n.º 4
0
        public static bool SendMail(SendMailViewModel model, ControllerContext controllerContext = null, IEmailLogService logger = null)
        {
            logger = logger ?? DependencyResolver.Current.GetService <IEmailLogService>();
            try
            {
                var message = new MailMessage();

                var toes = model.To.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var to in toes)
                {
                    message.To.Add(to);
                }
                message.From         = model.FromAddress;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml   = true;
                if (!string.IsNullOrEmpty(model.Bcc))
                {
                    toes = model.Bcc.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var to in toes)
                    {
                        message.Bcc.Add(to);
                    }
                }
                message.Subject = model.Subject;

                if (controllerContext != null)
                {
                    if (model.Files != null)
                    {
                        foreach (var file in model.Files)
                        {
                            var attachment = new Attachment(controllerContext.HttpContext.Server.MapPath(file.Name))
                            {
                                Name = file.SourceName
                            };
                            message.Attachments.Add(attachment);
                        }
                    }
                    var viewData = new ViewDataDictionary(model.ViewModel);
                    message.Body = ViewHelpers.RenderPartialViewToString(model.TemplateName, viewData, controllerContext);
                }
                else
                {
                    message.Body = model.Body;
                    if (model.Files != null)
                    {
                        foreach (var file in model.Files)
                        {
                            var attachment = new Attachment(file.Name)
                            {
                                Name = file.SourceName
                            };
                            message.Attachments.Add(attachment);
                        }
                    }
                }

                lock (LockObject)
                {
                    var client = new SmtpClient();
                    if (client.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory)
                    {
                        client.EnableSsl = false;
                    }
                    client.Send(message);
                }
                logger.Write(model.From, model.To, model.Subject, message.Body, null);
                return(true);
            }
            catch (Exception exception)
            {
                logger.Write(model.From, model.To, model.Subject, model.TemplateName, exception);
                return(false);
            }
        }
Exemplo n.º 5
0
        public SendMailPage()
        {
            InitializeComponent();

            BindingContext = viewModel = new SendMailViewModel(this);
        }
Exemplo n.º 6
0
        public IActionResult Listener(SendMailViewModel model)
        {
            try
            {
                /*
                 * var files = Request.Form.Files;
                 *
                 * if (files.Any(f => f.Length == 0))
                 * {
                 *  return BadRequest();
                 * }
                 */
                var origin = _origin.GetById(model.MailKey);

                var mail = new MimeMessage();

                mail.From.Add(new MailboxAddress(origin.MailAccount.Mail));

                foreach (var d in origin.Destinies)
                {
                    mail.To.Add(new MailboxAddress(d.Email));
                }

                var body = $"{origin.Body}\n\n";

                foreach (var p in model.Properties)
                {
                    body += $"{p.Key}: {p.Value} \n\n";
                }

                //mail.Body += $"<hr>";

                if (!string.IsNullOrEmpty(origin.Signature))
                {
                    body += $"\n\n{origin.Signature}";
                }

                mail.Body = new TextPart(MimeKit.Text.TextFormat.Text)
                {
                    Text = body
                };

                mail.Subject = origin.Subject;

                using (var client = new SmtpClient())
                {
                    try
                    {
                        if (origin.MailAccount.UseSSL)
                        {
                            client.Connect(origin.MailAccount.Server, origin.MailAccount.Port, origin.MailAccount.UseSSL);
                            client.Authenticate(origin.MailAccount.User, origin.MailAccount.Password);
                        }

                        else
                        {
                            client.Connect(origin.MailAccount.Server, origin.MailAccount.Port, SecureSocketOptions.None);
                        }

                        client.AuthenticationMechanisms.Remove("XOAUTH2");

                        client.Send(mail);
                    }
                    catch
                    {
                        //log an error message or throw an exception or both.
                        throw;
                    }
                    finally
                    {
                        client.Disconnect(true);
                        client.Dispose();
                    }
                }


                //var mailService = new SmtpClient(origin.MailAccount.Server, origin.MailAccount.Port);



                return(Ok());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Exemplo n.º 7
0
        //[ValidateAntiForgeryToken]
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    if (user.EmailConfirmed == false && user.PasswordHash == null)
                    {
                        ViewBag.Message = "Your email verification process is still pending. A verification email is sent to " + model.Email;
                        string token = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                        var mailContent = new SendMailViewModel()
                        {
                            Email = model.Email, Token = token, userId = user.Id, MailType = (int)MailType.EmailVerification, ChangePassword = true
                        };

                        var mailBody = ReturnMailBody(mailContent);
                        EmailHelper.SendEmail(model.Email, mailBody, "Activate Account", null, true);
                        return(View(model));
                    }
                    //await SignInAsync(user, model.RememberMe);
                    SignInStatus signInResult = await SignInManager.PasswordSignInAsync(user.UserName, model.Password, false, false);

                    if (signInResult != SignInStatus.Success)
                    {
                        ViewBag.Message = "Please enter correct details.";
                        return(View(model));
                    }
                    Session["token"] = await UserManager.GenerateUserTokenAsync("login", user.Id);

                    Session["userId"] = user.Id;

                    if (UserManager.IsInRole(user.Id, "Admin"))
                    {
                        Session["role"] = "Admin";
                        //Storing values in Cookies
                        AddCookie("role", "Admin");
                        return(RedirectToAction("Dashboard", "Admin"));
                    }
                    else if (UserManager.IsInRole(user.Id, "User"))
                    {
                        Session["role"] = "User";
                        AddCookie("role", "User");
                    }

                    //ViewBag.Message = model.Message;
                    if (returnUrl != null)
                    {
                        return(RedirectToLocal(returnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Dashboard", "Users", model.Message));
                    }
                }
                else
                {
                    ViewBag.Message = "Please enter correct details.";

                    //ModelState.AddModelError("", "Invalid username or password.");
                }
            }
            else
            {
                ViewBag.Message = "Email/Password is required.";
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemplo n.º 8
0
 public ActionResult Index(SendMailViewModel mailViewModel)
 {
     _mailSendLogic.SendMailAsync(mailViewModel.SendTo, mailViewModel.SendFrom, mailViewModel.Subject, mailViewModel.Text);
     return(View(mailViewModel));
 }