Example #1
0
        public async Task <ActionResult> SendMultipleEmail(CheckBoxList model)
        {
            if (model.checklist.Count(x => x.IsSelected) > 0)
            {
                var selectedEmail = model.GetSelectedEmail();
                var body          = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
                var message       = new MailMessage();
                foreach (var journalistEmail in selectedEmail)
                {
                    //string journalistEmail = db.Journalists.Find(journalistId).email;
                    message.To.Add(new MailAddress(journalistEmail));
                }
                message.From       = new MailAddress("*****@*****.**"); // replace with valid value
                message.Subject    = "MVC Email to Journalist";
                message.Body       = string.Format(body, model.FromName, model.FromEmail, model.message);
                message.IsBodyHtml = true;
                //if (model.Upload != null && model.Upload.ContentLength > 0)
                //{
                //    message.Attachments.Add(new Attachment(model.Upload.InputStream, Path.GetFileName(model.Upload.FileName)));
                //}

                using (var smtp = new SmtpClient())
                {
                    var credential = new NetworkCredential
                    {
                        UserName = "******",  // replace with valid value
                                                                   //Password = ""  // replace with valid value
                    };
                    smtp.Credentials = credential;
                    smtp.Host        = "smtp.monash.edu.au";
                    //smtp.Port = 587;
                    //smtp.EnableSsl = true;
                    await smtp.SendMailAsync(message);

                    return(RedirectToAction("Sent"));
                }
                //}
                //}
            }
            else
            {
                ViewBag.Message = "Please select at least 1 email";
            }
            return(View(model));
        }