This commit is contained in:
98
Pages/Contact.cshtml.cs
Normal file
98
Pages/Contact.cshtml.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net.Mail;
|
||||
using System.Net;
|
||||
|
||||
namespace contact.Pages
|
||||
{
|
||||
|
||||
public class ContactFormModel
|
||||
{
|
||||
[Required]
|
||||
public string Name { get; set; }
|
||||
[Required]
|
||||
public string Company { get; set; }
|
||||
[Required]
|
||||
public string Email { get; set; }
|
||||
[Required]
|
||||
public string Message { get; set; }
|
||||
}
|
||||
|
||||
public class ContactModel : PageModel
|
||||
{
|
||||
public string Message { get; set; }
|
||||
[BindProperty]
|
||||
public ContactFormModel Contact { get; set; }
|
||||
|
||||
|
||||
public ActionResult OnPost()
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return Page();
|
||||
}
|
||||
|
||||
SendMail();
|
||||
|
||||
//todo go back to where they came from?
|
||||
//or more likely just show a success page
|
||||
return RedirectToPage("Index");
|
||||
}
|
||||
|
||||
|
||||
private void SendMail()
|
||||
{
|
||||
var MessageBody = $"Name:\r\n{Contact.Name}\r\nCompany:\r\n{Contact.Company}\r\nMessage:\r\n{Contact.Message}";
|
||||
|
||||
//SEND TO US
|
||||
using (var message = new MailMessage())
|
||||
{
|
||||
message.To.Add(new MailAddress("support@ayanova.com"));
|
||||
message.From = new MailAddress(Contact.Email);
|
||||
message.Subject = $"Question about AyaNova from {Contact.Company}";
|
||||
message.Body = MessageBody;
|
||||
using (var smtpClient = new SmtpClient("mail.ayanova.com"))
|
||||
{
|
||||
|
||||
smtpClient.Host = "mail.ayanova.com";
|
||||
smtpClient.Port = 2525;
|
||||
smtpClient.UseDefaultCredentials = false;
|
||||
//NOTE: Do not use the noreply email address to send mail, it crashes the server somehow
|
||||
smtpClient.Credentials = new System.Net.NetworkCredential("webmaster@ayanova.com", "c63c17add818fca81cae71a241ea1b552675a86280b7e7e45d36cbf2e8f3bc0e");
|
||||
smtpClient.Send(message);
|
||||
}
|
||||
}
|
||||
|
||||
//SEND COPY TO CLIENT
|
||||
using (var message = new MailMessage())
|
||||
{
|
||||
message.To.Add(new MailAddress(Contact.Email));
|
||||
message.From = new MailAddress("noreply@ayanova.com");
|
||||
message.Subject = $"Confirmation that your question about AyaNova has been received from {Contact.Company}";
|
||||
message.Body = MessageBody;
|
||||
|
||||
using (var smtpClient = new SmtpClient("mail.ayanova.com"))
|
||||
{
|
||||
|
||||
smtpClient.Host = "mail.ayanova.com";
|
||||
smtpClient.Port = 2525;
|
||||
smtpClient.UseDefaultCredentials = false;
|
||||
//NOTE: Do not use the noreply email address to send mail, it crashes the server somehow
|
||||
smtpClient.Credentials = new System.Net.NetworkCredential("webmaster@ayanova.com", "c63c17add818fca81cae71a241ea1b552675a86280b7e7e45d36cbf2e8f3bc0e");
|
||||
smtpClient.Send(message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void OnGet()
|
||||
{
|
||||
Message = "Your contact page.";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user