55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using System.Net.Mail;
|
|
using System.Net;
|
|
using System.Web.Mvc;
|
|
using System.Web.ModelBinding;
|
|
using System;
|
|
using Newtonsoft.Json;
|
|
|
|
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(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" });
|
|
}
|
|
}
|
|
} |