Stay organized with collections Save and categorize content based on your preferences.
This tutorial shows how to use SendGrid to send email from an app running on a Compute Engine virtual machine (VM) instance.
ObjectivesFor other information about setting up SendGrid, see the SendGrid developer documentation.
CostsIn this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage, use the pricing calculator.
New Google Cloud users might be eligible for a
free trial.
Before you beginIn the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.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.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.Verify that billing is enabled for your Google Cloud project.
sendgrid-tutorial
.smtp.sendgrid.net
.Complete the following steps to connect to your sendgrid-tutorial instance and run SendGrid with Postfix.
Note: The SendGrid documentation provides a set of instructions for Postfix installation and setup. If you run into issues setting up Postfix with SendGrid, review the SendGrid Postfix instructions. Connect to your sendgrid-tutorial instance using SSHRun the following commands in your SSH terminal to use SendGrid as an SMTP relay with Postfix.
Become a superuser:
sudo su -
Set a safe umask:
umask 077
Install the Postfix Mail Transport Agent:
Debianapt update && apt -y install postfix libsasl2-modulesCentOS
yum install postfix cyrus-sasl-plain cyrus-sasl-md5 -y
If prompted, select the Local Only configuration and accept the default domain name.
Modify the Postfix configuration options. Open /etc/postfix/main.cf
for editing. For example, to use the nano
text editor, enter the following command:
nano /etc/postfix/main.cf
Update the file:
Comment out the following lines:
# default_transport = error # relay_transport = error
Add the following lines to the end of the file:
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
The above lines enforce SSL/TLS support and configure SMTP authentication for these requests. A simple access and security layer (SASL) module handles authentication in the Postfix configuration.
Save and close the file.
Generate the SASL password map using the API key you generated in the Before you begin section. Replace your-api-key
with the API key you generated.
echo [smtp.sendgrid.net]:2525 apikey:your-api-key >> /etc/postfix/sasl_passwd
Use the postmap
utility to generate a .db
file:
postmap /etc/postfix/sasl_passwd
Verify that you have a .db
file:
ls -l /etc/postfix/sasl_passwd*
-rw------- 1 root root ... /etc/postfix/sasl_passwd -rw------- 1 root root ... /etc/postfix/sasl_passwd.db
Remove the file containing your credentials because it is no longer needed:
rm /etc/postfix/sasl_passwd
Set the permissions on your .db
file and verify that the other file was removed:
chmod 600 /etc/postfix/sasl_passwd.db
ls -la /etc/postfix/sasl_passwd*
-rw------- 1 root root ... /etc/postfix/sasl_passwd.db
Reload your configuration to load the modified parameters:
Debian/etc/init.d/postfix restart
CentOS
postfix reload
Install the mailutils
or mailx
package:
apt -y install mailutilsCentOS
yum install mailx -y
Send a test email:
echo 'message' | mail -s subject email@example.com
Replace the following:
message
: The body of the email.subject
: The subject of the email.email@example.com
: The email address that you want to send a message to.Look in your system logs for a status line containing status
and the successful server response code (250)
:
tail -n 5 /var/log/syslog
CentOS
tail -n 5 /var/log/maillog
The following instructions use the SendGrid Java client library to construct and send an email message through SendGrid. You can view the full example on GitHub.
In your SSH terminal:
Become a superuser and set a safe umask:
sudo su -
umask 077
Install Java and Maven:
apt -y update && apt -y install git-core openjdk-11-jdk maven
Clone the GitHub repo:
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Go to the main source code for the example:
cd /root/java-docs-samples/compute/sendgrid/src/main/java/com/example/compute/sendgrid
Open SendEmailServelet.java
for editing.
Replace your-sendgrid-api-key
with the API key for your SendGrid account.
Replace your-sendgrid-from-email
with the email address you want to send mail from.
Replace destination-email
with the email address you want to send mail to.
Go to the root directory of the sample code:
cd /root/java-docs-samples/compute/sendgrid
Package the Java class:
mvn clean package
Go to the new target
directory:
cd target
Set permissions that let you execute the jar file:
chmod +x compute-sendgrid-1.0-SNAPSHOT-jar-with-dependencies.jar
Run the alternative Java version selector:
update-alternatives --config java
Select the java-11-openjdk-amd64
option.
Execute the Java file:
java -jar compute-sendgrid-1.0-SNAPSHOT-jar-with-dependencies.jar
To run this sample, you must have Node.js
version 7.6 or later installed on the VM instance.
In your SSH terminal:
Become a superuser and set a safe umask:
sudo su -
umask 077
Update your package repositories:
Debianapt update
CentOS
yum update -y
Install Node.js dependencies:
Debianapt -y install git-core curl build-essential openssl libssl-dev
CentOS
yum install git-core curl openssl openssl-devel -y
yum groupinstall "Development Tools" -y
Install Node.js. By default, the installation also installs npm:
Debiancurl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
sudo apt -y install nodejs
CentOS
curl --silent --location https://rpm.nodesource.com/setup_14.x | bash -
Then, install Node.js:
yum -y install nodejs
Install the SendGrid Node.js client:
npm install sendgrid
Clone the sample repository:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Go to the directory that contains the SendGrid sample:
cd nodejs-docs-samples/compute
Copy the sendgrid.js
file:
cp sendgrid.js sendmail.js
Open sendmail.js
for editing.
Replace your-sendgrid-api-key
with the API key for your SendGrid account.
Replace from-email@example.com
with the email address that you want to send mail from.
Replace to-email@example.com
with the email address that you want to send mail to.
Run the program to send an email message through SendGrid:
node sendmail.js
You can set up Microsoft Exchange to send outbound email with SendGrid by configuring an outbound send connector. For details, see Deploying Microsoft Exchange Server 2016 on Compute Engine.
Clean upTo avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Delete the projectThe easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
appspot.com
URL, delete selected resources inside the project instead of deleting the whole project.If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects can help you avoid exceeding project quota limits.
To delete a Compute Engine instance:
sendgrid-tutorial
instance.Explore reference architectures, diagrams, and best practices about Google Cloud. Take a look at our Cloud Architecture Center.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["This tutorial demonstrates how to use SendGrid to send emails from a Compute Engine virtual machine (VM) instance."],["You can use SendGrid with Postfix, which is covered through a step-by-step setup and configuration guide."],["The tutorial outlines how to send emails with SendGrid using Java code, which involves setting up the necessary environment and running a pre-made Java sample."],["Instructions are provided for using SendGrid to send emails from a Node.js application, including installing dependencies and configuring a sample script."],["You can also set up SendGrid with a Microsoft Exchange edge transport server to manage outbound email flow and the tutorial ends by providing guidance on how to clean up the used project or its individual resources."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4