出现问题
下面代码为使用 golang 给用户发送邮件功能:
package main
import (
"fmt"
"gopkg.in/gomail.v2"
)
func main() {
subject := "邮件标题"
sendUserEmail := []string{"[email protected]", "[email protected]"}
text := "邮件内容"
fromUser := "发件人"
host := "smtp.163.com"
port := 25
fromUserEmail := "xxxxxxxxxxx"
password := "xxxxxxxxxx"
m := gomail.NewMessage()
m.SetHeader("From", m.FormatAddress(fromUserEmail, fromUser))
m.SetHeader("To", sendUserEmail...)
m.SetHeader("Subject", subject)
m.SetBody("text/html", text)
d := gomail.NewDialer(host, port, fromUserEmail, password)
err := d.DialAndSend(m)
if err != nil {
fmt.Println("err:", err)
return
}
fmt.Println("发送成功!")
}
该代码在本地运行没问题,打包部署到云服务环境上时邮件发送不出去,查看后发现大部分云服务器默认把25端口禁掉的,以华为云为例:
修改建议
- 云服务器申请开通25端口,有些不支持25端口解封
- 修改发送邮件端口
port := 25 修改为 port := 465
标签:25,fromUserEmail,err,端口,失败,服务器发送,port,邮件
From: https://www.cnblogs.com/xingzr/p/18033884