Forum-Inzynieria1/Trunk/Server/Forum/MailSender/Controllers/HomeController.cs

49 lines
1.5 KiB
C#
Raw Normal View History

2018-12-18 15:42:39 +01:00
using System.Net.Mail;
2018-12-08 19:27:21 +01:00
using System.Net;
using System.Web.Mvc;
2018-12-18 15:42:39 +01:00
using System.Net.Http;
using System.Web.ModelBinding;
using System;
using Newtonsoft.Json;
2018-12-08 19:27:21 +01:00
namespace MailSender.Controllers
{
public class HomeController : Controller
2018-12-18 15:42:39 +01:00
{
2018-12-08 19:27:21 +01:00
[HttpGet]
public ActionResult Index()
{
2018-12-18 15:42:39 +01:00
string jsonString = TempData["mailModel"] as string;
Models.MailModel model = null;
if (!String.IsNullOrEmpty(jsonString))
model = JsonConvert.DeserializeObject<Models.MailModel>(jsonString);
if (model == null)
model = new Models.MailModel();
return View(model);
2018-12-08 19:27:21 +01:00
}
[HttpGet]
public ActionResult Error(string errMsg)
{
ViewData.Add("errMsg", errMsg);
return View();
}
[HttpPost]
2018-12-18 15:42:39 +01:00
public ActionResult Send(MailSender.Models.MailModel model)
2018-12-08 19:27:21 +01:00
{
if (ModelState.IsValid)
{
var smtpClient = new SmtpClient(Settings.smtpServer, Settings.smtpPort)
{
Credentials = new NetworkCredential(Settings.user, Settings.pass),
EnableSsl = true
};
smtpClient.Send(Settings.user, model.EMailTo, model.Subject, model.Content);
return RedirectToAction("Index");
}
else
return RedirectToAction("Error", new { errMsg = "Error nie umiesz pisac maila menelu" });
}
}
}