AI提示词
用go语言直接向mx记录的25端口发邮件,要采用STARTTLS连接方式,要包含Message-ID
package main
import (
"crypto/tls"
"fmt"
"log"
"math/rand"
"net"
"net/mail"
"net/smtp"
"strings"
"time"
)
func main() {
from := "[email protected]"
to := "[email protected]"
subject := "Test Email via STARTTLS"
body := "This is a test email sent using STARTTLS."
err := SendMail(from, to, subject, body)
if err != nil {
log.Fatal(err)
}
fmt.Println("Email sent successfully")
}
func generateMessageID(from string) string {
timestamp := time.Now().UnixNano()
randomPart := rand.Int()
domain := strings.Split(from, "@")[1]
return fmt.Sprintf("<%d.%d@%s>", timestamp, randomPart, domain)
}
func SendMail(from, to, subject, body string) error {
// 1. 解析收件人域名的MX记录
domain := strings.Split(to, "@")[1]
mxRecords, err := net.LookupMX(domain)
if err != nil {
return fmt.Errorf("failed to lookup MX records: %v", err)
}
if len(mxRecords) == 0 {
return fmt.Errorf("no MX records found for domain %s", domain)
}
// 2. 连接到第一个MX记录的25端口
mxHost := mxRecords[0].Host
conn, err := net.Dial("tcp", mxHost+":25")
if err != nil {
return fmt.Errorf("failed to connect to MX host %s: %v", mxHost, err)
}
defer conn.Close()
client, err := smtp.NewClient(conn, mxHost)
if err != nil {
return fmt.Errorf("failed to create SMTP client: %v", err)
}
defer client.Quit()
// 3. 发送 HELO 命令
if err := client.Hello("localhost"); err != nil {
return fmt.Errorf("failed to say HELO: %v", err)
}
// 4. 发送 STARTTLS 命令
if ok, _ := client.Extension("STARTTLS"); !ok {
return fmt.Errorf("STARTTLS not supported by %s", mxHost)
}
config := &tls.Config{ServerName: mxHost, InsecureSkipVerify: true}
if err := client.StartTLS(config); err != nil {
return fmt.Errorf("failed to start TLS: %v", err)
}
// 5. 设置发件人和收件人
if err := client.Mail(from); err != nil {
return fmt.Errorf("failed to set sender: %v", err)
}
if err := client.Rcpt(to); err != nil {
return fmt.Errorf("failed to set recipient: %v", err)
}
// 6. 编写邮件内容,添加Message-ID头
wc, err := client.Data()
if err != nil {
return fmt.Errorf("failed to get write closer: %v", err)
}
msgID := generateMessageID(from)
msg := fmt.Sprintf("Message-ID: %s\r\nFrom: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s", msgID, from, to, subject, body)
_, err = wc.Write([]byte(msg))
if err != nil {
return fmt.Errorf("failed to write message: %v", err)
}
if err := wc.Close(); err != nil {
return fmt.Errorf("failed to close write closer: %v", err)
}
return nil
}
标签:发邮件,return,nil,err,fmt,golang,failed,Errorf,编写
From: https://www.cnblogs.com/xiangxisheng/p/18517267