81 lines
3.0 KiB
C#
81 lines
3.0 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using InnovEnergy.App.Backend.DataTypes;
|
|
using MailKit.Net.Smtp;
|
|
using MimeKit;
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
|
|
|
namespace InnovEnergy.App.Backend.Mailer;
|
|
public static class Mailer
|
|
{
|
|
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
|
|
public static Boolean SendVerificationMessage (User emailRecipientUser)
|
|
{
|
|
var config = JsonSerializer.Deserialize<SmptConfig>(File.OpenRead("./Resources/smtpConfig.json"))!;
|
|
var email = new MimeMessage();
|
|
|
|
try
|
|
{
|
|
|
|
email.From.Add(new MailboxAddress("InnovEnergy", "noreply@innov.energy"));
|
|
email.To.Add(new MailboxAddress(emailRecipientUser.Name, emailRecipientUser.Email));
|
|
|
|
email.Subject = "Create a new password for your Innovenergy-Account";
|
|
email.Body = new TextPart(MimeKit.Text.TextFormat.Plain) {
|
|
Text = "Dear " + emailRecipientUser.Name + "\n Please create a new password for your Innovenergy-account." +
|
|
"\n To do this just login at https://HEEEEELP"
|
|
};
|
|
|
|
using var smtp = new SmtpClient();
|
|
smtp.Connect(config.Url, config.Port, false);
|
|
|
|
smtp.Authenticate(config.Username, config.Password);
|
|
|
|
smtp.Send(email);
|
|
smtp.Disconnect(true);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
public static Boolean SendPasswordResetMessage (User emailRecipientUser, String token)
|
|
{
|
|
var config = JsonSerializer.Deserialize<SmptConfig>(File.OpenRead("./Resources/smtpConfig.json"))!;
|
|
var email = new MimeMessage();
|
|
|
|
//todo build me dynamically
|
|
const String resetLink = "https://monitor.innov.energy/resetPassword";
|
|
|
|
try
|
|
{
|
|
|
|
email.From.Add(new MailboxAddress("InnovEnergy", "noreply@innov.energy"));
|
|
email.To.Add(new MailboxAddress(emailRecipientUser.Name, emailRecipientUser.Email));
|
|
|
|
email.Subject = "Reset the password of your Innovenergy-Account";
|
|
email.Body = new TextPart(MimeKit.Text.TextFormat.Plain) {
|
|
Text = "Dear " + emailRecipientUser.Name
|
|
+ "\n To reset your password open this link:"
|
|
+ resetLink + "/"
|
|
+ token
|
|
};
|
|
|
|
using var smtp = new SmtpClient();
|
|
smtp.Connect(config.Url, config.Port, false);
|
|
|
|
smtp.Authenticate(config.Username, config.Password);
|
|
|
|
smtp.Send(email);
|
|
smtp.Disconnect(true);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|