当邮件服务器在本地网络中时,如何在没有凭据的情况下在 .Net Core 中添加 SMTP

How to add SMTP in .NetCore without credentials when the mail server is within the local network

如何在没有凭据的情况下在 .NET Core 2.1 中设置我的 SMTP 客户端。我的邮件服务器在网络内。

private async Task<bool> SendEmailToCustomer(string customerEmail, string Message)
{
    try
    {
        SmtpClient client = new SmtpClient(SmtpConfig.server);

        if (SmtpConfig.userName != "" && SmtpConfig.password != "")
        {
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(SmtpConfig.userName, SmtpConfig.password);
        }

        //client.DeliveryMethod = SmtpDeliveryMethod.Network;
        MailMessage mailMessage = new MailMessage();
        mailMessage.From = new MailAddress(SmtpConfig.fromForCustomer);
        mailMessage.To.Add(customerEmail);
        mailMessage.Body = Message;
        mailMessage.Subject = SmtpConfig.subjectForCustomer;
        await client.SendMailAsync(mailMessage);
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

没有变化很简单,不需要为 smtpclient

设置凭据