golang轻量级SMTP客户端提供HTTP API插件库Hectane的使用
Golang轻量级SMTP客户端提供HTTP API插件库Hectane的使用
Hectane简介
Hectane是一个Go语言实现的轻量级SMTP客户端库,同时也是一个独立的应用程序,可以通过HTTP API提供邮件发送功能。
主要特性
- 支持在邮件中添加附件
- 支持TLS加密和HTTP基本认证
- 高效的邮件队列系统,确保邮件投递
- 队列中的邮件会持久化存储在磁盘上直到投递成功
- 按照优先级顺序尝试目标主机的MX记录
- 在Windows上可以作为服务运行
安装方式
Hectane可以通过以下方式安装:
- PPA稳定版或每日构建版
- Juju charm store
- Docker Hub
使用示例
作为Go包使用
package main
import (
"github.com/hectane/hectane"
"github.com/hectane/hectane/email"
)
func main() {
// 创建Hectane实例
h := hectane.New(&hectane.Config{
StoragePath: "storage", // 邮件存储路径
})
// 启动服务
if err := h.Start(); err != nil {
panic(err)
}
defer h.Stop()
// 创建邮件
e := &email.Email{
From: "sender@example.com",
To: []string{"recipient@example.com"},
Subject: "Test Email",
Text: "This is a test email sent using Hectane.",
}
// 将邮件加入队列
if err := h.Queue(e); err != nil {
panic(err)
}
}
作为HTTP API服务使用
package main
import (
"github.com/hectane/hectane"
"github.com/hectane/hectane/api"
"net/http"
)
func main() {
// 创建Hectane实例
h := hectane.New(&hectane.Config{
StoragePath: "storage", // 邮件存储路径
})
// 启动邮件服务
if err := h.Start(); err != nil {
panic(err)
}
defer h.Stop()
// 创建HTTP API服务
s := api.New(&api.Config{
Hectane: h,
Auth: "username:password", // 基本认证
})
// 启动HTTP服务器
if err := http.ListenAndServe(":8080", s); err != nil {
panic(err)
}
}
通过HTTP API发送邮件
package main
import (
"bytes"
"encoding/json"
"net/http"
)
func main() {
// 构造邮件数据
data := map[string]interface{}{
"from": "sender@example.com",
"to": []string{"recipient@example.com"},
"subject": "Test Email",
"text": "This is a test email sent via Hectane API",
}
jsonData, _ := json.Marshal(data)
// 创建HTTP请求
req, _ := http.NewRequest(
"POST",
"http://localhost:8080/emails",
bytes.NewBuffer(jsonData),
)
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth("username", "password") // 基本认证
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
}
文档参考
- 在Go应用中使用Hectane
- 在其他语言或服务器上使用Hectane守护进程
Hectane是一个功能强大且易于集成的SMTP客户端解决方案,特别适合需要邮件队列和HTTP API集成的应用场景。
更多关于golang轻量级SMTP客户端提供HTTP API插件库Hectane的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于golang轻量级SMTP客户端提供HTTP API插件库Hectane的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Hectane: 轻量级Golang SMTP客户端与HTTP API插件库
Hectane是一个轻量级的Golang库,它提供了SMTP客户端功能,并可通过HTTP API进行控制。以下是关于Hectane的详细介绍和使用示例。
Hectane主要特性
- 轻量级SMTP客户端实现
- 内置HTTP API服务
- 支持多账户管理
- 简单的邮件发送接口
- 可扩展的插件架构
安装Hectane
go get github.com/hectane/hectane
基本使用示例
1. 发送简单邮件
package main
import (
"log"
"github.com/hectane/hectane"
"github.com/hectane/hectane/email"
)
func main() {
// 创建Hectane实例
h := hectane.New()
// 添加SMTP账户配置
h.AddAccount(&hectane.Account{
Name: "default",
Host: "smtp.example.com",
Port: 587,
Username: "user@example.com",
Password: "password",
})
// 创建邮件
msg := &email.Email{
From: "sender@example.com",
To: []string{"recipient@example.com"},
Subject: "Test Email",
Text: "This is a test email sent using Hectane.",
}
// 发送邮件
if err := h.Send(msg, "default"); err != nil {
log.Fatal(err)
}
log.Println("Email sent successfully!")
}
2. 启动HTTP API服务
package main
import (
"log"
"net/http"
"github.com/hectane/hectane"
)
func main() {
// 创建Hectane实例
h := hectane.New()
// 添加SMTP账户
h.AddAccount(&hectane.Account{
Name: "default",
Host: "smtp.example.com",
Port: 587,
Username: "user@example.com",
Password: "password",
})
// 启动HTTP服务器
http.Handle("/", h.HTTPHandler())
log.Println("Starting HTTP server on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
HTTP API接口
启动HTTP服务后,可以通过以下API端点发送邮件:
发送邮件 (POST /email)
请求示例:
{
"account": "default",
"email": {
"from": "sender@example.com",
"to": ["recipient@example.com"],
"subject": "Test Email",
"text": "This is a test email."
}
}
响应示例:
{
"success": true
}
高级配置
1. 配置TLS
h.AddAccount(&hectane.Account{
Name: "secure",
Host: "smtp.example.com",
Port: 465,
Username: "user@example.com",
Password: "password",
TLS: true, // 启用TLS
})
2. 添加多个账户
h.AddAccount(&hectane.Account{
Name: "gmail",
Host: "smtp.gmail.com",
Port: 587,
Username: "user@gmail.com",
Password: "password",
})
h.AddAccount(&hectane.Account{
Name: "outlook",
Host: "smtp.office365.com",
Port: 587,
Username: "user@outlook.com",
Password: "password",
})
3. 发送HTML邮件
msg := &email.Email{
From: "sender@example.com",
To: []string{"recipient@example.com"},
Subject: "HTML Email",
HTML: "<h1>Hello</h1><p>This is an <b>HTML</b> email.</p>",
}
错误处理
if err := h.Send(msg, "default"); err != nil {
switch err {
case hectane.ErrAccountNotFound:
log.Println("Account not found")
case hectane.ErrInvalidEmail:
log.Println("Invalid email format")
default:
log.Printf("Failed to send email: %v", err)
}
}
性能考虑
Hectane设计为轻量级,适合中小规模邮件发送需求。对于高并发场景,建议:
- 使用连接池
- 限制并发发送数量
- 实现队列机制处理大量邮件
总结
Hectane提供了一个简单而强大的方式来在Golang应用中集成SMTP邮件发送功能,并通过HTTP API暴露这些功能。它的轻量级设计使其成为微服务架构或需要简单邮件功能的应用程序的理想选择。
更多高级用法和配置选项,请参考Hectane的官方文档和源代码。