Fluent API to send emails in a .NET Core application using your own SMTP settings
$ dotnet add package FluentEmailer.LJSholeFluent API to send emails in a .NET Core application using your own SMTP settings
This change addresses an issue with setting the Bcc and Cc email addresses before this change. These were added onto the To email list.
This change also includes a change to the .FromMailAddresses method; since an email can only originate from one email address, this method should be named accordingly i.e in singular form not plural.
_fluentEmail
.UsingSMTPServer(hostName, portNumber, userName, password, sslRequired)
.Message("Email Subject", new List<MailAddress> { new(toEmail) }, new(userName, "Email Display Name"))
.FromMailAddress(new MailAddress(***userName***))
.Body("Test body content")
.Send();
Future versions of the application / package will provide support for SendGrid, with this in mind, I have introduced breaking changes which pave way for the coming support. In addition, the change aims to make using the package much more intuitive and seemless. We do still support dependency injection. Simply add sample code below in your program.cs and import the FluentEmailer.LJShole namespace in the program.cs file.
builder.Services.AddFluentEmailer()
This will allow you to inject the IFluentEmail interface which you can use your code.
_fluentEmail
.UsingSMTPServer(hostName, portNumber, userName, password, sslRequired)
.Message("Email Subject", new List<MailAddress> { new(toEmail) }, new(userName, "Email Display Name"))
.FromMailAddresses(new MailAddress(***userName***))
.CcMailAddresses([new MailAddress(***ccEmail***)])
.BccMailAddresses([new MailAddress(***bccEmail***)])
.AddAttachments([new($"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SamplePDF.pdf")}")])
.Body("Test body content")
.Send();
var templateValues = new Dictionary<string, string> { { "{{subject}}", "Testing Message" }, { "{{body}}", "<section><h2>This is</h2><p>Welcome to our world</p></section>" } };
_fluentEmail
.UsingSMTPServer(hostName, portNumber, userName, password, sslRequired)
.Message("Email Subject", new List<MailAddress> { new(toEmail) }, new(userName, "Email Display Name"))
.FromMailAddresses(new MailAddress(***userName***))
.CcMailAddresses([new MailAddress(***ccEmail***)])
.BccMailAddresses([new MailAddress(***bccEmail***)])
.AddAttachments([new($"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SamplePDF.pdf")}")])
.Body(templateValues, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestHtmlPage.html"))
.Send();
new Mailer()
.Message()
.WithSubject("Email Subject")
.AddFromMailAddresses(new MailAddress(userName))
.AddToMailAddresses(new List<MailAddress> { new MailAddress(toEmail) })
.WithBody()
.UsingString("Email body as string")
.SetBodyIsHtmlFlag()
.WithCredentials()
.UsingHostServer(hostName)
.OnPortNumber(portNumber)
.WithUserName(userName)
.WithPassword(password)
.Send();
hostName The SMPT/IMAP server you'll be using to send emails from. <br/> portNumber The port number used by the hostName<br/> userName The email address you'll be using to send emails from as configured on *hostName. <br/> password The password used to log in when accessing the email address (userName)<br/>
new Mailer()
.Message()
.WithSubject("Email Subject")
.AddFromMailAddresses(new MailAddress(userName))
.AddToMailAddresses(new List<MailAddress> { new MailAddress(***toEmail***) })
.AddCcMailAddresses(new List<MailAddress> { new MailAddress(***ccEmail***) })
.AddBccMailAddresses(new List<MailAddress> { new MailAddress(***bccEmail***) })
.WithBody()
.UsingEmailTemplate($"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"TestHtmlPage.html")}")
.UsingTemplateDictionary(new Dictionary<string, string> { { "{{subject}}","Testing Message"}, { "{{body}}", "<section><h2>This is</h2><p>Welcome to our world</p></section>" } })
.CompileTemplate()
.SetBodyIsHtmlFlag()
.WithCredentials()
.UsingHostServer(hostName)
.OnPortNumber(portNumber)
.WithUserName(userName)
.WithPassword(password)
.Send();
This usage requires a Dictionary<string,string> where the keys in the dictionary represent placeholders in the template to be replaced by their respective values.<br/>
new Mailer()
.Message()
.WithSubject("Mail Subject Template")
.AddFromMailAddresses(new MailAddress(userName,"Fluent Email"))
.AddToMailAddresses(new List<MailAddress> { new MailAddress(***toEmail***) })
.AddCcMailAddresses(new List<MailAddress> { new MailAddress(***ccEmail***) })
.AddBccMailAddresses(new List<MailAddress> { new MailAddress(***bccEmail***) })
.WithAttachments(new List<Attachment> { new Attachment($"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SamplePDF.pdf")}") })
.WithBody()
.UsingEmailTemplate($"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"TestHtmlPage.html")}")
.UsingTemplateDictionary(new Dictionary<string, string> { { "{{subject}}","Testing Message"}, { "{{body}}", "<section><h2>This is</h2><p>Welcome to our world</p></section>" } })
.CompileTemplate()
.SetBodyIsHtmlFlag()
.WithCredentials()
.UsingHostServer(hostName)
.OnPortNumber(portNumber)
.WithUserName(userName)
.WithPassword(password)
.Send();
new Mailer()
.SetUpMessage()
.WithSubject("Mail Subject")
.AddFromMailAddresses(new MailAddress(userName, "Fluent Email - No Attachments - With ReplyTo"))
.AddToMailAddresses(new List<MailAddress> { new MailAddress(toEmail) })
.SetSubjectEncoding(Encoding.UTF8)
.WithReplyTo(new MailAddress("info@creativemode.co.za"))
.SetUpBody()
.SetBodyEncoding(Encoding.UTF8)
.SetBodyTransferEncoding(TransferEncoding.Unknown)
.Body()
.UsingString("This is me Testing")
.SetBodyIsHtmlFlag()
.SetPriority(MailPriority.Normal)
.WithCredentials()
.UsingHostServer(hostName)
.OnPortNumber(portNumber)
.WithUserName(userName)
.WithPassword(password)
.Send();