I have a .net backend with react on frontend, when i click on a button from client side it should trigger an api that will put data in the db and then send an email. On local host everything works fine, i have followed the documentation from microsoft to configure azure. It has also a custom domain, but on the deployed web app with azure devops, it doesn’t want to send it, and i don’t understand what i miss and how can i fix it.
This is a part of my email service, and as i said, on local host works just fine.
public EmailService(IConfiguration configuration)
{
var endpoint = configuration["EmailService:CommunicationServicesEndpoint"];
var tenantId = configuration["EmailService:TenantId"];
var clientId = configuration["EmailService:ClientId"];
var clientSecret = configuration["EmailService:ClientSecret"];
_emailSettings = configuration.GetSection("EmailService:AccountingDepartment").Get<EmailSettings>()!;
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
_emailClient = new EmailClient(new Uri($"https://{endpoint}.communication.azure.com/"), credential);
}
public async Task SendEmailAsync(
string subject,
string htmlContent,
List<string> toRecipients,
List<string> ccRecipients,
List<string> bccRecipients,
string senderAddress,
List<string> replyToAddresses)
{
var emailContent = new EmailContent(subject)
{
Html = htmlContent
};
var toEmailAddresses = toRecipients.Where(email => !string.IsNullOrWhiteSpace(email)).Select(email => new EmailAddress(email)).ToList();
var ccEmailAddresses = ccRecipients?.Where(email => !string.IsNullOrWhiteSpace(email)).Select(email => new EmailAddress(email)).ToList() ?? [];
var bccEmailAddresses = bccRecipients?.Where(email => !string.IsNullOrWhiteSpace(email)).Select(email => new EmailAddress(email)).ToList() ?? [];
var emailRecipients = new EmailRecipients(toEmailAddresses, ccEmailAddresses, bccEmailAddresses);
var emailMessage = new EmailMessage(senderAddress, emailRecipients, emailContent);
if (replyToAddresses != null)
{
foreach (var replyToAddress in replyToAddresses.Where(email => !string.IsNullOrWhiteSpace(email)))
{
emailMessage.ReplyTo.Add(new EmailAddress(replyToAddress));
}
}
try
{
var emailSendOperation = await _emailClient.SendAsync(WaitUntil.Completed, emailMessage);
string operationId = emailSendOperation.Id;
}
catch (RequestFailedException ex)
{
throw new Exception("Failed to send email.", ex);
}
}
To change environment variables, to restart the app, change the build configuration.