I have a class named BrevoEmailSender which functions to send transactional email using the Brevo (previously named Sendinblue) See Here..
using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Identity.UI.Services;
using Newtonsoft.Json.Linq;
using sib_api_v3_sdk.Api;
using sib_api_v3_sdk.Client;
using sib_api_v3_sdk.Model;
namespace XYZproject.Services
{
public class BrevoEmailSender
{
public void main()
{
Configuration.Default.ApiKey.Add("api-key", "xkeysib-xxx");
var apiInstance = new TransactionalEmailsApi();
string SenderName = "J";
string SenderEmail = "[email protected]";
SendSmtpEmailSender Email = new SendSmtpEmailSender(SenderName, SenderEmail);
string ToEmail = "[email protected]";
string ToName = "W";
SendSmtpEmailTo smtpEmailTo = new SendSmtpEmailTo(ToEmail, ToName);
List<SendSmtpEmailTo> To = new List<SendSmtpEmailTo>();
To.Add(smtpEmailTo);
string BccName = "J";
string BccEmail = "[email protected]";
SendSmtpEmailBcc BccData = new SendSmtpEmailBcc(BccEmail, BccName);
List<SendSmtpEmailBcc> Bcc = new List<SendSmtpEmailBcc>();
Bcc.Add(BccData);
string CcName = "J";
string CcEmail = "[email protected]";
SendSmtpEmailCc CcData = new SendSmtpEmailCc(CcEmail, CcName);
List<SendSmtpEmailCc> Cc = new List<SendSmtpEmailCc>();
Cc.Add(CcData);
string HtmlContent = "<html><body><h1>This is my first transactional email {{params.parameter}}</h1> Try link <a href=google.com>a</a> </body></html>";
string TextContent = null;
string Subject = "My {{params.subject}}";
string ReplyToName = "J";
string ReplyToEmail = "j";
SendSmtpEmailReplyTo ReplyTo = new SendSmtpEmailReplyTo(ReplyToEmail, ReplyToName);
string AttachmentUrl = null;
string stringInBase64 = "aGVsbG8gdGhpcyBpcyB0ZXN0";
byte[] Content = System.Convert.FromBase64String(stringInBase64);
string AttachmentName = "test.txt";
SendSmtpEmailAttachment AttachmentContent = new SendSmtpEmailAttachment(AttachmentUrl, Content, AttachmentName);
List<SendSmtpEmailAttachment> Attachment = new List<SendSmtpEmailAttachment>();
Attachment.Add(AttachmentContent);
JObject Headers = new JObject();
Headers.Add("Some-Custom-Name", "unique-id-1234");
long? TemplateId = null;
JObject Params = new JObject();
Params.Add("parameter", "Some Parameter Here");
Params.Add("subject", "Title Of Email");
List<string> Tags = new List<string>();
Tags.Add("mytag");
SendSmtpEmailTo1 smtpEmailTo1 = new SendSmtpEmailTo1(ToEmail, ToName);
List<SendSmtpEmailTo1> To1 = new List<SendSmtpEmailTo1>();
To1.Add(smtpEmailTo1);
Dictionary<string, object> _parmas = new Dictionary<string, object>();
_parmas.Add("params", Params);
SendSmtpEmailReplyTo1 ReplyTo1 = new SendSmtpEmailReplyTo1(ReplyToEmail, ReplyToName);
SendSmtpEmailMessageVersions messageVersion = new SendSmtpEmailMessageVersions(To1, _parmas, null, Cc, ReplyTo1, Subject);
List<SendSmtpEmailMessageVersions> messageVersiopns = new List<SendSmtpEmailMessageVersions>();
messageVersiopns.Add(messageVersion);
try
{
var sendSmtpEmail = new SendSmtpEmail(Email, To, Bcc, Cc, HtmlContent, TextContent, Subject, ReplyTo, Attachment, Headers, TemplateId, Params, messageVersiopns, Tags);
CreateSmtpEmail result = apiInstance.SendTransacEmail(sendSmtpEmail);
Debug.WriteLine(result.ToJson());
Console.WriteLine(result.ToJson());
Console.ReadLine();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
}
In my Program.cs, I have this line:
builder.Services.AddTransient<BrevoEmailSender, BrevoEmailSender>();
In my controller, I call 'Main' like this:
private readonly BrevoEmailSender _brevoEmailSender;
public XyzController(BrevoEmailSender brevoEmailSender)
{
_brevoEmailSender = brevoEmailSender;
}
[HttpPost]
public IActionResult Index(string? submitButton = "")
{
if (submitButton == "submit")
{
_brevoEmailSender.main();
}
return View();
}
When I run this, the email was send. The problem is, I have this endless thread running. It keeps running on the line ' _brevoEmailSender.main(); ' in my controller. Here is the thread.
{
"messageIds": [
"<[email protected]>"
]
}
The thread 0x2cf0 has exited with code 0 (0x0).
The thread 0x50d4 has exited with code 0 (0x0).
The thread 0x4a0 has exited with code 0 (0x0).
The thread 0x147c has exited with code 0 (0x0).
The thread 0x1a00 has exited with code 0 (0x0).
The thread 0x53a4 has exited with code 0 (0x0).
The thread 0x4978 has exited with code 0 (0x0).
The thread 0x4fa8 has exited with code 0 (0x0).
The thread 0x1198 has exited with code 0 (0x0).
.
.
.
I read in many solutions here that this thread is not a problem, but in my case it is, as it goes on endlessly. Any idea how to solve this?