dev@yourdomain.com
)。Abc@12345678
)。smtp.sendgrid.net
587
(推荐使用 TLS 加密)apikey
yourdomain.com
),选择 Single Sender Verification。pip install sendgrid
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
message = Mail(
from_email='sender@yourdomain.com',
to_emails='recipient@example.com',
subject='测试邮件:Python 集成 SendGrid',
plain_text_content='这是一封通过 SendGrid API 发送的纯文本邮件',
html_content='这是一封通过 SendGrid API 发送的 HTML 格式邮件'
)
try:
sg = SendGridAPIClient(api_key='YOUR_API_KEY')
response = sg.send(message)
print(f'邮件发送成功!状态码:{response.status_code}')
print(f'响应内容:{response.body}')
except Exception as e:
print(f'发送失败:{str(e)}')
pom.xml
中加入:<dependency>
<groupId>com.sendgridgroupId>
<artifactId>sendgridartifactId>
<version>4.9.5version>
dependency>
import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
public class SendGridExample {
private static final String API_KEY = System.getenv("SENDGRID_API_KEY");
public static void main(String[] args) {
Email from = new Email("sender@yourdomain.com", "发件人名称");
Email to = new Email("recipient@example.com", "收件人名称");
String subject = "Java 集成 SendGrid 测试邮件";
Content content = new Content("text/plain", "这是 Java 发送的纯文本邮件内容");
Mail mail = new Mail(from, subject, to, content);
try {
SendGrid sg = new SendGrid(API_KEY);
Request request = new Request();
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 (Exception e) {
System.err.println("发送邮件时发生错误:" + e.getMessage());
}
}
}
System.getenv()
获取 API 密钥,避免硬编码至代码。Content
类同时设置 HTML 内容(text/html
类型)。Gemfile
中添加:gem 'sendgrid-ruby'
bundle install
安装。config/environments/production.rb
中添加:config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.sendgrid.net',
port: ,
user_name: 'apikey',
password: ENV['SENDGRID_API_KEY'],
domain: 'yourdomain.com',
enable_starttls_auto: true
}
class UserMailer < ActionMailer::Base
default from: 'notifications@yourdomain.com'
def welcome_email(user)
@user = user
mail(to: user.email, subject: '欢迎加入我们的平台')
end
end
UserMailer.welcome_email(@user).deliver_now
domain
参数需填写发件域名(如 yourdomain.com
),提升邮件可信度。npm install nodemailer nodemailer-sendgrid-transport
const nodemailer = require('nodemailer');
async function sendEmail() {
const transport = nodemailer.createTransport({
host: 'smtp.sendgrid.net',
port: ,
auth: {
user: 'apikey',
pass: process.env.SENDGRID_API_KEY
}
});
const message = {
from: '"系统通知" ' ,
to: 'recipient@example.com',
subject: 'Node.js 集成 SendGrid 测试邮件',
text: '这是 Node.js 发送的纯文本邮件',
html: '这是 Node.js 发送的 HTML 格式邮件
'
};
try {
const info = await transport.sendMail(message);
console.log('邮件已发送:', info.messageId);
} catch (error) {
console.error('发送失败:', error);
}
}
sendEmail();
🔍 有一云一键分发深度测评:能否破解新媒体团队协作效率困局? 🚀 全流程提效:从内容生产到多平台分发的 “闪电战” 新媒体团队日常最耗时间的环节是什么?答案一定是多平台内容适配。同样一篇文章,要手
打开编辑器,光标闪了半小时,屏幕还是一片空白。后台催更信息响个不停,粉丝留言问 “今天怎么没内容”,这种选题枯竭的窒息感,做自媒体的没人想再经历第二次。其实解决办法没那么复杂,试试 “主题日” 创作
? 一、为什么说瓦斯阅读 2025 是资讯管理的革命性工具 如果你还在为每天被微信公众号推送、新闻弹窗、社交媒体动态轰炸而感到焦虑,那你一定要试试瓦斯阅读 2025 升级版。这款工具把分散在各个平台的
? 百度千帆 AppBuilder Agent 框架实战:低代码实现流程自动化应用全攻略 2025 在数字化转型浪潮中,企业对流程自动化的需求愈发迫切。百度千帆 AppBuilder Agent 框架
? 智源 2025 重磅发布「悟界」系列大模型:从数字智能到物理智能的跨越 在人工智能领域,北京智源人工智能研究院始终是技术探索的先锋。2025 年 6 月,第七届北京智源大会上,智源正式推出「悟界」
?儿童抑郁早发现:CDI 与 DSRSC 量表测评全解析 家里有 7 到 17 岁孩子的家长们,有没有发现孩子最近突然变得沉默寡言?或者写作业时总是心不在焉,甚至半夜躲在被子里偷偷哭?别以为这只是 “
?️ 软件下载安全指南:2025 年佛系推荐的 Windows/macOS 精品软件实测 作为混迹设计圈多年的老鸟,我见过太多同行因为下载软件踩坑 —— 轻则电脑卡慢弹窗不断,重则文件泄露项目黄掉。最
? NovoEd 企业在线培训平台:AI 如何重塑组织学习生态 当越来越多企业意识到员工技能迭代速度决定组织竞争力时,NovoEd 带着 AI 驱动的个性化学习方案闯入视野。这个成立于 2012 年的