本文整理汇总了Golang中net/smtp.Dial函数的典型用法代码示例。如果您正苦于以下问题:Golang Dial函数的具体用法?Golang Dial怎么用?Golang Dial使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Dial函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTimeoutClose
func TestTimeoutClose(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
MaxConnections: 1,
ReadTimeout: time.Second,
WriteTimeout: time.Second,
})
defer closer()
c1, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
time.Sleep(time.Second * 2)
c2, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
defer c2.Close()
if err := c1.Mail("[email protected]"); err == nil {
t.Fatal("MAIL succeeded despite being timed out.")
}
if err := c2.Mail("[email protected]"); err != nil {
t.Fatalf("MAIL failed: %v", err)
}
if err := c2.Quit(); err != nil {
t.Fatalf("Quit failed: %v", err)
}
}
示例2: TestSmtpdServer
func TestSmtpdServer(t *testing.T) {
newMailWasReceived = false
srvPort := startServer()
conn, err := smtp.Dial(fmt.Sprintf("localhost:%d", srvPort))
for err != nil {
conn, err = smtp.Dial(fmt.Sprintf("localhost:%d", srvPort))
}
defer conn.Close()
conn.Mail("[email protected]")
conn.Rcpt("[email protected]")
writeCloser, err := conn.Data()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer writeCloser.Close()
buf := bytes.NewBufferString(`Hello World!`)
buf.WriteTo(writeCloser)
if newMailWasReceived != true {
t.Fatalf("Email was not received")
}
}
示例3: TestMaxConnections
func TestMaxConnections(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer ln.Close()
server := &smtpd.Server{
MaxConnections: 1,
}
go func() {
server.Serve(ln)
}()
c1, err := smtp.Dial(ln.Addr().String())
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
_, err = smtp.Dial(ln.Addr().String())
if err == nil {
t.Fatal("Dial succeeded despite MaxConnections = 1")
}
c1.Close()
}
示例4: sendMessage
func sendMessage(mailMessage MailMessage) {
conn, err := smtp.Dial("bulk-s13.nexthosting.ro:5445")
if err != nil {
log.Fatal("err ", err)
}
if err := conn.Mail(mailMessage.from); err != nil {
log.Fatal(err)
}
if err := conn.Rcpt(mailMessage.to); err != nil {
log.Fatal(err)
}
// Send the email body.
wc, err := conn.Data()
if err != nil {
log.Fatal(err)
}
_, err = fmt.Fprintf(wc, getMessageBody(mailMessage))
if err != nil {
log.Fatal(err)
}
err = wc.Close()
if err != nil {
log.Fatal(err)
}
// Send the QUIT command and close the connection.
err = conn.Quit()
if err != nil {
log.Fatal(err)
}
}
示例5: sendMail
func (t Tracker) sendMail(body string) error {
// Connect to the remote SMTP server.
c, err := smtp.Dial(t.config.Mail.ReceiptMail)
if err != nil {
return err
}
defer c.Close()
// Set the sender and recipient.
c.Mail(t.config.Mail.SenderMail)
c.Rcpt(t.config.Mail.ReceiptMail)
// Send the email body.
wc, err := c.Data()
if err != nil {
return err
}
defer wc.Close()
buf := bytes.NewBufferString(body)
if _, err = buf.WriteTo(wc); err != nil {
return err
}
return nil
}
示例6: main
func main() {
c, err := smtp.Dial("mail.it.northwestern.edu:25")
if err != nil {
log.Fatal(err)
}
if err := c.Mail("[email protected]"); err != nil {
log.Fatalf("Error in mail command %s\n", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
log.Fatal(err)
}
wc, err := c.Data()
if err != nil {
log.Fatal(err)
}
_, err = fmt.Fprintf(wc, "This is a test mail from golang")
if err != nil {
log.Fatal(err)
}
err = wc.Close()
if err != nil {
log.Fatal(err)
}
err = c.Quit()
if err != nil {
log.Fatal(err)
}
fmt.Println("sent the email")
}
示例7: sendEmail
func sendEmail(to string, body []byte) error {
c, err := smtp.Dial(*mailServer)
if err != nil {
return err
}
if err = c.Mail(*mailFrom); err != nil {
return err
}
if err = c.Rcpt(to); err != nil {
return err
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write(body)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}
示例8: sendMail
func sendMail(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
c, err := smtp.Dial(addr)
if err != nil {
return err
}
if err = c.StartTLS(nil); err != nil {
return err
}
if err = c.Auth(a); err != nil {
return err
}
if err = c.Mail(from); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write(msg)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}
示例9: TestRecipientCheck
func TestRecipientCheck(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
RecipientChain: []smtpd.Recipient{
smtpd.RecipientFunc(func(x func(conn *smtpd.Connection)) func(*smtpd.Connection) {
return func(conn *smtpd.Connection) {
conn.Error(errors.New("Random error"))
}
}),
},
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("Mail failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err == nil {
t.Fatal("Unexpected RCPT success")
}
}
示例10: SMTPAuth
func SMTPAuth(a smtp.Auth, cfg *SMTPConfig) error {
c, err := smtp.Dial(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
if err != nil {
return err
}
defer c.Close()
if err = c.Hello("gogs"); err != nil {
return err
}
if cfg.TLS {
if ok, _ := c.Extension("STARTTLS"); ok {
if err = c.StartTLS(&tls.Config{
InsecureSkipVerify: cfg.SkipVerify,
ServerName: cfg.Host,
}); err != nil {
return err
}
} else {
return errors.New("SMTP server unsupports TLS")
}
}
if ok, _ := c.Extension("AUTH"); ok {
if err = c.Auth(a); err != nil {
return err
}
return nil
}
return ErrUnsupportedLoginType
}
示例11: TestInvalidRecipient
func TestInvalidRecipient(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer ln.Close()
server := &smtpd.Server{}
go func() {
server.Serve(ln)
}()
c, err := smtp.Dial(ln.Addr().String())
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("Mail failed: %v", err)
}
if err := c.Rcpt("[email protected]@example.org"); err == nil {
t.Fatal("Unexpected RCPT success")
}
}
示例12: TestInvalidHelo
func TestInvalidHelo(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer ln.Close()
server := &smtpd.Server{}
go func() {
server.Serve(ln)
}()
c, err := smtp.Dial(ln.Addr().String())
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Hello(""); err == nil {
t.Fatal("Unexpected HELO success")
}
}
示例13: TestRecipientCheck
func TestRecipientCheck(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer ln.Close()
server := &smtpd.Server{
RecipientChecker: func(peer smtpd.Peer, addr string) error {
return smtpd.Error{Code: 552, Message: "Denied"}
},
}
go func() {
server.Serve(ln)
}()
c, err := smtp.Dial(ln.Addr().String())
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("Mail failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err == nil {
t.Fatal("Unexpected RCPT success")
}
}
示例14: TestHELOCheck
func TestHELOCheck(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
defer ln.Close()
server := &smtpd.Server{
HeloChecker: func(peer smtpd.Peer, name string) error {
if name != "foobar.local" {
t.Fatal("Wrong HELO name")
}
return smtpd.Error{Code: 552, Message: "Denied"}
},
}
go func() {
server.Serve(ln)
}()
c, err := smtp.Dial(ln.Addr().String())
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Hello("foobar.local"); err == nil {
t.Fatal("Unexpected HELO success")
}
}
示例15: TestListenAndServe
func TestListenAndServe(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
addr := ln.Addr().String()
ln.Close()
server := &smtpd.Server{Addr: addr}
go func() {
server.ListenAndServe()
}()
time.Sleep(100 * time.Millisecond)
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Quit(); err != nil {
t.Fatalf("Quit failed: %v", err)
}
}