当前位置: 首页>>代码示例>>Golang>>正文


Golang smtp.Dial函数代码示例

本文整理汇总了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)
	}
}
开发者ID:tiare-lava,项目名称:smtpd,代码行数:33,代码来源:smtpd_test.go

示例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")
	}
}
开发者ID:viovanov,项目名称:bosh-agent,代码行数:26,代码来源:smtpd_test.go

示例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()
}
开发者ID:dieucao,项目名称:notifications,代码行数:29,代码来源:smtpd_test.go

示例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)
	}
}
开发者ID:LicaSterian,项目名称:powerFeeder,代码行数:34,代码来源:main.go

示例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
}
开发者ID:AdriVillaB,项目名称:tools,代码行数:25,代码来源:gh-tracker.go

示例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")
}
开发者ID:dictyBase,项目名称:webhooks,代码行数:30,代码来源:send.go

示例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()
}
开发者ID:jk-cbfs,项目名称:cbugg,代码行数:25,代码来源:email.go

示例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()
}
开发者ID:0x7cc,项目名称:rsc,代码行数:33,代码来源:send.go

示例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")
	}
}
开发者ID:tiare-lava,项目名称:smtpd,代码行数:25,代码来源:smtpd_test.go

示例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
}
开发者ID:Janfred,项目名称:gogs,代码行数:32,代码来源:login.go

示例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")
	}

}
开发者ID:dieucao,项目名称:notifications,代码行数:29,代码来源:smtpd_test.go

示例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")
	}

}
开发者ID:dieucao,项目名称:notifications,代码行数:25,代码来源:smtpd_test.go

示例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")
	}

}
开发者ID:dieucao,项目名称:notifications,代码行数:33,代码来源:smtpd_test.go

示例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")
	}

}
开发者ID:dieucao,项目名称:notifications,代码行数:32,代码来源:smtpd_test.go

示例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)
	}

}
开发者ID:dieucao,项目名称:notifications,代码行数:29,代码来源:smtpd_test.go


注:本文中的net/smtp.Dial函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。