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.Web.ModelBinding;
|
|
|
|
|
using System;
|
|
|
|
|
using Newtonsoft.Json;
|
2018-12-20 01:59:38 +01:00
|
|
|
|
using System.Configuration;
|
2018-12-08 19:27:21 +01:00
|
|
|
|
|
|
|
|
|
namespace MailSender.Controllers
|
|
|
|
|
{
|
2018-12-20 01:22:51 +01:00
|
|
|
|
[RoutePrefix("")]
|
2018-12-08 19:27:21 +01:00
|
|
|
|
public class HomeController : Controller
|
2018-12-18 15:42:39 +01:00
|
|
|
|
{
|
2018-12-19 12:32:14 +01:00
|
|
|
|
[HttpPost]
|
2018-12-19 12:55:35 +01:00
|
|
|
|
public ActionResult Index([System.Web.Http.FromBody]MailDTO.MailDTO mail)
|
2018-12-08 19:27:21 +01:00
|
|
|
|
{
|
2018-12-20 01:22:51 +01:00
|
|
|
|
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());
|
2018-12-08 19:27:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-19 12:55:35 +01:00
|
|
|
|
[System.Web.Http.HttpGet]
|
2018-12-08 19:27:21 +01:00
|
|
|
|
public ActionResult Error(string errMsg)
|
|
|
|
|
{
|
2018-12-20 01:22:51 +01:00
|
|
|
|
TempData.Add("errMsg", errMsg);
|
|
|
|
|
return RedirectToAction("Index");
|
2018-12-08 19:27:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-19 12:55:35 +01:00
|
|
|
|
[System.Web.Mvc.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)
|
|
|
|
|
{
|
2018-12-20 01:59:38 +01:00
|
|
|
|
var smtpClient = new SmtpClient(ConfigurationManager.AppSettings["smtp-server"],
|
|
|
|
|
Int32.Parse(ConfigurationManager.AppSettings["smtp-port"]))
|
2018-12-08 19:27:21 +01:00
|
|
|
|
{
|
2018-12-20 01:59:38 +01:00
|
|
|
|
Credentials = new NetworkCredential(
|
|
|
|
|
ConfigurationManager.AppSettings["user-mail"],
|
|
|
|
|
ConfigurationManager.AppSettings["pass-mail"]),
|
2018-12-08 19:27:21 +01:00
|
|
|
|
EnableSsl = true
|
|
|
|
|
};
|
2018-12-20 01:59:38 +01:00
|
|
|
|
smtpClient.Send(ConfigurationManager.AppSettings["user-mail"], model.EMailTo, model.Subject, model.Content);
|
|
|
|
|
return Redirect(ConfigurationManager.AppSettings["forum-address"]);
|
2018-12-08 19:27:21 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
2018-12-20 01:59:38 +01:00
|
|
|
|
return RedirectToAction("Error", new { errMsg = "Błąd wysyłania wiadomości e-mail." });
|
2018-12-08 19:27:21 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|