Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use SendGrid to send email

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Let's continue to take a look at some of the services in Azure Global. This time, let's take a look at the services that can be used to send email in Azure. In fact, there is no clear service in Azure that can be used to send email, because Microsoft already has O365 to do this, but operations such as sending alarm email can still be implemented directly by using the built-in functions in Azure, but if you send email separately, for example, it is used as a notification mailbox for the system. Send some email messages to users, etc., this kind of operation cannot be done with Azure services, but in fact, Azure not only provides Microsoft's own services, but also thousands of third-party vendors' services can be used. Today, let's introduce SendGrid, a third-party service of Azure Global.

SendGrid can be used as an ordinary Mail Server, and it is very easy to use SendGrid in Azure. Today, we will demonstrate how to create a SendGrid account in Azure and use Python to send email.

First of all, let's look at the creation of SendGrid.

Search SendGrid directly in Azure Global and fill in some basic information to create a SendGrid account. Pay attention to the Pricing Tier here. We choose the Free,Free level to send 25000 free emails per month.

The process of creation is very fast, normally within 1 minute. After the creation is completed, click manage to manage the SendGrid in the overview interface.

After that, you can jump to the management interface of SendGrid, where we select Account Details in the upper right corner. This is mainly to check the Email Address of our account. Without Email Address, we cannot send email.

We can see our own email address here.

Next, let's see how to send an e-mail.

In fact, SendGrid provides a variety of ways to send e-mail. The simplest way is through Web API. Before sending email, we first need to obtain our Key,SendGrid to verify the user's identity, not by the password filled in before, but by the Key of API.

So first of all, we need to setup our key and choose Web API.

SendGrid provides API for multiple languages. We choose Python here, and then we will demonstrate how to use Python to send email through SendGrid.

After that, we directly create key to create a key

The key is in the form of a long string. Save the string.

Write Python code

Finally, we call API to send mail by writing Python code. First, install the module of SendGrid's python.

Module can install pip install sendgrid directly through Pip install

Then we store the key in the environment variable of the system

Setx SENDGRID_API_KEY "YOUR_API_KEY"

After the storage is completed, you can read the key value directly in PowerShell, so it is finished.

Then you can write code to try to send the email.

Import osfrom sendgrid import SendGridAPIClientmessage = {'personalizations': [{' to': [{'email':' xxx@xxx.com'}], 'subject':' Test SendGrid'}] 'from': {'email':' azure_xxxxxs@azure.com'}, 'content': [{' type': 'text/html'' Try: sg = SendGridAPIClient (os.environ.get ('SENDGRID_API_KEY')) response = sg.send (message) print (response.status_code) print (response.body) print (response.headers) except Exception as e: print (str (e))

The email address here can be replaced with the one you need.

After the sending is completed, you can receive the email! Generally speaking, it is still very simple, if it is not too complex requirements, and there are certain considerations for the cost, we can consider this approach.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report