Skip to content

1.Emails

Lucas Eschechola edited this page Dec 8, 2021 · 5 revisions

Emails


Here is an example of sending email using the EscNet micro framework

In Startup.cs, configure your dependency

services.AddEmailSender(emailSender, passwordSender);

In your code, first, receive your dependency via constructor

private readonly IEmailSender _emailSender;

public MyClass(IEmailSender emailSender){
    _emailSender = emailSender;
}


var email = new Email
{
    Receiver = "user.recipient@email.com",
    Subject = "Message subject",
    Body = "Message Body"
};

_emailSender.SendEmail(email);



To enable HTML template in the message body, just use

_emailSender.SendEmail(email, true); 

//or

_emailSender.SendEmail(email, isHtml: true);

// if you want async

await _emailSender.SendEmailAsync(email, isHtml: true);



By default the framework sets Gmail SMTP, but you can switch to Outlook SMTP by passing an enum

_emailSender.SetSMTP(SMTPType);



To set up an SMTP in addition to these use the following method

_emailSender.SetSMTP("Smtp Url");



By default, port 587 is already set, to change the port use the following value

_emailSender.SetPort(PORT_NUMBER);



To add one or more emails to be attached as a copy, use the following method

_emailSender.AddCopyEmail("copy@email.com");



To change

_emailSender.AddCopyEmail("copy@email.com");



And to remove a copy email, just use

_emailSender.RemoveCopyEmail("copy@email.com");




2021 ©


Clone this wiki locally