Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMTP sample for email services #138

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions SendEmailSMTP/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
page_type: sample
languages:
- csharp
products:
- azure
- azure-communication-services
---

# Email Sample

This sample sends an email to the selected recipients of any domain using an [Email Communication Services resource](https://docs.microsoft.com/azure/communication-services/quickstarts/email/create-email-communication-resource) with SMTP.

Additional documentation for this sample can be found on [Microsoft Docs](https://learn.microsoft.com/en-us/azure/communication-services/quickstarts/email/send-email-smtp/send-email-smtp?pivots=smtp-method-smtpclient).

## Prerequisites

- Create an Azure account with an active subscription. For details, see [Create an account for free](https://azure.microsoft.com/free/).
- [Visual Studio (2019 and above)](https://visualstudio.microsoft.com/vs/).
- [.NET 6.0](https://dotnet.microsoft.com/download/dotnet/6.0) (Make sure to install version that corresponds with your Visual Studio instance, 32 vs 64 bit).
- Create an Azure Communication Services resource. For details, see [Create an Azure Communication Resource](https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource).
- Create an [Azure Email Communication Services resource](https://docs.microsoft.com/azure/communication-services/quickstarts/email/create-email-communication-resource) to start sending emails.

> Note: We can send an email from our own verified domain also [Add custom verified domains to Email Communication Service](https://docs.microsoft.com/azure/communication-services/quickstarts/email/add-custom-verified-domains).

## Code structure

- ./SendEmailSMTP/SendEmailSMTP/Program.cs: Entry point for sending email.

## Before running the sample for the first time

1. Open an instance of PowerShell, Windows Terminal, Command Prompt or equivalent program and navigate to the directory that you'd like to clone the sample to.
2. `git clone https://github.com/Azure-Samples/communication-services-dotnet-quickstarts.git`

### Locally configuring the application

1. Navigate to the SendEmailSMTP folder and open the `SendEmailSMTP.sln` solution in Visual Studio.
2. Open the program.cs file to configure the following settings:

- ` smtpAuthUsername`: Replace `<Azure Communication Services Resource name>|<Entra Application Id>|<Entra Application Tenant Id>` as mentioned. For more details please refer:[Microsoft Docs](https://learn.microsoft.com/en-us/azure/communication-services/quickstarts/email/send-email-smtp/smtp-authentication).
- ` smtpAuthPassword`: Replace `<Entra Application Client Secret>` which is your Microsoft Entra Application Secret.
- `sender`: Replace `<SENDER_EMAIL>` with the sender email obtained from Azure Communication Service.
- `recipient`: Replace `<RECIPIENT_EMAIL>` with the recipient email.
- The body and message can be added as per your wish. The parameters such as hostname, port, should be kept intact.

1. Run `SendEmail` project.
25 changes: 25 additions & 0 deletions SendEmailSMTP/SendEmailSMTP.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34408.163
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SendEmailSMTP", "SendEmailSMTP\SendEmailSMTP.csproj", "{06B928F5-436F-4BE5-904A-80D94C9E02A9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{06B928F5-436F-4BE5-904A-80D94C9E02A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06B928F5-436F-4BE5-904A-80D94C9E02A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06B928F5-436F-4BE5-904A-80D94C9E02A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06B928F5-436F-4BE5-904A-80D94C9E02A9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {79FCEDE6-9F1D-41A9-A8E8-7C3C4FC67EE2}
EndGlobalSection
EndGlobal
39 changes: 39 additions & 0 deletions SendEmailSMTP/SendEmailSMTP/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Net;
using System.Net.Mail;


namespace SendEmailSMTP
{
class Program
{
static void Main(String[] args)
{
string smtpAuthUsername = "<Azure Communication Services Resource name>|<Entra Application Id>|<Entra Application Tenant Id>";
string smtpAuthPassword = "<Entra Application Client Secret>";
string sender = "[email protected]";
string recipient = "[email protected]";
string subject = "Welcome to Azure Communication Service Email SMTP";
string body = "This email message is sent from Azure Communication Service Email using SMTP.";

string smtpHostUrl = "smtp.azurecomm.net";
var client = new SmtpClient(smtpHostUrl)
{
Port = 587,
Credentials = new NetworkCredential(smtpAuthUsername, smtpAuthPassword),
EnableSsl = true
};

var message = new MailMessage(sender, recipient, subject, body);

try
{
client.Send(message);
Console.WriteLine("The email was successfully sent using Smtp.");
}
catch (Exception ex)
{
Console.WriteLine($"Smtp failed the the exception: {ex.Message}.");
}
}
}
}
10 changes: 10 additions & 0 deletions SendEmailSMTP/SendEmailSMTP/SendEmailSMTP.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>