na formu mám povinnej input EMAIL a textareu POPIS
pomocí ajaxu posílám do Controlleru
- Kód: Vybrat vše
var divEmailElement = $('#modalniOknoEmailu');
$("#divLoader").show();
$.post(actionUrl, dataToSend).done(function (data) {
$("#divLoader").hide();
if (data.odeslo != null) {
if (data.odeslo) {
divEmailElement.find('.modal-title').html("email odeslán");
divEmailElement.find('.modal-body').html(data.obsahSuccess);
}
else {
divEmailElement.find('.modal-title').html("chyba odeslání");
divEmailElement.find('.modal-body').html(data.obsahError);
}
}
else {
var newBody = $('.modal-body', data);
divEmailElement.find('.modal-body').replaceWith(newBody);
};
});
controller:
- Kód: Vybrat vše
[HttpGet]
public ActionResult PosliEmail(string modelUrl)
{
var model = new Email { };
model.Predmet = modelUrl;
return PartialView("_emailModalPartial", model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult PosliEmail(Email email)
{
if (ModelState.IsValid)
{
try
{
var contentRoot = _env.ContentRootPath;
string poOdeslani = string.Empty;
if(OdesliEmail(email.Predmet, email.EmailOdesilatele, email.Obsah)) {
using (StreamReader reader = new StreamReader(contentRoot + "\\Views\\Karta\\_emailSuccess.cshtml"))
{
poOdeslani = reader.ReadToEnd();
}
return Json(new { odeslo = true, obsahSuccess = poOdeslani });
}
else
{
using (StreamReader reader = new StreamReader(contentRoot + "\\Views\\Karta\\_emailError.cshtml"))
{
poOdeslani = reader.ReadToEnd();
}
return Json(new { odeslo = false, obsahError = poOdeslani });
}
}
catch (Exception ex)
{
return Json(new { odeslo = false, errorMessage = ex });
}
}
else
{
return PartialView("_EmailModalPartial", email);
}
}
a funkce odeslání emailu:
- Kód: Vybrat vše
public bool OdesliEmail(string predmet, string emailOdesilatele, string obsah)
{
try
{
MimeMessage message = new MimeMessage();
MailboxAddress from = new MailboxAddress(emailOdesilatele, emailOdesilatele);
message.From.Add(from);
MailboxAddress toDyzon = new MailboxAddress("dyzon", "dyzon@email.cz");
message.To.Add(toDyzon);
message.Subject = predmet;
var contentRoot = _env.ContentRootPath;
string telo = string.Empty;
using (StreamReader reader = new StreamReader(contentRoot + "\\Models\\TeloEmailu.cshtml"))
{
telo = reader.ReadToEnd();
}
telo = telo.Replace("{odesilatel}", emailOdesilatele);
telo = telo.Replace("{predmet}", predmet);
telo = telo.Replace("{obsah}", obsah);
BodyBuilder bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = telo;
message.Body = bodyBuilder.ToMessageBody();
string user = configuration.GetSection("EmailData").GetSection("EmailUser").Value;
string pass = configuration.GetSection("EmailData").GetSection("EmailPass").Value;
SmtpClient client = new SmtpClient();
client.Connect("email.active24.com", 465, true);
client.Authenticate(user, pass);
client.Send(message);
client.Disconnect(true);
client.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
no a problém je, že z localu se email odešle v pohodě, ale když to nahraju na server, tak mi to háže chybu:
POST https://mojedomena.cz/Karta/PosliEmail 500 (Internal Server Error)
prosím nevíte někdo co je špatně ??