gomail无身份验证示例崩溃

huangapple go评论94阅读模式
英文:

gomail noauth example crashes

问题

我正在尝试按照这个示例进行操作:

package main

import (
	"gopkg.in/gomail.v2"
)

func main() {
	m := gomail.NewMessage()
	m.SetHeader("From", "from@example.com")
	m.SetHeader("To", "to@example.com")
	m.SetHeader("Subject", "Hello!")
	m.SetBody("text/plain", "Hello!")

	d := gomail.Dialer{Host: "localhost", Port: 587}
	if err := d.DialAndSend(m); err != nil {
		panic(err)
	}
}

但是我遇到了以下错误:

panic: 454 4.7.0 TLS not available due to local problem

goroutine 1 [running]:
panic(0x244ca0, 0xc820010b20)
	/usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
main.main()
	/Users/kaiyin/IdeaProjects/gomail/main.go:19 +0x342
exit status 2

我还尝试禁用TLS和SSL:

d := gomail.Dialer{Host: "localhost", Port: 587, SSL: false, TLSConfig: nil}

但是错误仍然存在。


我查看了mail.log(来自OSX 10.10上的postfix):

Apr  7 14:28:05 kaiyins-mbp postfix/smtpd[64525]: warning: No server certs available. TLS won't be enabled
Apr  7 14:28:05 kaiyins-mbp postfix/smtpd[64525]: connect from localhost[::1]
Apr  7 14:28:05 kaiyins-mbp postfix/smtpd[64525]: lost connection after STARTTLS from localhost[::1]

所以我按照以下方式生成了证书:

cd /etc/postfix
sudo openssl req -new -outform PEM -out smtpd.cert -newkey rsa:2048 -nodes -keyout smtpd.key -keyform PEM -days 365 -x509

并更改了postfix的设置:

smtpd_enforce_tls = no
smtpd_tls_loglevel = 1
smtpd_use_tls = yes
smtpd_tls_key_file = /etc/postfix/smtpd.key
smtpd_tls_cert_file = /etc/postfix/smtpd.cert

现在我得到了一个不同的错误:

panic: x509: certificate signed by unknown authority

goroutine 1 [running]:
panic(0x26e3c0, 0xc8203e4b80)
	/usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
main.main()
	/Users/kaiyin/IdeaProjects/gomail/main.go:19 +0x32f
exit status 2

我尝试按照这里的指示生成证书:http://www.akadia.com/services/ssh_test_certificate.html

然后我又遇到了另一个错误:

panic: x509: certificate is valid for Kaiyin Zhong, not localhost

goroutine 1 [running]:
panic(0x26a7e0, 0xc8203e6800)
	/usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
main.main()
	/Users/kaiyin/IdeaProjects/gomail/main.go:19 +0x32f
exit status 2

我假设我可能弄错了服务器名称,所以我重新进行了所有操作,将服务器FQDN设置为localhost:

Common Name (e.g. server FQDN or YOUR name) []:localhost

然后我又回到了之前的错误:

panic: x509: certificate signed by unknown authority

goroutine 1 [running]:
panic(0x26e3c0, 0xc8203e6800)
	/usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
main.main()
	/Users/kaiyin/IdeaProjects/gomail/main.go:19 +0x32f
exit status 2
英文:

I am trying to follow this example:

package main

import (
	"gopkg.in/gomail.v2"
)




func main() {
	m := gomail.NewMessage()
	m.SetHeader("From", "from@example.com")
	m.SetHeader("To", "to@example.com")
	m.SetHeader("Subject", "Hello!")
	m.SetBody("text/plain", "Hello!")

	d := gomail.Dialer{Host: "localhost", Port: 587}
	if err := d.DialAndSend(m); err != nil {
		panic(err)
	}
} //main

And I got this error:

panic: 454 4.7.0 TLS not available due to local problem

goroutine 1 [running]:
panic(0x244ca0, 0xc820010b20)
	/usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
main.main()
	/Users/kaiyin/IdeaProjects/gomail/main.go:19 +0x342
exit status 2

I also tried disabling TLS and SSL:

	d := gomail.Dialer{Host: "localhost", Port: 587, SSL: false, TLSConfig: nil}

But the error still persists.


I had a look into the mail.log (from postfix on osx 10.10):

Apr  7 14:28:05 kaiyins-mbp postfix/smtpd[64525]: warning: No server certs available. TLS won't be enabled
Apr  7 14:28:05 kaiyins-mbp postfix/smtpd[64525]: connect from localhost[::1]
Apr  7 14:28:05 kaiyins-mbp postfix/smtpd[64525]: lost connection after STARTTLS from localhost[::1]

So I generated certs like this:

cd /etc/postfix
sudo openssl req -new -outform PEM -out smtpd.cert    -newkey rsa:2048 -nodes -keyout smtpd.key -keyform PEM    -days 365 -x509

And changed postfix settings:

smtpd_enforce_tls                = no
smtpd_tls_loglevel               = 1
smtpd_use_tls                    = yes
smtpd_tls_key_file               = /etc/postfix/smtpd.key
smtpd_tls_cert_file              = /etc/postfix/smtpd.cert

Now I get a different error:

panic: x509: certificate signed by unknown authority

goroutine 1 [running]:
panic(0x26e3c0, 0xc8203e4b80)
	/usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
main.main()
	/Users/kaiyin/IdeaProjects/gomail/main.go:19 +0x32f
exit status 2

I tried to generate a cert as instructed here: http://www.akadia.com/services/ssh_test_certificate.html

And I got yet another error:

panic: x509: certificate is valid for Kaiyin Zhong, not localhost

goroutine 1 [running]:
panic(0x26a7e0, 0xc8203e6800)
	/usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
main.main()
	/Users/kaiyin/IdeaProjects/gomail/main.go:19 +0x32f
exit status 2

I assumed that I got the server name wrong, so I did it all over again, with the server FQDN set to localhost:

Common Name (e.g. server FQDN or YOUR name) []:localhost

Then I got back to the previous error:

panic: x509: certificate signed by unknown authority

goroutine 1 [running]:
panic(0x26e3c0, 0xc8203e6800)
	/usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
main.main()
	/Users/kaiyin/IdeaProjects/gomail/main.go:19 +0x32f
exit status 2

答案1

得分: 1

设置 InsecureSkipVerify 选项:

package main

import (
    "crypto/tls"

    "gopkg.in/gomail.v2"
)

func main() {
    d := gomail.NewDialer("smtp.example.com", 587, "user", "123456")
    d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

    // 使用 d 发送邮件。
}

来源:https://github.com/go-gomail/gomail

英文:

Set the InsecureSkipVerify option:

package main

import (
    "crypto/tls"

    "gopkg.in/gomail.v2"
)

func main() {
    d := gomail.NewDialer("smtp.example.com", 587, "user", "123456")
    d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

    // Send emails using d.
}

Source: https://github.com/go-gomail/gomail

huangapple
  • 本文由 发表于 2016年4月7日 20:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/36476601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定