If the application needs to send reliable emails, it might be necessary to set up an SMTP relay in Azure. Azure does not have an SMTP server directly out of the box but can be configured through services like Azure Logic Apps, Azure Functions, or by using third-party SMTP providers. This blog will walk you through the process of how to set up an SMTP relay in Azure through all the above methods.

Why Use SMTP Relay in Azure?

  • Reliable Email Delivery: Using a trusted SMTP relay for sending emails will increase deliverability and decrease the likelihood of being marked as spam.
  • Cost-Effective: You can save money by using Azure resources instead of using other email services.
  • Integration: You can easily integrate your email capabilities with other Azure services and applications.

Method 1: Using Azure Logic Apps

You can automate workflows as well as send emails with much ease using Azure Logic Apps. Here is how this is done:

Step 1: Create a Logic App

  1. Sign in to the Azure portal: Use your browser. Go to portal.azure.com then sign in to your account.
  2. New Logic App: Once you get to the top menu, click on “Create resource.” Then start searching for “Logic app,” then select it once you find it. Then “Create.”

Basic Information Creation

  • Name: Give your chosen name for your Logic app.
  • Subscription: Choose from your list of available Microsoft Azure subscriptions.
  • Resource Group: Create a new or use an existing resource group.
  • Location: Select your Logic App location.
  • Review and Create: Click Review + create, then Create after validation.

Step 2: Configure the Logic App to Send Email

  1. Open the Logic App Designer: Once you have created the Logic App, navigate to the Logic App and click on Logic App Designer.
  2. Choose a Trigger: Pick a trigger that will begin your workflow. You might use Recurrence to send emails at specific times or HTTP Request to send emails based on an API call.
  3. Add an Action: After you have chosen a trigger, click on New step.
  4. Search for Email Connector: Search for and select an email service. This might be Office 365 Outlook, SendGrid, or SMTP if you have a specific SMTP server configured.
  5. Configure Email Settings: Fill in the fields below:
    • To: The recipient’s email address.
    • Subject: The email subject line.
    • Body: The content of the email.
  6. Save the Logic App: Click on Save to complete your Logic App setup.

Step 3: Test the Logic App

  1. Run the Logic App: Activate your Logic App through the trigger you have set up (for example, an HTTP request).
  2. Check Email Delivery: Confirm that the email has been delivered to the email address specified.

Method 2: Azure Functions with SMTP

Use this method to create a serverless email application with full control, if required.

Step 1: Create an Azure Function

  1. Sign in to Azure Portal: Navigate to portal.azure.com.
  2. Create a new Function App: Click Create a resource, search for Function App, and choose it. Click Create.

Fill in Basic Information:

  • Subscription: Select an Azure subscription.
  • Resource Group: New or pre-existing resource group.
  • App Name: Choose a new name.
  • Runtime Stack: .NET, Node.js or Python.
  • Region: Your favourite region for the Function App.
  • Review and Create: Select review + create and create the Function App.

Step 2: Code your function

In the newly generated Function App, you would select “Functions” within that.

  1. Create a new function: Click on + Add and select a template, like HTTP trigger.
  2. Implement SMTP sending logic: Use the programming language you picked to write code for how to send emails via SMTP. Below is a simple Python example using the smtplib library:
python
import smtplib
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
smtp_server = "smtp.yourdomain.com"
port = 587
sender_email = "[email protected]"
receiver_email = "[email protected]"
password = "your-email-password"
subject = "Test Email"
body = "This is a test email sent from Azure Functions."

message = f"""\
Subject: {subject}\n
{body}
"""

try:
with smtplib.SMTP(smtp_server, port) as server:
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
return func.HttpResponse("Email sent successfully!", status_code=200)
except Exception as e:
return func.HttpResponse(f"Failed to send email: {str(e)}", status_code=500)

Step 3: Test the Azure Function

  1. Invoke the Function: Use the URL provided to send yourself a test email through the function.
  2. Check that the email is received in the inbox address.

Method 3: Third Party SMTP

Use a third-party SMTP like SendGrid, Mailgun, or Amazon SES, especially if you prefer to make your setup much simpler. It supports Azure out-of-the-box and can easily integrate into your system.

Steps:

  1. Register Third-Party SMTP: Account and Set up the Domain.
  2. Get SMTP Settings: Take down the SMTP server, port, username, and password.
  3. Integrate with Azure: Use Azure Functions or Logic Apps to send emails using the SMTP credentials from your chosen service.

Conclusion

You can set up a pretty simple SMTP relay that enhances the email functionality of applications both from within Logic Apps and Azure Functions or from some other SMTP provider. Of these options, there will certainly be something that meets your needs: the strategy outlined above would do an excellent job of helping deliver emails from your Azure applications in the right way.

FAQs

  1. Is there a cost of using SMTP relay in Azure? While Azure Functions or Logic Apps will incur charges based on usage, you will incur costs based on your chosen plan if using a third-party SMTP service.
  2. What do I do if my emails get flagged as spam? Ensure best practices in email delivery, like proper authentication (SPF, DKIM), clean list, and relevant content are used.

Using SMTP relay in Azure simplifies email processes and improves organizational communication efficiency.

Leave A Comment

more similar articles