gpt4 book ai didi

email - 在本地主机 smtp 上发送邮件不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 03:28:57 25 4
gpt4 key购买 nike

我正在尝试向本地主机 stmp 服务器发送电子邮件。我正在使用 fakesmtp接收来自本地主机的电子邮件的程序。

看下面的代码片段

package mail

import (
"encoding/base64"
"fmt"
"log"
"net/mail"
"net/smtp"
"strings"
)

func encodeRFC2047(String string) string {
// use mail's rfc2047 to encode any string
addr := mail.Address{String, ""}
return strings.Trim(addr.String(), " <>")
}

func Send() {
// Set up authentication information.

smtpServer := "127.0.0.1:2525"
auth := smtp.PlainAuth(
"",
"admin",
"admin",
smtpServer,
)

from := mail.Address{"example", "info@example.com"}
to := mail.Address{"customer", "customer@example.com"}
title := "Mail"

body := "This is an email confirmation."

header := make(map[string]string)
header["From"] = from.String()
header["To"] = to.String()
header["Subject"] = encodeRFC2047(title)
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/plain; charset=\"utf-8\""
header["Content-Transfer-Encoding"] = "base64"

message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))

// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
err := smtp.SendMail(
smtpServer,
auth,
from.Address,
[]string{to.Address},
[]byte(message),
//[]byte("This is the email body."),
)
if err != nil {
log.Fatal(err)
}
}

当我执行发送函数时,出现未加密连接错误。为什么?

最佳答案

服务器很可能不允许您在未加密的连接上使用纯文本身份验证,这对于几乎所有 MTA 都是合理的默认设置。将身份验证信息更改为例如摘要,或在您的客户端代码中启用 SSL/TLS。

记得使用tcpdumpwireshark 来检查实际传输的内容。

关于email - 在本地主机 smtp 上发送邮件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26827093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com