I’ve tried to deploy it as a webJob running in an App Service but I can’t listen on the SMTP port (587). I don’t think it’s possible to open a socket from within an appService/webJob. Is it?
Azure WebJobs run in a sandboxed environment within Azure App Services, which only allows HTTP/HTTPS traffic on specific ports. This means you can’t open a custom socket, like port 587 for SMTP, to receive external traffic.
By containerizing your worker service, you can open and expose any port you need, including port 587 for SMTP. Follow these steps:
- Create a .NET Worker Service using Docker and pushed the Docker image to Azure Container Registry (ACR). Deploy the container to Azure Container Instance (ACI) and expose port 587.
I created a .NET Worker Service project, built a .NET 9.0 application, and created a Docker file .NET 9.0 SDK.
Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -o /app
FROM mcr.microsoft.com/dotnet/runtime:9.0
WORKDIR /app
COPY --from=build /app .
EXPOSE 587
ENTRYPOINT ["dotnet", "MySmtpWorkerService.dll"]
I have built the Docker image and tested it using the command below.
docker build -t my-smtp-image:latest .
docker run -p 587:587 my-smtp-image:latest
After build, push the image to Azure Container Registry (ACR) using the below command.
az acr create --resource-group MyResourceGroup --name YourACRName --sku Basic
az acr login --name YourACRName
docker tag my-smtp-image:latest youracrname.azurecr.io/my-smtp-image:latest
docker push youracrname.azurecr.io/my-smtp-image:latest
Deploy it to Azure Container Instance (ACI) using the following command.
az container create --resource-group MyResourceGroup --name mysmtpcontainer --image youracrname.azurecr.io/my-smtp-image:latest --registry-login-server youracrname.azurecr.io --registry-username <Username> --registry-password <password> --ports 587 --os-type Linux --cpu 1 --memory 2 --dns-name-label my-smtp-server
You will find the username and password from ACR

And then check the deployment status using the following command.
az container show --resource-group <Resource group nmae> --name mysmtpcontainer --output table
To test connectivity from an external machine, open a command prompt or terminal and run below command.
telnet my-smtp-server.eastus.azurecontainer.io 587
To enable Telnet:
- Open an Command Prompt (Run as Administrator).
- Execute the following command:
dism /online /Enable-Feature /FeatureName:TelnetClient
Result:
