本教程介绍如何使用 SendGrid 从 Compute Engine 虚拟机 (VM) 实例上运行的应用发送电子邮件。
使用 Postfix 从实例发送邮件
完成以下步骤以连接到您的 sendgrid-tutorial 实例并使用 Postfix 运行 SendGrid。
使用 SSH 连接到您的 sendgrid-tutorial 实例
- In the Google Cloud console, go to the VM instances page.
- In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.
使用 Postfix 将 SendGrid 配置为 SMTP 中继
在 SSH 终端中运行以下命令,以使用 Postfix 将 SendGrid 用作 SMTP 中继。
成为超级用户:
sudo su -设置安全 umask 值:
umask 077安装 Postfix 邮件传输代理:
Debian
apt update && apt -y install postfix libsasl2-modules
CentOS
yum install postfix cyrus-sasl-plain cyrus-sasl-md5 -y
如果出现提示,请选择仅限本地配置并接受默认域名。
修改 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 protected]
替换以下内容:
message:电子邮件的正文。subject:电子邮件的主题。[email protected]:收件人电子邮件地址。
在系统日志中查找包含
status和表示成功的服务器响应代码(250)的状态行:Debian
tail -n 5 /var/log/syslogCentOS
tail -n 5 /var/log/maillog
在您的实例上通过 Java 发送邮件
使用 SSH 连接到您的 sendgrid-tutorial 实例
- In the Google Cloud console, go to the VM instances page.
- In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.
构建并发送电子邮件
以下说明使用 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 发送邮件
如需运行此示例,您必须在虚拟机实例上安装 Node.js 7.6 或更高版本。
使用 SSH 连接到您的 sendgrid-tutorial 实例
- In the Google Cloud console, go to the VM instances page.
- In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.
构建并发送电子邮件
在您的 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 密钥。将
[email protected]替换为发件人电子邮件地址。将
[email protected]替换为收件人电子邮件地址。
运行该程序,通过 SendGrid 发送一封电子邮件:
node sendmail.js
从 Exchange 边缘传输服务器发送邮件
您可以通过配置出站发送连接器来将 Microsoft Exchange 设置为使用 SendGrid 发送出站电子邮件。如需了解详情,请参阅在 Compute Engine 上部署 Microsoft Exchange Server 2016。