NiallMitch14
NiallMitch14

Reputation: 1229

SMTP Email Works locally but not on Devops

I have a ASP.Net core project with an Account Controller to handle logins and account functionality for user profiles. In this controller I have a function to handle an email if a user forgets their password via SMTP.

[HttpPost, Route("resetPasswordLink"), AllowAnonymous]
public async Task<IActionResult> sendResetPasswordLink([Required] string email)
{
    var dbUser = await UserManager.FindByNameAsync(email);
    if (dbUser != null)
    {
        var someURL = 'My URL Here'
        var token = await UserManager.GeneratePasswordResetTokenAsync(dbUser);
        var fromAddress = ConfigurationManager.AppSettings["SMTPFromAddress"];
        var mailServer = ConfigurationManager.AppSettings["SMTPServer"];

        var mail = new MailMessage();

        mail.From = new MailAddress(fromAddress);
        mail.To.Add(email);

        mail.Subject = "Password Reset";
        mail.IsBodyHtml = true;
        mail.Body += "<p>You have requested to change your password</p>";
        mail.Body += "<p></p>";
        mail.Body += "<p>You can reset your password by clicking <a href="+someURL+"/passwordReset/"+token+">here</a></p>";

        var smtp = new SmtpClient();
        smtp.Host = mailServer;
        try
        {
            smtp.Send(mail);
            return Ok("Email Sent");
        } catch(Exception ex)
        {
            return BadRequest("Email generated but failed to send " + ex);
        }
    } else
    {
        return BadRequest("Failed to send link");
    }
}

With my app.config containing my SMPTP configuration. When this is ran locally, the code works perfectly fine and the email is sent to an existing email on the system. However when this is published to Azure Devops, I get this error:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.Sockets.SocketException: No such host is known

I assume this is to do with Devops hosting the .NET code on an external machine. Does anyone know how to resolve this issue?

Upvotes: 0

Views: 593

Answers (1)

Markus Meyer
Markus Meyer

Reputation: 3937

The configuration values are set correctly.

This might be a network issue:

  • Firewall setting
  • Internal network (SMTP-Server) vs. External network (App)

Upvotes: 1

Related Questions