amartin
amartin

Reputation: 350

How do you access .NET Framework default SMTP service in Azure Functions? Using Postal in Azure Function

We are moving certain background methods that send emails to Azure functions. Our emails are currently built and sent using Postal

Postal uses .NET Framework’s built-in SmtpClient, which has connection details defined in a web.config file for our MVC web app, which doesn't exist in an Azure Function.

Is there a way to use Postal in an Azure Function?

Upvotes: 0

Views: 68

Answers (1)

mason
mason

Reputation: 32693

If you look at the source code of the project on GitHub, you'll see that Postal's EmailServer takes in a Func. If you don't provide it, it creates an SmtpClient by executing the new SmtpClient() constructor, which pulls configuration in from the .config file. So if you want avoid using a .config file to get the settings, pass in a Func.

public SmtpClient CreateMySmtpClient()
{
    var client = new SmtpClient("mysmtpserver.com");
    // set credentials, timeouts etc
    return client;
}

public void SomeOtherMethod()
{
    var emailService = new EmailService(ViewEngines.Engines, CreateMySmtpClient);
    dynamic email = new Email("Test");
    email.Message = "Hello,  world!";
    emailService.Send(email);
}

Upvotes: 1

Related Questions