You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用gomail.v2发送邮件遇远程主机强制终止连接错误求助

解决gomail.v2发送邮件时“远程主机强制终止连接”问题

我使用gopkg.in/gomail.v2开发邮件发送功能,配置加载正常,但发送邮件时出现错误:An existing connection was forced to be terminated by the remote host。调整TLSConfig配置后问题仍未解决,相关代码如下:

func ConnectSmtpServer(smtpConfig SmtpConfig) (gomail.Dialer, SmtpConfig) {
    // load en .env config
    smtp_config := LoadConfig()

    // connect to smtp server
    smtp_server := gomail.NewDialer(smtp_config.Host, smtp_config.Port, smtp_config.Username, smtp_config.Password)
    smtp_server.TLSConfig = &tls.Config{
        InsecureSkipVerify: true,
        // Configuracion de cifrado
        CipherSuites: []uint16{
            tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
            tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
        },
        PreferServerCipherSuites: true,
        MinVersion:               tls.VersionTLS11,
        MaxVersion:               tls.VersionTLS11, 
    }
    return *smtp_server, smtp_config
}

func SendEmail(to string, subject string, body string, from string, server gomail.Dialer) {
    // prepare email
    new_email := gomail.NewMessage()
    new_email.SetHeader("From", from)
    new_email.SetHeader("To", to)
    new_email.SetHeader("Subject", subject)
    new_email.SetBody("text/html", body)
    
    // send email
    if err := server.DialAndSend(new_email); err != nil {
        // print more information about the error
        fmt.Printf("%s", err)
        panic(err)
    
    } else {
        fmt.Println("Email enviado a: " + to)
    }
}

问题分析与解决方案

1. TLS版本限制过严

你当前强制将TLS版本固定在TLS1.1,目前多数主流邮件服务器已经停止对TLS1.1的支持,直接导致连接被服务器主动终止。建议放宽版本范围到当前通用的安全标准:

MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS13,

2. 自定义加密套件不兼容

指定的CipherSuites可能不被目标SMTP服务器支持,服务器会直接断开连接。可以尝试两种处理方式:

  • 直接注释掉自定义的CipherSuites配置,使用Go默认的加密套件列表
  • 通过openssl s_client -connect 你的SMTP服务器:端口 -starttls smtp命令,查看服务器支持的加密套件,再针对性配置

3. SSL模式与端口不匹配

SMTP端口对应的连接逻辑不同:

  • 465端口:需要直接启用SSL连接,需设置smtp_server.SSL = true
  • 587端口:默认使用STARTTLS升级连接,无需手动设置SSL

4. 增加连接超时

网络延迟可能导致连接被提前终止,给Dialer设置合理的超时时间:

smtp_server.Timeout = 10 * time.Second

修改后的示例代码

import "time"

func ConnectSmtpServer(smtpConfig SmtpConfig) (gomail.Dialer, SmtpConfig) {
    smtp_config := LoadConfig()

    smtp_server := gomail.NewDialer(smtp_config.Host, smtp_config.Port, smtp_config.Username, smtp_config.Password)
    
    // 根据端口设置SSL模式
    if smtp_config.Port == 465 {
        smtp_server.SSL = true
    }
    
    smtp_server.TLSConfig = &tls.Config{
        InsecureSkipVerify: true,
        MinVersion:         tls.VersionTLS12,
        MaxVersion:         tls.VersionTLS13,
        PreferServerCipherSuites: true,
    }
    // 设置10秒连接超时
    smtp_server.Timeout = 10 * time.Second
    
    return *smtp_server, smtp_config
}

func SendEmail(to string, subject string, body string, from string, server gomail.Dialer) {
    new_email := gomail.NewMessage()
    new_email.SetHeader("From", from)
    new_email.SetHeader("To", to)
    new_email.SetHeader("Subject", subject)
    new_email.SetBody("text/html", body)
    
    if err := server.DialAndSend(new_email); err != nil {
        fmt.Printf("发送失败: %v\n", err)
        panic(err)
    } else {
        fmt.Println("邮件已发送至: " + to)
    }
}

额外排查点

  • 确认SMTP账号是否使用授权码而非登录密码(比如QQ、163等国内邮箱)
  • 检查本地防火墙或网络环境是否屏蔽了25/587/465端口
  • 用telnet 你的SMTP服务器 端口测试基础连通性

内容的提问来源于stack exchange,提问作者elAnticrypt0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 15:47:39