Skip to content

Support SMTP Username #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appsettings.json.sample
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"SmtpDomain": "smtp.gmail.com",
"SmtpPort": 587,
"Email": "",
"Username": "",
"Password": ""
},

Expand Down
51 changes: 22 additions & 29 deletions src/iCTF Website/Services/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,57 @@

namespace iCTF_Website.Services
{
public interface IEmailService {
public interface IEmailService
{
void Send(string to, string subject, string html);
Task SendAsync(string to, string subject, string html);
}

public class EmailService : IEmailService {
public class EmailService : IEmailService
{
private readonly record struct EmailSettings(string SmtpDomain, int SmtpPort, string Email, string Username, string Password);

private readonly IConfiguration _configuration;
private readonly EmailSettings _settings;

public EmailService(IServiceProvider serviceProvider)
{
_configuration = serviceProvider.GetService<IConfiguration>();
_settings = _configuration.GetSection("EmailSettings").Get<EmailSettings>();
}

public void Send(string to, string subject, string html)
{
var settings = _configuration.GetSection("EmailSettings");
string emailAddress = settings.GetValue<string>("Email");
string password = settings.GetValue<string>("Password");
string smtpDomain = settings.GetValue<string>("SmtpDomain");
int smtpPort = settings.GetValue<int>("SmtpPort");
var email = GetMessage(to, _settings.Email, subject, html);

// create message
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse(emailAddress));
email.To.Add(MailboxAddress.Parse(to));
email.Subject = subject;
email.Body = new TextPart(TextFormat.Html) { Text = html };

// send email
using var smtp = new SmtpClient();
smtp.Connect(smtpDomain, smtpPort, SecureSocketOptions.StartTls);
smtp.Authenticate(emailAddress, password);
smtp.Connect(_settings.SmtpDomain, _settings.SmtpPort, SecureSocketOptions.StartTls);
smtp.Authenticate(userName: _settings.Username, password: _settings.Password);
smtp.Send(email);
smtp.Disconnect(true);
}

public async Task SendAsync(string to, string subject, string html)
{
var settings = _configuration.GetSection("EmailSettings");
string emailAddress = settings.GetValue<string>("Email");
string password = settings.GetValue<string>("Password");
string smtpDomain = settings.GetValue<string>("SmtpDomain");
int smtpPort = settings.GetValue<int>("SmtpPort");
var email = GetMessage(to, _settings.Email, subject, html);

using var smtp = new SmtpClient();
await smtp.ConnectAsync(_settings.SmtpDomain, _settings.SmtpPort, SecureSocketOptions.StartTls);
await smtp.AuthenticateAsync(userName: _settings.Username, password: _settings.Password);
await smtp.SendAsync(email);
await smtp.DisconnectAsync(true);
}

private MimeMessage GetMessage(string to, string from, string subject, string html)
{
// create message
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse(emailAddress));
email.From.Add(MailboxAddress.Parse(from));
email.To.Add(MailboxAddress.Parse(to));
email.Subject = subject;
email.Body = new TextPart(TextFormat.Html) { Text = html };

// send email
using var smtp = new SmtpClient();
await smtp.ConnectAsync(smtpDomain, smtpPort, SecureSocketOptions.StartTls);
await smtp.AuthenticateAsync(emailAddress, password);
await smtp.SendAsync(email);
await smtp.DisconnectAsync(true);
return email;
}
}
}