Skip to content

Instantly share code, notes, and snippets.

@Lajt
Created August 6, 2021 08:49
Show Gist options
  • Save Lajt/b8de9d050d94d3f73e31896e0658395a to your computer and use it in GitHub Desktop.
Save Lajt/b8de9d050d94d3f73e31896e0658395a to your computer and use it in GitHub Desktop.
  1. use https://github.com/maildev/maildev
  2. NodeJS required
  3. to install: npm install -g maildev
  4. start: maildev --outgoing-secure --outgoing-user lajt --outgoing-pass lajt
  5. ignore ssl check, only for dev environment! never in production. in your blazor server app add inside public void ConfigureServices(IServiceCollection services):
if (_env.IsDevelopment())
{
  ServicePointManager
						.ServerCertificateValidationCallback += 
					(sender, cert, chain, sslPolicyErrors) => true;
}

example settings file in appsettings.Development.json:

"MailSettings": {
  "Username": "lajt",
  "Password": "lajt",
  "FromEmail": "lajt@mail.com",
  "Host": "localhost",
  "Port": 1025
},

example method to send mail:

public async Task SendEmailAsync(string toEmail, string subject, string htmlBody)
{
    var message = new MailMessage();
    var client = new SmtpClient();
    message.From = new MailAddress(_mailSettings.FromEmail);
    message.To.Add(new MailAddress(toEmail));
    message.Subject = subject;
    message.IsBodyHtml = true;
    message.Body = htmlBody;

    client.Host = _mailSettings.Host;
    client.Port = _mailSettings.Port;
    client.EnableSsl = true;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential(_mailSettings.Username, _mailSettings.Password);
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    await client.SendMailAsync(message);
    
}
  1. navigate to http://localhost:1080/, all mails during development will be there
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment