在 Heroku Node.js 應用程序中使用 Aspose.Email Cloud 發送電子郵件

有關如何設置 heroku node.js 應用程序以及如何使用 Aspose.Email Cloud 在 Node.js 應用程序中發送電子郵件的教程。

此博客將指導您如何在 Heroku 上部署 Node.js 應用程序。並且,本文幫助您了解 Aspose.Email Cloud,以及如何使用它發送電子郵件。本文假定您已經有一個免費的 Heroku 帳戶設置並且在本地安裝了 Node.js 和 NPM。讓我們開始吧!

安裝 Heroku

要開始使用,您首先需要安裝 Heroku 命令行界面 (CLI)。 Heroku CLI 用於管理和執行各種可伸縮性任務。您可以使用它來配置附加組件、查看您的應用程序日誌以及在本地運行您的應用程序。如果你使用的是 macOS,你可以使用 Homebrew 安裝它,或者可以訪問官方的 Heroku。

brew install heroku/brew/heroku

安裝完成後,您可以運行以下命令來驗證要在本地使用的 Heroku。

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

此命令將您的瀏覽器打開到 Heroku 登錄頁面以進行身份驗證。這是 Heroku 和 git 命令正常工作所必需的

設置 Aspose.Email Cloud

Aspose.Email Cloud 是一個 Cloud SDK,用於發送、接收、附加、標記和轉換雲電子郵件,並支持創建用於在雲中歸檔電子郵件的文件夾結構。這是易於使用且快速的 API,不需要安裝額外的軟件。 API 支持多種編程語言,例如 C#、Java、PHP、Python、Ruby 和 Typescript。要了解如何安裝 SDK,請按照 官方指南 中的說明進行操作。

使用 Aspose.Email Cloud 發送電子郵件

假設您已經安裝了 Node.js,請為您的應用程序創建一個目錄。

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

現在在您的 main.js 文件中添加以下代碼

$ 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

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

// 設置應用憑證 
const AsposeApp = {
    ClientId: '\*\*\*\*\*',
    ClientSecret: '\*\*\*\*\*',
}

// 設置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);

// 設置電子郵件帳戶以發送電子郵件
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) => {
    // 使用電子郵件帳戶發送電子郵件
    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}\`)
})

將 Node.js 應用程序部署到 Heroku

完成所有更改並準備發布應用程序後,您可以使用以下命令將更改推送到 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

這將在 Heroku 上創建一個 git 存儲庫,您推送到該存儲庫的任何內容都將部署到您的 Heroku 應用程序中。

$ git push heroku main

現在您可以使用 heroku open 命令打開您的應用程序。

結論

在本文中,我們了解了 Heroku 平台以及在 Heroku 上使用 Node.js 應用程序發送電子郵件。我們還探索了 Aspose.Email Cloud,並用於設置 SMTP 電子郵件客戶端以在線發送電子郵件。 Aspose.Email Cloud 不僅僅用於發送電子郵件。相反,它是一個雲 SDK,用於發送、接收、附加、標記和轉換雲電子郵件,並支持創建用於在雲中歸檔電子郵件的文件夾結構。這是易於使用且快速的 API,不需要安裝額外的軟件。 API 支持多種編程語言,例如 C#、Java、PHP、Python、Ruby 和 Typescript。我希望這篇文章對你有所幫助。

我們強烈建議通過 產品文檔 探索 Aspose.Email for Cloud 的功能。此外,如果您在使用 API 時遇到任何問題,請隨時通過免費產品支持論壇 與我們聯繫。

探索