63

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?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
user1144596
  • 2,068
  • 8
  • 36
  • 56

7 Answers7

61

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.

Kevin Montrose
  • 22,191
  • 9
  • 88
  • 137
David Conlisk
  • 3,387
  • 4
  • 33
  • 39
  • And when setting it up causes this error "he market on the billing account does not match the market on the user account", here is how to solve it https://social.msdn.microsoft.com/Forums/azure/en-US/b7e65da4-76c5-46e5-bb2e-e40b16e882ea/cant-subscribe-to-the-scheduler-addon-due-to-an-error-the-market-on-the-billing-account-does-not?forum=DataMarket – Stoyan Dimov May 09 '16 at 18:17
  • 17
    I can't recommend SendGrid. Setting up an account took days. SendGrid wouldn't validate our DNS records. Support figure it out only after repeated requests, and recomended to disable default functionalities of our account to bypass the problem. Same story 3 months later when we had to migrate to a separate account due to unreliable integration with Azure Additionally, the .net API is confusing and lacks transparency in many ways. Finally, today is the 3rd day ALL our emails are deferred because of "changes to our infrastructure that MAY throttle SOME of your emails" . We're fully in the mud! – BernardV Jul 23 '16 at 15:07
  • 2
    @BernardV, I got to agree, that creating an account from `Azure` was not a smooth experience for me too. Yet, I used `SendGrid API` together with `Azure Scheduler Job` and it worked for me perfectly. – Gabrielius Aug 03 '16 at 13:16
  • 1
    See answer by @viperguynaz below, just use it as you would a third party smtp. worked like a charm. – Antonio Nicolaas Teyken Feb 28 '17 at 10:35
  • Down to 3000 free emails a month – jjxtra Feb 06 '20 at 03:18
  • I can't seem to get access to the API keys. The interface looks different (and more limited) than any examples I see. – VSO Feb 13 '20 at 20:30
  • How secure is sendgrid? do they store email address and contents? – kudlatiger Mar 08 '21 at 06:28
  • 1
    It's only with USD 9 subscription. – A.Sideris Mar 17 '21 at 15:41
  • No free sendgrid subscription for azure – leo Martin Jul 18 '21 at 12:38
  • Sendgrid are awful, do yourself a favour and use Postmark. – Chris Butler Jul 22 '21 at 21:24
  • Used to be a great tip, but is no longer relevant. The new SendGrid free-tier cap is only 100 emails a month... – rothschild86 Oct 04 '21 at 14:22
19

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

john blair
  • 466
  • 5
  • 13
15

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("info@YourWebSiteDomain.com"); 
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

Red
  • 3,030
  • 3
  • 22
  • 39
viperguynaz
  • 12,044
  • 4
  • 30
  • 41
  • 1
    correct, so that means i need to have a SMTP service provider? Azure has nothing built into it? Will the third part approach work for receiving email? – user1144596 Jul 16 '13 at 01:46
  • 3
    Correct - Azure does not have SMTP enabled. You have to use a 3-party to send. Receiving is up to you. You can receive emails at whatever domain email service you have setup - again Azure isn't involved. You have to have DNS MX records setup to handle receiving email thorough a provider like GMail. – viperguynaz Jul 16 '13 at 02:03
  • This worked for me, registered for SendGrid and used the above with their smtp and the login i created. Only thing to remember is to open the ports. – Antonio Nicolaas Teyken Feb 28 '17 at 10:33
8

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!

Saif Asad
  • 779
  • 10
  • 8
  • 2
    This is tricky for Enterprise accounts as [the docs](https://learn.microsoft.com/en-us/azure/bot-service/bot-service-channel-connect-email?view=azure-bot-service-4.0) say it's best to use a dedicated account which also has to have MFA disabled. So basically, you can't use your individually allocated Enterprise account and if you get another one created just for this, you need to also turn MFA off - some hoops to jump through with your IT department. – J_L Apr 15 '20 at 20:37
  • If MFA cannot be disabled (for security reasons in Enterprise accounts of course) why don't use App Password instead? – Ivan Paniagua Aug 03 '21 at 13:46
6

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.

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
grebett
  • 467
  • 4
  • 10
6

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>
Steve Danner
  • 21,818
  • 7
  • 41
  • 51
4

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("emailalias@contoso.com") { DisplayName = "Friendly Display Name" }};
EmailRecipients emailRecipients = new EmailRecipients(emailAddresses);
EmailMessage emailMessage = new EmailMessage("donotreply@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azurecomm.net", 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.

Matt Sykes
  • 41
  • 3
  • Any idea how to read an email sent to a email communication resource? Like if I register my domain and sendfrom like support@mydomain through azure, how do I view emails sent to support@mydomain? or can I only send from that address? – Michael G Aug 10 '23 at 05:51