user1144596
user1144596

Reputation: 2088

Sending email from Azure

Hope someone can help. I want to send email from my Azure account. My domain name is configured to work with Azure.

I could not find easily on the web how to send an email from an Azure account. There was some mention of SendGrid, but it seems my account does not support it.

Can someone please guide me through how to send email from a website hosted in Azure?

Upvotes: 63

Views: 133653

Answers (7)

john blair
john blair

Reputation: 536

I would never recommend SendGrid. I took up their free account offer and never managed to send a single email - all got blocked - I spent days trying to resolve it. When I enquired why they got blocked, they told me that free accounts share an ip address and if any account abuses that ip by sending spam - then everyone on the shared ip address gets blocked - totally useless. Also if you use them - do not store your email key in a git public repository as anyone can read the key from there (using a crawler) and use your chargeable account to send bulk emails.

A free email service which I've been using reliably with an Azure website is to use my Gmail (Google mail) account. That account has an option for using it with applications - once you enable that, then email can be sent from your azure website. Pasting in sample send code as the port to use (587) is not obvious.

  public static void SendMail(MailMessage Message)
    {
        SmtpClient client = new SmtpClient();
        client.Host = EnvironmentSecret.Instance.SmtpHost; // smtp.googlemail.com
        client.Port = 587;
        client.UseDefaultCredentials = false;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.EnableSsl = true;
        client.Credentials = new NetworkCredential(
            EnvironmentSecret.Instance.NetworkCredentialUserName,
            EnvironmentSecret.Instance.NetworkCredentialPassword);
        client.Send(Message);
    }

UPDATE: Google no longer let you use your google account name and password for sending e-mail. Instead you need to now switch on 2-factor authentication and create an app password and use that password in your app with your google account. Here is a step by step guide: youtube.com/watch?v=Y_u5KIeXiVI

Upvotes: 19

Matt Sykes
Matt Sykes

Reputation: 41

For anyone reading this in 2022, Microsoft now have the Azure Communication Service in public preview, which when combined with the Azure Email Communication Service, will allow for a nicer solution to emailing through an SDK.

A quick start guide is available here and is pretty straight forward:

https://learn.microsoft.com/en-us/azure/communication-services/quickstarts/email/send-email?pivots=programming-language-csharp

EmailClient emailClient = new EmailClient(connectionString);
EmailContent emailContent = new EmailContent("Welcome to Azure 
Communication Service Email APIs.");
emailContent.PlainText = "This email message is sent from Azure Communication Service Email using .NET SDK.";
List<EmailAddress> emailAddresses = new List<EmailAddress> { new EmailAddress("[email protected]") { DisplayName = "Friendly Display Name" }};
EmailRecipients emailRecipients = new EmailRecipients(emailAddresses);
EmailMessage emailMessage = new EmailMessage("[email protected]", emailContent, emailRecipients);
SendEmailResult emailResult = emailClient.Send(emailMessage,CancellationToken.None);

The service also allows authentication through connection string and Azure AD. It also allows for custom domain validation and sending.

Upvotes: 4

viperguynaz
viperguynaz

Reputation: 12174

Sending from a third party SMTP isn't restricted by or specific to Azure. Using System.Net.Mail, create your message, configure your SMTP client, send the email:

// create the message
var msg = new MailMessage();
msg.From = new MailAddress("[email protected]"); 
msg.To.Add(strTo); 
msg.Subject = strSubject; 
msg.IsBodyHtml = true; 
msg.Body = strMessage;

// configure the smtp server
var smtp = new SmtpClient("YourSMTPServer")
{
    Credentials = new System.Net.NetworkCredential("YourSMTPServerUserName", "YourSMTPServerPassword")
};

// send the message
smtp.Send(msg); 

UPDATE: I added a post on Medium about how to do this with an Azure Function - https://medium.com/@viperguynaz/building-a-serverless-contact-form-f8f0bff46ba9

Upvotes: 15

Saif Asad
Saif Asad

Reputation: 819

A nice way to achieve this "if you have an office 365 account" is to use Office 365 outlook connector integrated with Azure Logic App,

Hope this helps someone!

Upvotes: 8

Steve Danner
Steve Danner

Reputation: 22148

For people wanting to use the built-in .NET SmtpClient rather than the SendGrid client library (not sure if that was the OP's intent), I couldn't get it to work unless I used apikey as my username and the api key itself as the password as outlined here.

<mailSettings>
    <smtp>
        <network host="smtp.sendgrid.net" port="587" userName="apikey" password="<your key goes here>" />
    </smtp>
</mailSettings>

Upvotes: 7

grebett
grebett

Reputation: 467

If you're looking for some ESP alternatives, you should have a look at Mailjet for Microsoft Azure too! As a global email service and infrastructure provider, they enable you to send, deliver and track transactional and marketing emails via their APIs, SMTP Relay or UI all from one single platform, thought both for developers and emails owners.

Disclaimer: I’m working at Mailjet as a Developer Evangelist.

Upvotes: 6

David Conlisk
David Conlisk

Reputation: 3492

I know this is an old post but I've just signed up for Azure and I get 25,000 emails a month for free via SendGrid. These instructions are excellent, I was up and running in minutes:

How to Send Email Using SendGrid with Azure

Azure customers can unlock 25,000 free emails each month.

Upvotes: 61

Related Questions