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

时间:2018-12-20 11:39:17

标签: c# .net-core-2.1

如何在没有凭据的情况下在.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;
        }

    }

1 个答案:

答案 0 :(得分:1)

没有任何变化,它很简单,无需将凭据设置为smtpclient