? SendGrid 怎么设置 SMTP 服务?多语言开发集成全攻略 2025
? 一、SendGrid SMTP 服务基础设置
? 二、Python 开发集成
import sendgrid
from sendgrid.helpers.mail import Mail
sg = sendgrid.SendGridAPIClient(api_key='YOUR_API_KEY')
from_email = Email("sender@example.com")
to_email = To("recipient@example.com")
subject = "Hello from SendGrid"
content = Content("text/plain", "This is a test email.")
mail = Mail(from_email, to_email, subject, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
? 三、JavaScript(Node.js)开发集成
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'recipient@example.com',
from: 'sender@example.com',
subject: 'Hello from SendGrid',
text: 'This is a test email.',
html: 'This is a test email.
',
};
sgMail.send(msg).then(response => {
console.log(response[].statusCode);
}).catch(error => {
console.error(error);
});
☕ 四、Java 开发集成
<dependency>
<groupId>com.sendgridgroupId>
<artifactId>sendgridartifactId>
<version>4.7.6version>
dependency>
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 {
public static void main(String[] args) {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Email from = new Email("sender@example.com");
Email to = new Email("recipient@example.com");
String subject = "Hello from SendGrid";
Content content = new Content("text/plain", "This is a test email.");
Mail mail = new Mail(from, subject, to, content);
try {
SendGrid.Response response = sg.api(mail.build());
System.out.println(response.getStatusCode());
} catch (Exception e) {
e.printStackTrace();
}
}
}
? 五、PHP 开发集成
require 'vendor/autoload.php';
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
$from = new \SendGrid\Mail\From("sender@example.com");
$to = new \SendGrid\Mail\To("recipient@example.com");
$subject = "Hello from SendGrid";
$content = new \SendGrid\Mail\Content("text/plain", "This is a test email.");
$mail = new \SendGrid\Mail\Mail($from, $subject, $to, $content);
try {
$response = $sg->send($mail);
print $response->statusCode() . "\n";
} catch (Exception $e) {
echo 'Caught exception: ' . $e->getMessage() . "\n";
}
? 六、C# 开发集成
using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
var client = new SendGridClient(apiKey);
var from = new EmailAddress("sender@example.com", "Example User");
var subject = "Hello from SendGrid";
var to = new EmailAddress("recipient@example.com", "Example User");
var plainTextContent = "This is a test email.";
var htmlContent = "This is a test email.
";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
Console.WriteLine(response.StatusCode);
}
}
? 七、Go 开发集成
package main
import (
"fmt"
"log"
"os"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)
func main() {
apiKey := os.Getenv("SENDGRID_API_KEY")
sg := sendgrid.NewSendGridClient(apiKey)
from := mail.NewEmail("Example User", "sender@example.com")
subject := "Hello from SendGrid"
to := mail.NewEmail("Example User", "recipient@example.com")
content := mail.NewContent("text/plain", "This is a test email.")
mail := mail.NewSingleEmail(from, subject, to, content)
response, err := sg.Send(mail)
if err != nil {
log.Fatalf("SendGrid Error: %s\n", err)
}
fmt.Println(response.StatusCode)
}