Skip to content

Instantly share code, notes, and snippets.

@thomasgroch
Forked from Lajt/smpt-test-server-blazor.md
Created December 3, 2022 01:45
Show Gist options
  • Save thomasgroch/50f2dbddfa77bc4014fa4e4dbbd861f8 to your computer and use it in GitHub Desktop.
Save thomasgroch/50f2dbddfa77bc4014fa4e4dbbd861f8 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