Dans MVC ASP.NET, vous pouvez définir la configuration smtp dans le fichier web.config comme ceci:
<system.net>
<mailSettings>
<smtp from="MyEmailAddress" deliveryMethod="Network">
<network Host="smtp.MyHost.com" port="25" />
</smtp>
</mailSettings>
</system.net>
Et cela fonctionne parfaitement.
Mais je ne peux pas le faire fonctionner dans .NET Core 2.2 car vous avez là un fichier appsettings.json.
J'ai ceci :
"Smtp": {
"Server": "smtp.MyHost.com",
"Port": 25,
"FromAddress": "MyEmailAddress"
}
Lors de l'envoi d'un courrier, il affiche ce message d'erreur:
Vous pouvez utiliser Options
avec DI dans votre expéditeur d'e-mail, reportez-vous à
https://kenhaggerty.com/articles/article/aspnet-core-22-smtp-emailsender-implementation
1.appsettings.json
"Smtp": {
"Server": "smtp.MyHost.com",
"Port": 25,
"FromAddress": "MyEmailAddress"
}
2.SmtpSettings.cs
public class SmtpSettings
{
public string Server { get; set; }
public int Port { get; set; }
public string FromAddress { get; set; }
}
3.Démarrer ConfigureServices
public class Startup
{
IConfiguration Configuration;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<SmtpSettings>(Configuration.GetSection("Smtp"));
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc();
}
}
4.Accédez aux SmtpSettings en utilisant Options
par DI où vous en avez besoin.
public class EmailSender : IEmailSender
{
private readonly SmtpSettings _smtpSettings;
public EmailSender(IOptions<SmtpSettings> smtpSettings)
{
_smtpSettings = smtpSettings.Value;
}
public Task SendEmailAsync(string email, string subject, string message)
{
var from = _smtpSettings.FromAddress;
//other logic
using (var client = new SmtpClient())
{
{
await client.ConnectAsync(smtpSettings.Server, smtpSettings.Port, true);
}
}
return Task.CompletedTask;
}
}
Afin d'obtenir les paramètres SMTP à partir de appsettings.json, suivez les étapes,
"EmailSettings": {
"EmailSettings": {
"MailServer": "smtp.gmail.com",
"MailPort": 587,
"SenderName": "Sender Name",
"SenderEmail": "preferred email",
"UserName": "username",
"Password": "password",
"EnableSsl": "true", /// if you need it use
"EmailKey": "EmailKey" /// if you need it use
}
définir les propriétés des identifiants de messagerie,
public void ConfigureServices(IServiceCollection services)
{
services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));
}
3. propriétés de classe,
public class EmailSettings
{
public string MailServer { get; set; }
public int MailPort { get; set; }
public string SenderName { get; set; }
public string SenderEmail { get; set; }
public string Password { get; set; }
public bool EnableSsl { get; set; }
public string EmailKey { get; set; }
}
var emailResult = SendEmail(loginName, Message, subject, "User Creation");
5. obtenir et définir les valeurs,
public async Task<IActionResult> SendEmail(string email, string Message, string Subject, string EmailType, string HTMLMessageContent = "")
{
HTMLMessageContent = Message;
EmailLog emailModel = new EmailLog
{
EmailType = EmailType,
Subject = Subject,
EmailContent = Message,
FromEmail = _emailSettings.SenderEmail,
ToEmails = email,
CreatedBy = sessionData != null ? sessionData.ApplicationUserId : "",
OrganizationId = sessionData != null ? sessionData.OrganizationId : 0
};
var from = new EmailAddress(_emailSettings.SenderEmail, _emailSettings.SenderName);
var to = new EmailAddress(email, "");
var msg = MailHelper.CreateSingleEmail(from, to, Subject, Message, HTMLMessageContent);
var response = await client.SendEmailAsync(msg);
return Ok("Success");
}
Enfin, l'e-mail envoyé à la personne concernée. Code Cool ..