Email Sending using Aspose.Email Cloud in Heroku Node.js App

A tutorial on how to setup a heroku node.js app, and how to use Aspose.Email Cloud for email sending in Node.js application.

This blog guides you on how to deploy a Node.js app on Heroku. And, the article helps you understand Aspose.Email Cloud, and how to use it for email sending. The article assumes that you already have a free Heroku Account setup and Node.js and NPM installed locally. Let’s get started!

Setup Heroku

To get started you first need to install the Heroku Command Line Interface (CLI). Heroku CLI is used to manage and perform various scalability tasks. You can use this to provision add-ons, view your application logs, and run your application locally. If you are using macOS, you can use Homebrew to install it or can visit the official Heroku.

brew install heroku/brew/heroku

Once the installation is done you can run the following command to authenticate Heroku to be used locally.

heroku login
heroku: Press any key to open up the browser to login or q to exit
 ›   Warning: If browser does not open, visit
 ›   https://cli-auth.heroku.com/auth/browser/\*\*\*
heroku: Waiting for login...
Logging in... done
Logged in as me@example.com

This command opens your browser to the Heroku login page for authentication. This is required for both Heroku and git commands to work properly

Setup Aspose.Email Cloud

Aspose.Email Cloud is a Cloud SDK to send, receive, append, flag, and convert cloud emails & support to create a folder structure for email archiving in the cloud. This is easy to use and fast API, that doesn’t need to install the additional software. The API supports many programming languages, such as C#, Java, PHP, Python, Ruby & Typescript. To know how to install the SDK please follow the instructions in official guide.

Email Sending using Aspose.Email Cloud

Assuming you’ve already installed Node.js, please create a directory for your application.

$ mkdir my-email-sending-app
$ cd my-email-sending-app
$ npm init
$ npm install express --save
$ npm install @asposecloud/aspose-email-cloud

add following code in your main.js file

//  Import the SDK
const email = require('@asposecloud/aspose-email-cloud');
const express = require('express')
const app = express()
const port = 5000

// Setup App Credentials 
const AsposeApp = {
    ClientId: '\*\*\*\*\*',
    ClientSecret: '\*\*\*\*\*',
}

// Setup the SDK
const api = new email.EmailCloud(AsposeApp.ClientId, AsposeApp.ClientSecret)

const credentials = new email.EmailClientAccountPasswordCredentials(
    'my@email.com', '\*\*\*\*\*');
const receiveAccountDto = new email.EmailClientAccount(
    'smtp.email.com', 465, 'SSLAuto', 'SMTP', credentials);

// Setup an email account for email sending
const smtpAccount = 'smtp.account';
const storageName = 'MyEmailStorage';
const accountFolder = 'MyEmailFolder';
const smtpLocation = new email.StorageFileLocation(
    storageName, accountFolder, smtpAccount);


app.get('/', async (req, res) => {
  res.send('Welcome to my email sending app in Node.js')
})

app.get('/setup-account', async (req, res) => {
    await api.client.account.save(new email.ClientAccountSaveRequest(smtpLocation, receiveAccountDto));
    res.send('Account setup successfully');
})

app.get('/send-email', async (req, res) => {
    // Send email using the email account
    const emaildto = new email.EmailDto();
    emaildto.from = new email.MailAddress('From address', 'example@gmail.com');
    emaildto.to = [new email.MailAddress('To address', 'to@aspose.com')];
    emaildto.subject = 'Some subject';
    emaildto.body = 'Some body';
    await api.client.message.send(
        new email.ClientMessageSendRequest(
            smtpLocation, new MailMessageDto(emaildto)));
    
    res.send('Email Sent Successfully');

});

app.listen(port, () => {
  console.log(\`Example app listening on port ${port}\`)
})

Deploy Node.js App to Heroku

Once you are done with all your changes and ready to publish your app, you can use the following commands to push your changes to Heroku.

$ heroku create
Creating sharp-rain-871... done, stack is heroku-18
http://sharp-rain-871.herokuapp.com/ | https://git.heroku.com/sharp-rain-871.git
Git remote heroku added

this will create a git repository on Heroku and anything you push to this repo will get deployed to your Heroku application.

$ git push heroku main

Now you can open your application using the heroku open command.

Conclusion

In this article, we learned about the Heroku platform and how we can set up a Node.js application on Heroku. We also explored the Aspose.Email Cloud, and used to set up an SMTP email client for email sending. Aspose.Email Cloud isn’t just for sending emails. Instead, it’s a Cloud SDK to send, receive, append, flag, and convert cloud emails & support to create a folder structure for email archiving in the cloud. This is easy to use and fast API, that doesn’t need to install the additional software. The API supports many programming languages, such as C#, Java, PHP, Python, Ruby & Typescript. I hope this article helped you.

Explore