使用 NetCore.MailKit 发送电子邮件时地址的 ArgumentNullException

ArgumentNullException for address when sending email using NetCore.MailKit

我正在使用 NetCore.MailKit NuGet 包来帮助我发送一封包含 link 的电子邮件,以便在用户注册时确认他们的电子邮件。我已按照他们 Github page 上的步骤进行操作,但是,我收到了一个 ArgumentNullException 作为地址,即使我已经设置了发送地址。

错误

ArgumentNullException: Value cannot be null. (Parameter 'address')
    MimeKit.MailboxAddress..ctor(Encoding encoding, string name, string address)

在我的控制器中使用 IEmailService 发送电子邮件时出现上述错误:

_EmailService.Send("usersemail@gmail.com", "subject", "message body");

这是我的appsettings.json配置:

  "EmailConfiguration": {
    "Server": "smtp.gmail.com",
    "Port": 587,
    "SenderName": "name",
    "SenderEmail": "mygmail@gmail.com",
    "SenderPassword": "My gmail password"
  }

这是我在 startup.cs

中的设置
    services.AddMailKit(optionBuilder =>
    {
        optionBuilder.UseMailKit(new MailKitOptions()
        {
            //get options from sercets.json
            Server = Configuration["Server"],
            Port = Convert.ToInt32(Configuration["Port"]),
            SenderName = Configuration["SenderName"],
            SenderEmail = Configuration["SenderEmail"],
            Account = Configuration["SenderEmail"],
            Password = Configuration["SenderPassword"],
            Security = true
        });
    });

下面是 EmailService 的代码:

    public void Send(string mailTo, string subject, string message, bool isHtml = false)
    {
        SendEmail(mailTo, null, null, subject, message, Encoding.UTF8, isHtml);
    }

    private void SendEmail(string mailTo, string mailCc, string mailBcc, string subject, string message, Encoding encoding, bool isHtml)
    {
        var _to = new string[0];
        var _cc = new string[0];
        var _bcc = new string[0];
        if (!string.IsNullOrEmpty(mailTo))
            _to = mailTo.Split(',').Select(x => x.Trim()).ToArray();
        if (!string.IsNullOrEmpty(mailCc))
            _cc = mailCc.Split(',').Select(x => x.Trim()).ToArray();
        if (!string.IsNullOrEmpty(mailBcc))
            _bcc = mailBcc.Split(',').Select(x => x.Trim()).ToArray();

        Check.Argument.IsNotEmpty(_to, nameof(mailTo));
        Check.Argument.IsNotEmpty(message, nameof(message));

        var mimeMessage = new MimeMessage();

        //add mail from
        mimeMessage.From.Add(new MailboxAddress(_MailKitProvider.Options.SenderName, _MailKitProvider.Options.SenderEmail));

        //add mail to 
        foreach (var to in _to)
        {
            mimeMessage.To.Add(MailboxAddress.Parse(to));
        }

        //add mail cc
        foreach (var cc in _cc)
        {
            mimeMessage.Cc.Add(MailboxAddress.Parse(cc));
        }

        //add mail bcc 
        foreach (var bcc in _bcc)
        {
            mimeMessage.Bcc.Add(MailboxAddress.Parse(bcc));
        }

        //add subject
        mimeMessage.Subject = subject;

        //add email body
        TextPart body = null;

        if (isHtml)
        {

            body = new TextPart(TextFormat.Html);
        }
        else
        {
            body = new TextPart(TextFormat.Text);
        }
        //set email encoding
        body.SetText(encoding, message);

        //set email body
        mimeMessage.Body = body;

        using (var client = _MailKitProvider.SmtpClient)
        {
            client.Send(mimeMessage);
        }
    }

如您所见,我已经设置了所有内容,我是否遗漏了什么?为什么 MimeKit.MailboxAddress() 的地址为空?

我的猜测是以下行引发了异常:

mimeMessage.From.Add(new MailboxAddress(_MailKitProvider.Options.SenderName, _MailKitProvider.Options.SenderEmail));

这意味着_MailKitProvider.Options.SenderEmailnull

我知道您希望从您的 appsettings.json 文件中正确加载这些值,但似乎由于某种原因它们没有被加载。

您似乎没有正确加载配置设置。我怀疑您代码中的失败行是

//add mail from
mimeMessage.From.Add(new MailboxAddress(_MailKitProvider.Options.SenderName, _MailKitProvider.Options.SenderEmail));

Options全为null,导致异常

当您从配置文件加载设置时,您需要将部分名称添加到变量中。例如

services.AddMailKit(optionBuilder =>
{
    optionBuilder.UseMailKit(new MailKitOptions()
    {
        //get options from sercets.json
        Server = Configuration["EmailConfiguration:Server"],
        Port = Convert.ToInt32(Configuration["EmailConfiguration:Port"]),
        SenderName = Configuration["EmailConfiguration:SenderName"],
        SenderEmail = Configuration["EmailConfiguration:SenderEmail"],
        Account = Configuration["EmailConfiguration:SenderEmail"],
        Password = Configuration["EmailConfiguration:SenderPassword"],
        Security = true
    });
});