62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using BLAIzor.Models;
|
|
using System.Net.Mail;
|
|
using System.Net;
|
|
using BLAIzor.Helpers;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
|
|
namespace BLAIzor.Services
|
|
{
|
|
public class EmailService : IEmailSender
|
|
{
|
|
private readonly SmtpSettings _smtpSettings;
|
|
public EmailService(IOptions<SmtpSettings> smtpSettings)
|
|
{
|
|
_smtpSettings = smtpSettings.Value;
|
|
}
|
|
public async Task SendEmailAsync(ContactFormModel formData, string Receiver)
|
|
{
|
|
using var client = new SmtpClient(_smtpSettings.Host, _smtpSettings.Port)
|
|
{
|
|
Credentials = new NetworkCredential(_smtpSettings.Username, _smtpSettings.Password),
|
|
EnableSsl = _smtpSettings.EnableSsl
|
|
};
|
|
|
|
var mailMessage = new MailMessage
|
|
{
|
|
From = new MailAddress(_smtpSettings.Username),
|
|
Subject = "New Contact Form Submission",
|
|
Body = $"Name: {formData.Name}\nEmail: {formData.Email}\nMessage:\n{formData.Message}",
|
|
IsBodyHtml = false,
|
|
};
|
|
|
|
mailMessage.To.Add(Receiver);
|
|
|
|
await client.SendMailAsync(mailMessage);
|
|
}
|
|
|
|
public async Task SendEmailAsync(string email, string subject, string message)
|
|
{
|
|
using var client = new SmtpClient(_smtpSettings.Host, _smtpSettings.Port)
|
|
{
|
|
Credentials = new NetworkCredential(_smtpSettings.Username, _smtpSettings.Password),
|
|
EnableSsl = _smtpSettings.EnableSsl
|
|
};
|
|
|
|
var mailMessage = new MailMessage
|
|
{
|
|
From = new MailAddress(_smtpSettings.Username),
|
|
Subject = subject,
|
|
Body = message,
|
|
IsBodyHtml = true
|
|
};
|
|
|
|
mailMessage.To.Add(email);
|
|
|
|
await client.SendMailAsync(mailMessage);
|
|
}
|
|
}
|
|
|
|
|
|
}
|