Skip to content
Merged
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
21 changes: 20 additions & 1 deletion samples/AndreGoepel.AppFoundation.AppHost/AppHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@
// (AppFoundationOptions.DatabaseConnectionName == "appfoundation-database").
var database = postgres.AddDatabase("appfoundation-database", "appfoundation");

// MailHog captures outgoing development email locally: an SMTP server on 1025 and a web UI on
// 8025 to read what was "sent". Nothing leaves the machine, and no real mail account is needed.
var mailhog = builder
.AddContainer("mailhog", "mailhog/mailhog", "v1.0.1")
.WithEndpoint(name: "smtp", port: 1025, targetPort: 1025)
.WithHttpEndpoint(name: "http", port: 8025, targetPort: 8025);

// The sample web app, wired to the database and started only once it is ready.
builder
.AddProject<Projects.AndreGoepel_AppFoundation_Sample>("web")
.WithReference(database)
.WaitFor(database);
.WaitFor(database)
.WaitFor(mailhog)
// Pre-configure the EmailSender bootstrap section to send through MailHog. These values
// come from configuration rather than the database, so email keeps working after a
// database reset with no manual setup on the Email Settings page. MailHog needs no
// credentials, but the settings are required, so placeholders are supplied.
.WithEnvironment("EmailSender__SenderName", "AppFoundation Dev")
.WithEnvironment("EmailSender__SenderEmail", "dev@appfoundation.local")
.WithEnvironment("EmailSender__Server", "localhost")
.WithEnvironment("EmailSender__Port", "1025")
.WithEnvironment("EmailSender__UseSsl", "false")
.WithEnvironment("EmailSender__Username", "dev")
.WithEnvironment("EmailSender__Password", "dev");

builder.Build().Run();
22 changes: 17 additions & 5 deletions src/AndreGoepel.AppFoundation.MailService/SmtpEmailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,23 @@ await client.ConnectAsync(
configuration.UseSsl,
cancellationToken
);
await client.AuthenticateAsync(
configuration.Username,
configuration.Password,
cancellationToken
);

// Only authenticate when the server offers it and credentials are configured.
// Development relays such as MailHog accept mail without authentication and don't
// advertise AUTH, and MailKit throws if AuthenticateAsync is called against such a
// server.
if (
client.Capabilities.HasFlag(SmtpCapabilities.Authentication)
&& !string.IsNullOrEmpty(configuration.Username)
)
{
await client.AuthenticateAsync(
configuration.Username,
configuration.Password,
cancellationToken
);
}

await client.SendAsync(message, cancellationToken);
await client.DisconnectAsync(true, cancellationToken);
}
Expand Down
Loading