2023-09-15 14:30:41 +00:00
|
|
|
using MailKit.Net.Smtp;
|
|
|
|
using MimeKit;
|
|
|
|
|
|
|
|
namespace InnovEnergy.Lib.Mailer;
|
|
|
|
|
|
|
|
|
|
|
|
public class Mailer
|
|
|
|
{
|
|
|
|
private static MimeMessage Email = new();
|
|
|
|
|
2023-09-15 14:59:59 +00:00
|
|
|
public MimeMessage To(String name, String emailAddress)
|
2023-09-15 14:30:41 +00:00
|
|
|
{
|
|
|
|
Email.To.Add(new MailboxAddress(name, emailAddress));
|
|
|
|
return Email;
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:59:59 +00:00
|
|
|
public MimeMessage From(String name, String emailAddress)
|
2023-09-15 14:30:41 +00:00
|
|
|
{
|
|
|
|
Email.From.Add(new MailboxAddress(name, emailAddress));
|
|
|
|
return Email;
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:59:59 +00:00
|
|
|
public MimeMessage Subject(String subjectText)
|
2023-09-15 14:30:41 +00:00
|
|
|
{
|
|
|
|
Email.Subject = subjectText;
|
|
|
|
return Email;
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:59:59 +00:00
|
|
|
public MimeMessage Body(String bodyText)
|
2023-09-15 14:30:41 +00:00
|
|
|
{
|
|
|
|
Email.Body = new TextPart(MimeKit.Text.TextFormat.Plain)
|
|
|
|
{
|
|
|
|
Text = bodyText
|
|
|
|
};
|
|
|
|
return Email;
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:59:59 +00:00
|
|
|
public Boolean SendEmailUsingSmtpConfig(SmtpConfig config)
|
2023-09-15 14:30:41 +00:00
|
|
|
{
|
|
|
|
try{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|