【问题标题】:SMTP settings in appSettings.json in dotnet core 2.0dotnet core 2.0 中 appSettings.json 中的 SMTP 设置
【发布时间】:2019-02-21 09:57:20
【问题描述】:

在 Asp.net 中,我通常可以使用以下代码发送电子邮件:

using (var smtp = new SmtpClient())
{
    await smtp.SendMailAsync(mailMessage);
}

通过 web.config 中提供的 smtp 设置,SmtpClient 会自动使用这些设置。 web.config 配置部分如下所示:

<mailSettings>
    <smtp deliveryMethod="Network">
      <network host="myHost" port="25" userName="myUsername" password="myPassword" defaultCredentials="false" />
    </smtp>
</mailSettings>

是否可以在 dotnet core 2.0 应用程序的 appSettings.json 文件中进行配置,然后供SmtpClient 使用,类似于 Asp.net?

【问题讨论】:

  • 看看stackoverflow.com/questions/41407221/…这应该回答你的问题
  • 首先,不要使用 SmptClient。其次,.NET Core 中的任何 XML 或 JSON 部分都没有特殊含义。 appsettings.json也没有什么特殊含义,只是WebHostBuilder使用的默认文件名。
  • 为了解释don't use SmptClient.,该类的文档本身解释了it's obsolete,仅出于兼容性原因即使在完整框架中并且您should use MailKit instead
  • 使用您想要的任何 SMTP 服务的正确方法是从 .NET 配置系统,从您配置的任何来源加载其配置。它可能是您的json 文件中名为"MyMailSettings" 的部分。其次,您应该将该类注册为服务,而不是手动实例化它。
  • 检查Using MailKit To Send And Receive Email In ASP.net Core。它展示了如何从 JSON 加载设置,如何创建和注册 IEmailService

标签: c# asp.net .net-core


【解决方案1】:

如果你坚持使用 System.Net.Mail.SmtpClient,可以这样:

appsettings.json

{
  "Smtp": {
    "Server": "mail.whatever.com",
    "Port": 25,
    "FromAddress": "yourfromemail@whatever.com"
  },
}

代码:

    public async Task SendEmailAsync(string email, string subject, string htmlMessage)
    {
        MailMessage message = new MailMessage();
        message.Subject = subject;
        message.Body = htmlMessage;
        message.IsBodyHtml = true;
        message.To.Add(email);

        string host = _config.GetValue<string>("Smtp:Server", "defaultmailserver");
        int port = _config.GetValue<int>("Smtp:Port", 25);
        string fromAddress = _config.GetValue<string>("Smtp:FromAddress", "defaultfromaddress");

        message.From = new MailAddress(fromAddress);

        using (var smtpClient = new SmtpClient(host, port))
        {
            await smtpClient.SendMailAsync(message);
        }
    }

其中_configIConfiguration 的实现,它被注入到SendEmailAsync 方法所在的类中。

但是,由于它已经过时,最好探索上述 cmets 中提到的其他方法。

【讨论】:

  • 我不想手动从 appsettings.json 获取 smtp 配置值。我在问这些值是否可以自动使用。
  • 不要认为这是可能的。
  • 它给了我这个错误“邮箱不可用。服务器响应是:不允许未经身份验证的发件人”@HaukurHaf
  • @NitikaChopra 这意味着您使用的邮件服务器需要身份验证。您需要添加对用户名/密码的支持,然后将该信息连同主机和端口一起发送到 SmtpClient 类构造函数中。
  • @HaukurHaf 感谢您的回复。但实际上我的愚蠢问题是我输入了错误的用户名,我纠正这个我的问题解决了.. :)
【解决方案2】:

你可能想要达到的效果是这样的:

文件: appsettings.json(您在其中定义数据)

{
    ...
    "EmailSettings": {
        "From": "no-reply@example.com",
        "Username": "username",
        "Password": "password",
        "Host": "smtp.example.com",
        "Port": 25
    },
    ...
}

文件: Email.cs(您在其中定义将包含数据的模型)

public class EmailSettings
{
    public string From { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string Host { get; set; }
    public int Port { get; set; }
}

文件: Startup.cs(将数据绑定到随处可用的模型)

public void ConfigureServices(IServiceCollection services)
{              
   ...

     services.AddConfiguration<EmailSettings>(Configuration, "EmailSettings");

   ...
}

文件: Email.cs(您可以在其中定义如何发送邮件)

public class EmailService
{
    private readonly EmailSettings _mailSettings;
    public EmailService(EmailSettings mailSettings)
    {
        _mailSettings = mailSettings;
    }

    public async Task<bool> SendMail(MailRequest mailRequest)
    {
        //Here goes your code to send a message using "_mailSettings" like:
        // _mailSettings.From
        // _mailSettings.Port
        //ect...
    }
}

为此打开 this tutorial 并转到名为“#6 AppSettings – PRE-Binding”的 Way 6

您会找到不同的方法来完成这项工作,但强烈推荐第 6 种方法

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-12-15
  • 2017-10-22
  • 2019-07-06
  • 2018-02-11
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 2017-10-27
相关资源
最近更新 更多