
? Email API 和 SMTP 哪个适合我?SendGrid 多语言开发集成指南
? 一、Email API vs SMTP:核心差异大揭秘
1. 工作原理大不同
HELO
打招呼,用 MAIL FROM
指定发件人,用 RCPT TO
确定收件人,最后用 DATA
发送邮件内容。它更像是一种 “原始” 的通信方式,需要你手动处理很多底层的连接和指令操作。2. 功能丰富度有差距
3. 适用场景大比拼
? 二、SendGrid:为啥选它做邮件发送搭档
1. 超高的邮件送达率
2. 强大的多语言支持
3. 灵活的模板管理
{{name}}
代表收件人的名字,{{order_id}}
代表订单号等。在发送邮件时,只需要传入这些变量的值,就能生成个性化的邮件内容。4. 完善的数据分析和监控
?️ 三、SendGrid 多语言开发集成实战
1. Python 集成步骤
pip install sendgrid
就能安装了。安装好之后,需要获取 API 密钥。登录 SendGrid 后台,进入 “设置 - API 密钥”,创建一个新的 API 密钥,记住要保存好,别泄露了。from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
message = Mail(
from_email='sender@example.com',
to_emails='recipient@example.com',
subject='Sending with SendGrid is Fun',
plain_text_content='and easy to do anywhere, even with Python')
try:
sg = SendGridAPIClient(api_key='你的 API 密钥')
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.message)
plain_text_content
换成 html_content
就行,比如:html_content = 'and easy to do anywhere, even with Python
'
message = Mail(
from_email='sender@example.com',
to_emails='recipient@example.com',
subject='Sending with SendGrid is Fun',
html_content=html_content)
2. Java 集成步骤
pom.xml
中添加 SendGrid 的依赖:<dependency>
<groupId>com.sendgridgroupId>
<artifactId>sendgrid-javaartifactId>
<version>4.9.1version>
dependency>
SendGrid
对象,传入 API 密钥。创建 Mail
对象,设置发件人、收件人、主题和内容。这里的内容也支持 HTML 格式。import com.sendgrid.*;
import com.sendgrid.helpers.mail.*;
public class SendEmail {
public static void main(String[] args) {
SendGrid sg = new SendGrid("你的 API 密钥");
Email from = new Email("sender@example.com");
String subject = "Sending with SendGrid is Fun";
Email to = new Email("recipient@example.com");
Content content = new Content("text/plain", "and easy to do anywhere, even with Java");
Mail mail = new Mail(from, subject, to, content);
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
Content
的类型改成 text/html
,内容换成 HTML 字符串就行。3. JavaScript(Node.js)集成步骤
npm install @sendgrid/mail
。安装完成后,获取 API 密钥,然后在代码中引入 SendGrid 模块,设置 API 密钥。const sgMail = require('@sendgrid/mail');
sgMail.setApiKey('你的 API 密钥');
const msg = {
to: 'recipient@example.com',
from: 'sender@example.com',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: 'and easy to do anywhere, even with Node.js
',
};
sgMail.send(msg).then((response) => {
console.log(response[].statusCode);
console.log(response[].headers);
}).catch((error) => {
console.error(error);
});
text
和 html
内容,这样收件人如果不支持 HTML 邮件,就会显示纯文本内容。4. C# 集成步骤
SendGrid
并安装。然后获取 API 密钥,编写发送邮件的代码。using SendGrid;
using SendGrid.Helpers.Mail;
using System;
class Program
{
static void Main()
{
var client = new SendGridClient("你的 API 密钥");
var from = new EmailAddress("sender@example.com", "发件人名称");
var to = new EmailAddress("recipient@example.com", "收件人名称");
var subject = "Sending with SendGrid is Fun";
var content = new Content("text/plain", "and easy to do anywhere, even with C#");
var msg = MailHelper.CreateSingleEmail(from, to, subject, content, null);
var response = client.SendEmailAsync(msg).Result;
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers);
}
}
Content
对象时,类型改为 text/html
,内容换成 HTML 字符串。