當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。