Skip to content

Commit

Permalink
fixing mail configurations for using gmail
Browse files Browse the repository at this point in the history
  • Loading branch information
hrmoh committed Feb 18, 2021
1 parent d784559 commit 16f01ee
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
3 changes: 2 additions & 1 deletion RMuseum/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
},
"SmptConfig": {
"Server": "smtp.gmail.com",
"Port": "465",
"Port": "587",
"UseSsl": "true",
"UseTls": "true",
"Username": "[email protected]",
"Password": "password",
"From": "[email protected]"
Expand Down
5 changes: 5 additions & 0 deletions RSecurityBackend/Models/Mail/SmptConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ public class SmptConfig
/// use ssl
/// </summary>
public bool useSsl { get; set; }

/// <summary>
/// use tls
/// </summary>
public bool useTls { get; set; }
}
}
5 changes: 5 additions & 0 deletions RSecurityBackend/RSecurityBackend.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions RSecurityBackend/Services/Implementation/MailKitEmailSender.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Configuration;
using MimeKit;
Expand Down Expand Up @@ -53,7 +54,8 @@ public SmptConfig SmptConfig
useSsl = bool.Parse($"{Configuration.GetSection("SmptConfig")["UseSsl"]}"),
smtpUsername = $"{ Configuration.GetSection("SmptConfig")["Username"] }",
smtpPassword = $"{Configuration.GetSection("SmptConfig")["Password"]}",
from = $"{ Configuration.GetSection("SmptConfig")["From"] }"
from = $"{ Configuration.GetSection("SmptConfig")["From"] }",
useTls = bool.Parse($"{Configuration.GetSection("SmptConfig")["useTls"]}"),

};
}
Expand All @@ -69,9 +71,9 @@ public SmptConfig SmptConfig
/// <param name="subject"></param>
/// <param name="message"></param>
/// <returns></returns>
public Task SendEmailAsync(string email, string subject, string message)
public async Task SendEmailAsync(string email, string subject, string message)
{
return Execute(Options, subject, message, email);
await Execute(Options, subject, message, email);
}

/// <summary>
Expand All @@ -82,7 +84,7 @@ public Task SendEmailAsync(string email, string subject, string message)
/// <param name="message"></param>
/// <param name="email"></param>
/// <returns></returns>
public Task Execute(SmptConfig options, string subject, string message, string email)
public async Task Execute(SmptConfig options, string subject, string message, string email)
{
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(new MailboxAddress(options.from, options.from));
Expand All @@ -95,21 +97,19 @@ public Task Execute(SmptConfig options, string subject, string message, string e
};


SmtpClient client = new SmtpClient();

// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s, c, h, e) => true;

client.Connect(options.server, options.port, options.useSsl);
using (SmtpClient client = new SmtpClient())
{
// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s, c, h, e) => true;

// Note: only needed if the SMTP server requires authentication
client.Authenticate(options.smtpUsername, options.smtpPassword);
await client.ConnectAsync(options.server, options.port, options.useTls ? SecureSocketOptions.StartTls : options.useSsl ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.None );

client.Send(mimeMessage);
client.Disconnect(true);
client.Dispose();
// Note: only needed if the SMTP server requires authentication
await client.AuthenticateAsync(options.smtpUsername, options.smtpPassword);

return Task.FromResult(0);
await client.SendAsync(mimeMessage);
await client.DisconnectAsync(true);
}
}
}
}

0 comments on commit 16f01ee

Please sign in to comment.