本教學課程說明如何使用 SendGrid,從在 Compute Engine 虛擬機器 (VM) 執行個體上執行的應用程式傳送電子郵件。
目標
- 在 Compute Engine 執行個體上搭配 Postfix 使用 SendGrid。
- 在 Compute Engine 執行個體上執行的 Java 程式碼中使用 SendGrid。
- 在 Compute Engine 執行個體上執行的 Node.js 程式碼中使用 SendGrid。
- 在 Compute Engine 執行個體上,搭配 Microsoft Exchange 邊緣傳輸伺服器使用 SendGrid。
如需設定 SendGrid 的其他資訊,請參閱 SendGrid 開發人員說明文件。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
- Compute Engine
如要根據預測用量估算費用,請使用 Pricing Calculator。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
- 前往 Google Cloud 控制台的「Create an instance」(建立執行個體)頁面。
-
將「Name」(名稱) 設定為
sendgrid-tutorial。 - 在「OS 和儲存空間」部分中,按一下「變更」,即可開始設定開機磁碟。
- 在「Public images」(公開映像檔) 分頁中,選擇 Debian 或 CentOS 映像檔版本。
- 按一下「Select」(選取)。
- 按一下「Create」(建立),即可建立 VM。
-
使用 Google Cloud Marketplace 註冊 SendGrid 電子郵件服務。請記下您的 SendGrid SMTP 帳戶憑證,包括使用者名稱、密碼和主機名稱。您的 SMTP 使用者名稱和密碼,即是註冊服務時所用的使用者名稱和密碼。SendGrid 主機名稱為
smtp.sendgrid.net。 - 建立 API 金鑰:
- 登入 SendGrid,然後前往「Settings」>「API Keys」。
- 建立 API 金鑰。
- 選取金鑰的權限。金鑰至少必須具備「傳送郵件」權限,才能傳送電子郵件。
- 按一下 [Save] 建立金鑰。
- SendGrid 會產生一個新金鑰。此為唯一一份金鑰,因此請務必複製並儲存金鑰,以供日後使用。
使用 Postfix 從執行個體傳送郵件
請按照以下步驟連線至 sendgrid-tutorial 執行個體,再以 Postfix 執行 SendGrid。
使用 SSH 連線至 sendgrid-tutorial 執行個體
- 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
- 在虛擬機器執行個體清單中,找到您要建立連線的執行個體,然後在該列中點選「SSH」SSH。
透過 Postfix 將 SendGrid 設定為 SMTP 轉發服務
在 SSH 終端機中執行下列指令,藉以透過 Postfix 將 SendGrid 做為 SMTP 轉發使用。
成為超級使用者:
sudo su -設定安全的 umask:
umask 077安裝 Postfix Mail Transport Agent (Postfix 郵件傳輸代理程式):
Debian
apt update && apt -y install postfix libsasl2-modules
CentOS
yum install postfix cyrus-sasl-plain cyrus-sasl-md5 -y
如果出現系統提示,請選取 [Local Only] (僅限本機) 設定並接受預設的網域名稱。
修改 Postfix 設定選項。打開
/etc/postfix/main.cf進行編輯。舉例來說,如要使用nano文字編輯器,請輸入下列指令:nano /etc/postfix/main.cf更新檔案:
加註下列幾行:
# default_transport = error # relay_transport = error
在檔案末尾加入下列幾行:
relayhost = [smtp.sendgrid.net]:2525 smtp_tls_security_level = encrypt smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd header_size_limit = 4096000 smtp_sasl_security_options = noanonymous以上幾行會針對這些要求強制執行安全資料傳輸層 (SSL)/傳輸層安全標準 (TLS) 支援,並設定 SMTP 驗證。 簡單存取及安全性階層 (SASL) 模組可處理 Postfix 設定中的驗證作業。
儲存並關閉檔案。
使用您在「事前準備」一節中產生的 API 金鑰,產生 SASL 密碼對應。將
your-api-key改為您產生的 API 金鑰。echo [smtp.sendgrid.net]:2525 apikey:your-api-key >> /etc/postfix/sasl_passwd
使用
postmap公用程式產生.db檔案。postmap /etc/postfix/sasl_passwd確認您擁有
.db檔案:ls -l /etc/postfix/sasl_passwd*-rw------- 1 root root ... /etc/postfix/sasl_passwd -rw------- 1 root root ... /etc/postfix/sasl_passwd.db
移除內含憑證的檔案,因為現在已不需要該檔案:
rm /etc/postfix/sasl_passwd在
.db檔案上設定權限,並確認另一個檔案已移除:chmod 600 /etc/postfix/sasl_passwd.db ls -la /etc/postfix/sasl_passwd*-rw------- 1 root root ... /etc/postfix/sasl_passwd.db
重新載入設定,以載入修改後的參數:
Debian
/etc/init.d/postfix restartCentOS
postfix reload安裝
mailutils或mailx套件:Debian
apt -y install mailutils
CentOS
yum install mailx -y傳送測試電子郵件:
echo 'message' | mail -s subject email@example.com
更改下列內容:
message:電子郵件內文。subject:電子郵件主旨。email@example.com:要傳送訊息的電子郵件地址。
請在系統記錄中尋找內含
status的狀態行,以及成功的伺服器回應碼(250):Debian
tail -n 5 /var/log/syslogCentOS
tail -n 5 /var/log/maillog
在執行個體上以 Java 傳送郵件
使用 SSH 連線至 sendgrid-tutorial 執行個體
- 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
- 在虛擬機器執行個體清單中,找到您要建立連線的執行個體,然後在該列中點選「SSH」SSH。
建構並傳送電子郵件
以下操作說明會使用 SendGrid Java 用戶端程式庫,透過 SendGrid 來建構和傳送電子郵件。您可在 GitHub 上檢視完整範例。
在 SSH 終端機中:
成為超級使用者並設定安全的 umask:
sudo su - umask 077安裝 Java 和 Maven:
apt -y update && apt -y install git-core openjdk-11-jdk maven複製 GitHub 存放區:
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git前往範例的主要原始碼:
cd /root/java-docs-samples/compute/sendgrid/src/main/java/com/example/compute/sendgrid開啟
SendEmailServelet.java進行編輯。將
your-sendgrid-api-key替換為您 SendGrid 帳戶的 API 金鑰。將
your-sendgrid-from-email替換為您要用來發送郵件的電子郵件地址。將
destination-email替換為您要傳送郵件到其中的目標電子郵件地址。
前往程式碼範例的根目錄:
cd /root/java-docs-samples/compute/sendgrid建立 Java 類別套件:
mvn clean package前往新的
target目錄:cd target設定權限,允許執行 jar 檔案:
chmod +x compute-sendgrid-1.0-SNAPSHOT-jar-with-dependencies.jar執行替代的 Java 版本選取器:
update-alternatives --config java選取
java-11-openjdk-amd64選項。執行 Java 檔案:
java -jar compute-sendgrid-1.0-SNAPSHOT-jar-with-dependencies.jar
在執行個體上透過 Node.js 傳送郵件
如要執行此範例,VM 執行個體上必須已安裝 Node.js 7.6 版或更新版本。
使用 SSH 連線至 sendgrid-tutorial 執行個體
- 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
- 在虛擬機器執行個體清單中,找到您要建立連線的執行個體,然後在該列中點選「SSH」SSH。
建構並傳送電子郵件
在 SSH 終端機中:
成為超級使用者並設定安全的 umask:
sudo su - umask 077更新套件存放區:
Debian
apt updateCentOS
yum update -y安裝 Node.js 依附元件:
Debian
apt -y install git-core curl build-essential openssl libssl-devCentOS
yum install git-core curl openssl openssl-devel -y yum groupinstall "Development Tools" -y安裝 Node.js。根據預設,系統在安裝過程中也會安裝 npm:
Debian
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash - sudo apt -y install nodejsCentOS
curl --silent --location https://rpm.nodesource.com/setup_14.x | bash -然後再安裝 Node.js:
yum -y install nodejs安裝 SendGrid Node.js 用戶端:
npm install sendgrid複製範例存放區:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git前往含有 SendGrid 範例的目錄:
cd nodejs-docs-samples/compute複製
sendgrid.js檔案:cp sendgrid.js sendmail.js開啟
sendmail.js進行編輯。將
your-sendgrid-api-key替換為您 SendGrid 帳戶的 API 金鑰。將
from-email@example.com替換為您要用來發送郵件的電子郵件地址。將
to-email@example.com替換為您要傳送郵件到其中的目標電子郵件地址。
執行程式以透過 SendGrid 傳送電子郵件訊息:
node sendmail.js
從 Exchange Edge 傳輸伺服器傳送郵件
您可以設定外寄傳送連接器,讓 Microsoft Exchange 透過 SendGrid 傳送外寄電子郵件。詳情請參閱「在 Compute Engine 上部署 Microsoft Exchange Server 2016」。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
刪除專案的方法如下:
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
刪除 Compute Engine 執行個體
如要刪除 Compute Engine 執行個體:
- 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
-
勾選
sendgrid-tutorial執行個體的核取方塊。 - 如要刪除執行個體,請依序點選 「More actions」(更多動作) 和「Delete」(刪除),然後按照指示操作。
後續步驟
查看 Google Cloud 的參考架構、圖表和最佳做法。歡迎瀏覽我們的 Cloud Architecture Center。