jhipster / jhipster-dotnetcore

JHipster.NET blueprint
Apache License 2.0
313 stars 91 forks source link

Send an email when creating an account #16

Open BananeGuimauve opened 5 years ago

BananeGuimauve commented 5 years ago

In the Java version, when creating an account, a user receives directly in his mailbox a confirmation email. We want to replicate this behavior in the .Net version. Another question then arises, where to store the templates of the mails ?

ivanmonteiro commented 4 years ago

The templates of the emails could be stored as razor templates, the same template files of asp.net core/mvc. Maybe the folder for email razor templates could be stored at Views/Mails or Views/Shared/Mails. With this, we can pass a Model object, like WelcomeMailModel, use it on the view and get the final html that will be used on the email. Email Model classes could be stored at Models/Mails subfolder.

For reference: https://www.c-sharpcorner.com/article/send-email-from-a-razor-page-in-net-core-2-using-system-net-mail/ https://corstianboerman.com/2020-01-07/sending-transactional-emails-from-asp-net-core.html

nicolas63 commented 4 years ago

I think is a good idea :smile: Can you try an implementation ?

duymap commented 2 years ago

Sending emails is a very common feature and is always needs for any project. So if having it in place, would be a big help for developers. E.g we can have mail sending code function implementation in MailService.cs, it reads config from JSON setting file, and developers replace their STMP information. This has been mentioned in this issue https://github.com/jhipster/jhipster-dotnetcore/issues/1036

ivanmonteiro commented 2 years ago

@duymap Yes, It is a very common feature.

It has been a long time since I did the coment above. There are libraries that do exactly what I described such as FluentEmail. But even a very simple email sending solution like SmtpClient should be enough.

By the way, seems that Microsoft docs do not recommend using SmtpClient in new development due to the lack of modern protocols (see the full explanation here) and recommends using MailKit instead, or other email libraries.

If you're willing to implement this and needs any help or assistance don't be afraid to ask

duymap commented 2 years ago

Yes, willing to do. Just quick questions to ask for speeding up:

ivanmonteiro commented 2 years ago

Yes, willing to do. Just quick questions to ask for speeding up:

  • What are the templates code located?

Here is the MailService class: https://github.com/jhipster/jhipster-dotnetcore/blob/main/generators/server/templates/dotnetcore/src/Project.Domain.Services/MailService.cs.ejs

And smtp credentials/configuration shoud be added at SecuritySettings class: https://github.com/jhipster/jhipster-dotnetcore/blob/main/generators/server/templates/dotnetcore/src/Project.Infrastructure/Configuration/SecuritySettings.cs.ejs

SecuritySettings class is loaded with information from appsettings.{environment}.json during startup

  • Develop is the latest branch that I should branch off, then create PR ?

It seems that develop is outdated. You shoud create a branch from main and then create a PR here