Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public record MailSettings(string Username, string Password, int Port, string FromEmail, string Host);
- public class MailService(MailSettings mailConfig)
- {
- public async Task SendEmailAsync(string toEmail, string subject, string htmlBody)
- {
- using SmtpClient client = new(mailConfig.Host, mailConfig.Port);
- using MailMessage mail = new(mailConfig.FromEmail, toEmail, subject, htmlBody);
- client.Credentials = new NetworkCredential(mailConfig.Username, mailConfig.Password);
- client.EnableSsl = true;
- mail.IsBodyHtml = true;
- await client.SendMailAsync(mail);
- }
- }
- MailService mailService = new(
- mailConfig: new MailSettings(
- Username: "YOUR_EMAIL",
- Password: "APP_ID_PASSWORD",
- Port: 587,
- FromEmail: "YOUR_EMAIL",
- Host: "smtp.gmail.com"
- )
- );
- await mailService.SendEmailAsync(
- toEmail: Email,
- subject: "Subject Line",
- htmlBody: "Email Body in HTML"
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement