public ActionResult Index() { SmsService _smsService = new SmsService(); SharpGoogleVoice gv = new SharpGoogleVoice("*****@*****.**", "");//enter email gv.SendSMS("+14148408420", "test message");//- change number, just showing format prbly put this somewhere else^ - console app logic? //public ActionResult SendSMS(SMS sms) - this would be for different text service http://emekaemego.blogspot.com/2010/10/sending-sms-with-aspnet-mvc.html //{ //if (ModelState.IsValid) //{ // try // { // bool isSuccess = false; // string errMsg = null; // string response = _smsService.Send(sms); //Send sms // string code = _smsService.GetResponseMessage(response, out isSuccess, out errMsg); // if (!isSuccess) // { // ModelState.AddModelError("", errMsg); // } // else // { // ViewData["SuccessMsg"] = "Message was successfully sent."; // } // } // catch (Exception ex) // { // ModelState.AddModelError("", ex.Message); // } //} End text code return View(/*sms*/); }
public ActionResult Create([Bind(Include = "Id,Name,StartTime,EndTime,Description,Budget,Status,Priority,Flag,CommunityId")] CommunityTask communityTask) { var x1 = db.Communities; var x2 = x1.Where(i => i.Id == communityTask.CommunityId); var x3 = x2.Select(e => e.Email); var fromCommunityAddress = x3.FirstOrDefault(); var x4 = x2.Select(n => n.Name); var fromCommunityName = x4.FirstOrDefault(); var fromAddress = new MailAddress(fromCommunityAddress, fromCommunityName); string fromPassword = db.Communities .Where(i => i.Id == communityTask.CommunityId) .Select(p => p.Password). FirstOrDefault() .ToString(); string subject = communityTask.Name; string taskType; if (communityTask.Flag == true) { taskType = "task"; } else { taskType = "alert"; } string notificationBody = "Community \"" + fromCommunityName + "\" posted new " + taskType + ":\n"; notificationBody += "You can see that on <a href='/location'>"; var toMembersEmailList = db.Members .Where(i => i.CommunityId == communityTask.CommunityId) .Where(n => n.NotifyByEmail == true) .Select(e => e.Email) .ToList(); var toNameList = db.Members .Where(i => i.CommunityId == communityTask.CommunityId) .Where(n => n.NotifyByEmail == true) .Select(n => n.LastName) .ToList(); for (int index = 0; index < toMembersEmailList.Count; index++) { string toEmail = toMembersEmailList[index]; string toName = toNameList[index]; if (toEmail != null) { var toAddress = new MailAddress(toEmail, toName); var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = notificationBody }) { smtp.Send(message); } } } //Sending SMS notification if flag is true SharpGoogleVoice myAcc = new SharpGoogleVoice(fromCommunityAddress, fromPassword); var toNumbersList = db.Members .Where(i => i.CommunityId == communityTask.CommunityId) .Where(n => n.NotifyBySMS == true) .Select(p => p.Phone) .ToList(); for (int index = 0; index < toNumbersList.Count; index++) { string toNumber = "+"; toNumber += toNumbersList[index].ToString(); myAcc.SendSMS(toNumber, notificationBody); } if (ModelState.IsValid) { db.CommunityTasks.Add(communityTask); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CommunityId = new SelectList(db.Communities, "Id", "Name", communityTask.CommunityId); return(View(communityTask)); }