Usama Kiyani
Usama Kiyani

Reputation: 178

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

How to setup my SMTP client in .NET Core 2.1 without credentials. My mail server is within the network.

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;
    }
}

Upvotes: 0

Views: 302

Answers (1)

Usama Kiyani
Usama Kiyani

Reputation: 178

There is no change its simple no need to set credentials to the smtpclient

Upvotes: 1

Related Questions