1. Introduction
  2. Creating the basic form
  3. Integrating with AWS
  4. Integrating with S3
  5. Adding background tasks
  6. Sending emails and creating download links

Introduction

In the last part of this series, we will go through the final components that we need to build for our application to be complete and working as expected: the functionality for sending emails containing download links for the files.

Creating the email service

Let’s start by creating a service responsible with sending emails. For this tutorial, I will use Gmail – you can do the same by following this guide to allow your Gmail account to do this, or you can use another email provider. No matter what provider you use, you should create two parameters in the Parameter Store, called SMTP-username and SMTP-password, that contain your credentials.

Then, create an interface called IEmailService in the Services folder. This contains a single method that takes 3 parameters: the receiver’s email address, the title of the email, and the body:

To implement the interface, create a class called GmailEmailSender (or adapt the name, depending on what provider you use); we will inject an AwsParameterStoreClient object in the constructor:

Let’s also inject both classes from the ConfigureServices method in Startup:

And finally, let’s implement the SendEmail method; this will create an SmtpClient object that will connect to Gmail’s servers, setup the credentials from the Parameter Store, and then construct and send the email:

Creating and sending the email

Now that we have the service in place, we can construct the email from another class and schedule it to be sent when necessary. For constructing the right email body, let’s create a new class called EmailConstructorHelpers in a folder called Helpers. In here, we will store the application URI, since it’s necessary for the download link, and have a method that builds the URI from which the user will download the file:

Next, we will enqueue a background task that sends the email to the file receiver, giving them the download link, as well as letting them know who sent the file:

Creating the download link

There is one last component to be built: something that will let the user download the file when accessing the link from the email. We could accomplish this in 2 different ways:

  • Create a Razor page, get the file from S3, and use JavaScript interop to save it on the client side;
  • Create an ASP.NET Core controller and, get the file from S3 and return a FileStreamResult;

And since this series focuses on ASP.NET Core and AWS, and we did so nicely without JavaScript so far, we will go with the second option. Create a new folder called Controllers and a FilesController class inside it. In it, we will inject an IFileHandler instance and have a single method (action) that takes the file name from the URI, downloads the file and returns it to the user:

You can now test the whole application again and see the scheduled email:

And if you check your inbox, you should have received the message:

Conclusion

The tutorial series is now complete, and you have hopefully developed a file transfer application with Blazor and AWS. Don’t forget that you can see the whole code on GithHub, which I encourage you to star if you enjoyed the series. Also, if you would like to extend the functionality, you are welcome to fork the repository and start from there.

If you are interested in more content related to ASP.NET Core and Blazor, check out the e-book on ‘Full-stack development with ASP.NET Core’ from the right side of the page.

Thank you for following the series!

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn ASP.NET Core!
Subscribe!
Get the latest articles on .NET and cloud!