Create an API key in SendGrid for sending SMTP email
How to create an API key with the correct permissions for sending SMTP email in SendGrid, with an example of the settings to send mail using the API key
Create a new API key
Logon to SendGrid
https://app.sendgrid.com/login
Settings - API Keys - Create API Key
API Key Name e.g. website
API Key Permissions - Select Restricted Access
Set the API key permissions
Mail Send - set permissions to full access
You can use the API to access other SendGrid features, which may mean assigning different permissions to the API key. (refer to the SendGrid API documentation linked at the end of this article)
You should follow the principle of "least privilege" when assigning API key permissions. If in doubt, just assign "Mail send" permissions
Create & view
Copy the API key
Edit API key permissions
You can edit the permissions or delete the API key but you wont be able to view it again after its been created
Settings for using the API key to send SMTP mail
Here is an example for using the SendGrid API key to send mail using SMTP
The username will always be apikey, you'll need to replace SECRETKEY with your own API key
Mail server smtp.sendgrid.net
Port 587
Security STARTTLS
Username apikey
Password SECRETKEY
Send a test message using PowerShell
Send a test message using the API Key from PowerShell. You will get prompted for credentials, the username will be apikey, and the password will be the secret key.
Send-MailMessage -SmtpServer smtp.sendgrid.net -Credential $credential -Usessl -Port 587 -From noreply@yourdomain.com -To user@domain.com -Subject "test email from sendgrid" -Body "this is a test email"
References:
Manage SendGrid API Keys
https://docs.sendgrid.com/ui/account-and-settings/api-keys
SendGrid v3 API Documentation - API Key Permissions
https://sendgrid.api-docs.io/v3.0/api-key-permissions/api-key-permissions
Comments