0

I have a custom SMTP server that I need to deploy to Azure and call from outside of Azure.

This is a .Net worker service.

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?

I could do it with a VM, but I am trying to avoid that if possible.

How can I open a socket in a webJob, and receive traffic from outside of Azure?

If I can't, what is another option for running the worker service?

2
  • Please share your error message. Commented Feb 16 at 8:49
  • You're right, opening a socket in an Azure App Service/WebJob to listen on the SMTP port (587) isn't possible due to the restrictions on opening sockets. cicoria.com/troubleshooting-toolazure-webjob-tcp-ping. Your best bet would be using a VM Commented Feb 18 at 15:42

1 Answer 1

1

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

enter image description here

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.