55 lines
1.2 KiB
C#
55 lines
1.2 KiB
C#
using MailKit.Net.Smtp;
|
|
using MimeKit;
|
|
|
|
namespace InnovEnergy.Lib.Mailer;
|
|
|
|
|
|
public class Mailer
|
|
{
|
|
private static MimeMessage Email = new();
|
|
|
|
public MimeMessage To(String name, String emailAddress)
|
|
{
|
|
Email.To.Add(new MailboxAddress(name, emailAddress));
|
|
return Email;
|
|
}
|
|
|
|
public MimeMessage From(String name, String emailAddress)
|
|
{
|
|
Email.From.Add(new MailboxAddress(name, emailAddress));
|
|
return Email;
|
|
}
|
|
|
|
public MimeMessage Subject(String subjectText)
|
|
{
|
|
Email.Subject = subjectText;
|
|
return Email;
|
|
}
|
|
|
|
public MimeMessage Body(String bodyText)
|
|
{
|
|
Email.Body = new TextPart(MimeKit.Text.TextFormat.Plain)
|
|
{
|
|
Text = bodyText
|
|
};
|
|
return Email;
|
|
}
|
|
|
|
public Boolean SendEmailUsingSmtpConfig(SmtpConfig config)
|
|
{
|
|
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;
|
|
}
|
|
} |