Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email 在本地主机smtp上发送邮件不起作用_Email_Go - Fatal编程技术网

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

Email 在本地主机smtp上发送邮件不起作用,email,go,Email,Go,我正在尝试向localhost stmp服务器发送电子邮件。我正在使用该程序接收来自本地主机的电子邮件 请看下面的代码片段 package mail import ( "encoding/base64" "fmt" "log" "net/mail" "net/smtp" "strings" ) func encodeRFC2047(String string) string { // use mail's rfc2047 to enco

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

请看下面的代码片段

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)
    }
}
包裹邮件
进口(
“编码/base64”
“fmt”
“日志”
“网络/邮件”
“网络/smtp”
“字符串”
)
func编码器RFC2047(字符串)字符串{
//使用mail的rfc2047对任何字符串进行编码
地址:=邮件地址{String,“}
返回strings.Trim(addr.String(),“”)
}
func Send(){
//设置身份验证信息。
smtpServer:=“127.0.0.1:2525”
auth:=smtp.PlainAuth(
"",
“管理员”,
“管理员”,
smtpServer,
)
发件人:=邮件地址{“示例”info@example.com"}
收件人:=邮件地址{“客户”customer@example.com"}
标题:=“邮件”
正文:=“这是一封电子邮件确认。”
标题:=make(映射[字符串]字符串)
标题[“From”]=From.String()
标题[“到”]=到.String()
标题[“主题”]=编码RFC2047(标题)
标题[“MIME版本”]=“1.0”
标题[“内容类型”]=“文本/普通;字符集=\“utf-8”
标题[“内容传输编码”]=“base64”
消息:=“”
对于k,v:=范围标头{
message+=fmt.Sprintf(“%s:%s\r\n”,k,v)
}
消息+=“\r\n”+base64.StdEncoding.EncodeToString([]字节(正文))
//连接到服务器,进行身份验证,设置发件人和收件人,
//一步到位,发送电子邮件。
错误:=smtp.SendMail(
smtpServer,
啊,,
发件人地址:,
[]字符串{to.Address},
[]字节(消息),
//[]字节(“这是电子邮件正文。”),
)
如果错误!=零{
log.Fatal(错误)
}
}

当我执行send函数时,我得到了未加密连接的错误。为什么?

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


请记住使用
tcpdump
wireshark
检查实际传输的内容。

您的smtp服务器需要TLS连接吗?我没有上面提到的smtp服务器,只有假的stmp。您的假服务器需要TLS连接吗?你可能需要