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

59 lines
1.9 KiB
C#

using System.Net.Mail;
using System.Net;
using System.Web.Mvc;
using System.Web.ModelBinding;
using System;
using Newtonsoft.Json;
using System.Configuration;
namespace MailSender.Controllers
{
[RoutePrefix("")]
public class HomeController : Controller
{
[HttpPost]
public ActionResult Index([System.Web.Http.FromBody]MailDTO.MailDTO mail)
{
var model = new Models.MailModel(mail);
TempData.Add("mailMod", model);
return View(model);
}
[HttpGet]
public ActionResult Index()
{
if(TempData["mailMod"] != null)
{
return View(TempData["mailMod"]);
}
return View(new Models.MailModel());
}
[System.Web.Http.HttpGet]
public ActionResult Error(string errMsg)
{
TempData.Add("errMsg", errMsg);
return RedirectToAction("Index");
}
[System.Web.Mvc.HttpPost]
public ActionResult Send(MailSender.Models.MailModel model)
{
if (ModelState.IsValid)
{
var smtpClient = new SmtpClient(ConfigurationManager.AppSettings["smtp-server"],
Int32.Parse(ConfigurationManager.AppSettings["smtp-port"]))
{
Credentials = new NetworkCredential(
ConfigurationManager.AppSettings["user-mail"],
ConfigurationManager.AppSettings["pass-mail"]),
EnableSsl = true
};
smtpClient.Send(ConfigurationManager.AppSettings["user-mail"], model.EMailTo, model.Subject, model.Content);
return Redirect(ConfigurationManager.AppSettings["forum-address"]);
}
else
return RedirectToAction("Error", new { errMsg = "Błąd wysyłania wiadomości e-mail." });
}
}
}