當前位置: 首頁>>代碼示例>>Golang>>正文


Golang smtp.NewClient函數代碼示例

本文整理匯總了Golang中net/smtp.NewClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClient函數的具體用法?Golang NewClient怎麽用?Golang NewClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestTimeout

func TestTimeout(t *testing.T) {
	s, err := NewServer(&Config{
		Addr:        "127.0.0.1:0",
		ReadTimeout: 50 * time.Millisecond,
	})
	if err != nil {
		t.Fatal(err)
	}
	// Connect to the server using its address
	conn, err := net.Dial("tcp", s.listener.Addr().String())
	if err != nil {
		t.Fatal(err)
	}
	c, err := smtp.NewClient(conn, "localhost")
	if err != nil {
		t.Fatal(err)
	}
	// Hang for 100ms to trigger the timeout
	time.Sleep(100 * time.Millisecond)
	conn.SetDeadline(time.Now().Add(100 * time.Millisecond))
	if err := c.Hello("localhost"); err == nil {
		t.Fatal(errors.New("timeout expected"))
	}
	s.Close(false)
}
開發者ID:hectane,項目名稱:go-smtpsrv,代碼行數:25,代碼來源:server_test.go

示例2: sendMail

func sendMail(t *testing.T, unixSocket, testData string) {
	conn, err := net.Dial("unix", unixSocket)
	if err != nil {
		t.Error(err)
	}

	c, err := smtp.NewClient(conn, "localhost")
	defer c.Quit()

	if err != nil {
		t.Error(err)
	}

	c.Mail("ignored")
	c.Rcpt("ignored")

	w, err := c.Data()
	defer w.Close()
	if err != nil {
		t.Error(err)
	}

	data, err := ioutil.ReadFile(testData)
	if err != nil {
		t.Error(err)
	}

	w.Write(data)
}
開發者ID:t6,項目名稱:voicemail,代碼行數:29,代碼來源:smtp_test.go

示例3: newSMTPClient

func newSMTPClient(conn net.Conn, config *model.Config) (*smtp.Client, *model.AppError) {
	c, err := smtp.NewClient(conn, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
	if err != nil {
		l4g.Error("Failed to open a connection to SMTP server %v", err)
		return nil, model.NewAppError("SendMail", "Failed to open TLS connection", err.Error())
	}
	// GO does not support plain auth over a non encrypted connection.
	// so if not tls then no auth
	auth := smtp.PlainAuth("", config.EmailSettings.SMTPUsername, config.EmailSettings.SMTPPassword, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
	if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_TLS {
		if err = c.Auth(auth); err != nil {
			return nil, model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error())
		}
	} else if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_STARTTLS {
		tlsconfig := &tls.Config{
			InsecureSkipVerify: true,
			ServerName:         config.EmailSettings.SMTPServer,
		}
		c.StartTLS(tlsconfig)
		if err = c.Auth(auth); err != nil {
			return nil, model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error())
		}
	}
	return c, nil
}
開發者ID:marslabtron,項目名稱:platform,代碼行數:25,代碼來源:mail.go

示例4: SendTestMessage

// Send the given email messages using this Mailer.
func (m *Mailer) SendTestMessage(basicServer string, messages ...*Message) (actualcmds string, err error) {
	if m.Auth == nil {
		m.Auth = smtp.PlainAuth(m.UserName, m.UserName, m.Password, m.Server)
	}

	server := strings.Join(strings.Split(basicServer, "\n"), "\r\n")
	var cmdbuf bytes.Buffer
	bcmdbuf := bufio.NewWriter(&cmdbuf)
	var fake faker
	fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf)

	defer func() {
		bcmdbuf.Flush()
		actualcmds = cmdbuf.String()
	}()

	c, err := smtp.NewClient(fake, "fake.host")
	if err != nil {
		return
	}
	defer c.Quit()

	for _, message := range messages {
		m.fillDefault(message)
		if err = Send(c, message); err != nil {
			return
		}
	}

	return
}
開發者ID:nangong92t,項目名稱:go_src,代碼行數:32,代碼來源:mailer_test.go

示例5: newSMTPClient

func newSMTPClient(conn net.Conn) (*smtp.Client, *model.AppError) {
	host, _, _ := net.SplitHostPort(Cfg.EmailSettings.SMTPServer)
	c, err := smtp.NewClient(conn, host)
	if err != nil {
		l4g.Error("Failed to open a connection to SMTP server %v", err)
		return nil, model.NewAppError("SendMail", "Failed to open TLS connection", err.Error())
	}
	// GO does not support plain auth over a non encrypted connection.
	// so if not tls then no auth
	auth := smtp.PlainAuth("", Cfg.EmailSettings.SMTPUsername, Cfg.EmailSettings.SMTPPassword, host)
	if Cfg.EmailSettings.UseTLS {
		if err = c.Auth(auth); err != nil {
			return nil, model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error())
		}
	} else if Cfg.EmailSettings.UseStartTLS {
		tlsconfig := &tls.Config{
			InsecureSkipVerify: true,
			ServerName:         host,
		}
		c.StartTLS(tlsconfig)
		if err = c.Auth(auth); err != nil {
			return nil, model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error())
		}
	}
	return c, nil
}
開發者ID:ConsultingMD,項目名稱:platform,代碼行數:26,代碼來源:mail.go

示例6: newSMTPClient

func newSMTPClient(conn net.Conn, config *model.Config) (*smtp.Client, *model.AppError) {
	c, err := smtp.NewClient(conn, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
	if err != nil {
		l4g.Error(T("utils.mail.new_client.open.error"), err)
		return nil, model.NewLocAppError("SendMail", "utils.mail.connect_smtp.open_tls.app_error", nil, err.Error())
	}
	auth := smtp.PlainAuth("", config.EmailSettings.SMTPUsername, config.EmailSettings.SMTPPassword, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
	if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_TLS {
		if err = c.Auth(auth); err != nil {
			return nil, model.NewLocAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error())
		}
	} else if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_STARTTLS {
		tlsconfig := &tls.Config{
			InsecureSkipVerify: true,
			ServerName:         config.EmailSettings.SMTPServer,
		}
		c.StartTLS(tlsconfig)
		if err = c.Auth(auth); err != nil {
			return nil, model.NewLocAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error())
		}
	} else if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_PLAIN {
		// note: go library only supports PLAIN auth over non-tls connections
		if err = c.Auth(auth); err != nil {
			return nil, model.NewLocAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error())
		}
	}
	return c, nil
}
開發者ID:RangerRick,項目名稱:platform,代碼行數:28,代碼來源:mail.go

示例7: dial

// dial connects to the smtp server with the request encryption type
func dial(host string, port string, encryption encryption, config *tls.Config) (*smtp.Client, error) {
	var conn net.Conn
	var err error

	address := host + ":" + port

	// do the actual dial
	switch encryption {
	case EncryptionSSL:
		conn, err = tls.Dial("tcp", address, config)
	default:
		conn, err = net.Dial("tcp", address)
	}

	if err != nil {
		return nil, errors.New("Mail Error on dailing with encryption type " + encryption.String() + ": " + err.Error())
	}

	c, err := smtp.NewClient(conn, host)

	if err != nil {
		return nil, errors.New("Mail Error on smtp dial: " + err.Error())
	}

	return c, err
}
開發者ID:dulumao,項目名稱:mail-1,代碼行數:27,代碼來源:mail.go

示例8: SendAlert

func (e *EmailAlerter) SendAlert(errors map[string][]string) error {
	log.Println("dialing:", e.Smtp)

	conn, err := net.DialTimeout("tcp", e.Smtp, 15*time.Second)
	if err != nil {
		return err
	}
	defer conn.Close()

	c, err := smtp.NewClient(conn, e.Smtp)
	if err != nil {
		return err
	}
	defer c.Close()

	c.Mail(e.From)
	c.Rcpt(e.To)

	wc, err := c.Data()
	if err != nil {
		return err
	}
	defer wc.Close()

	buf := bytes.NewBufferString(fmt.Sprintf("%v", errors))
	if _, err = buf.WriteTo(wc); err != nil {
		return err
	}

	return nil
}
開發者ID:mnadel,項目名稱:logmon,代碼行數:31,代碼來源:alert.go

示例9: Dial

func Dial(addr string) (*smtp.Client, error) {
	conn, err := tls.Dial("tcp", addr, nil) //smtp包中net.Dial()當使用ssl連接時會卡住
	if err != nil {
		fmt.Println(err.Error())
		return nil, err
	}
	host, _, _ := net.SplitHostPort(addr)
	return smtp.NewClient(conn, host)
}
開發者ID:NotBadPad,項目名稱:go-learn,代碼行數:9,代碼來源:mail.go

示例10: Dial

//return a smtp client
func Dial(addr string) (*smtp.Client, error) {
	conn, err := tls.Dial("tcp", addr, nil)
	if err != nil {
		log.Println("Dialing Error:", err)
		return nil, err
	}
	//分解主機端口字符串
	host, _, _ := net.SplitHostPort(addr)
	return smtp.NewClient(conn, host)
}
開發者ID:suntong,項目名稱:lang,代碼行數:11,代碼來源:sendMail-SSL2.go

示例11: sendSSL

// SSL方式連接服務器 (與STARTTLS方式不同 適用於465端口)
func (this *SMTPClient) sendSSL(to []string, title, context string) (err error) {
	defer util.Catch(&err)
	host := this.Server[:strings.Index(this.Server, ":")]
	conn, err := tls.Dial("tcp", this.Server, nil)
	if err != nil {
		return err
	}
	c, err := smtp.NewClient(conn, host)
	if err != nil {
		return err
	}
	*(*bool)(unsafe.Pointer(reflect.ValueOf(c).Elem().FieldByName("tls").UnsafeAddr())) = true // set tls to true for AUTH

	if this.auth != nil {
		if ok, _ := c.Extension("AUTH"); ok {
			if err = c.Auth(this.auth); err != nil {
				return err
			}
		}
	}
	if err = c.Mail(this.User); 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
	}
	body := []byte(strings.Join([]string{
		"From: " + this.User,
		"To: " + strings.Join(to, ","),
		"Subject: " + title,
		"Content-Type: text/html; charset=utf-8;",
		"",
		context,
	}, "\r\n"))
	_, err = w.Write(body)
	if err != nil {
		return err
	}

	err = w.Close()
	if err != nil {
		return err
	}

	return c.Quit()
}
開發者ID:pa001024,項目名稱:reflex,代碼行數:54,代碼來源:SMTP.go

示例12: sendMail

// sendMail connects to the server at addr, switches to TLS if possible (using the given config),
// authenticates with mechanism a if possible, and then sends an email from
// address from, to addresses to, with message msg.
//
// If msg is nil, then quits, this testing the recipients and the server
func sendMail(addr string, a smtp.Auth, from string, to []string, msg []byte, timeout time.Duration, tlsConfig *tls.Config) error {
	//c, err := Dial(addr)
	conn, err := net.DialTimeout("tcp", addr, timeout)
	if err != nil {
		return err
	}
	host, _, _ := net.SplitHostPort(addr)
	c, err := smtp.NewClient(conn, host)
	if err != nil {
		return err
	}
	//if err := c.hello(); err != nil {
	//    return err
	//}
	if err := c.Hello("localhost"); err != nil {
		return err
	}
	if ok, _ := c.Extension("STARTTLS"); ok {
		if err = c.StartTLS(tlsConfig); err != nil {
			return err
		}
	}
	if a != nil {
		if ok, _ := c.Extension("AUTH"); ok {
			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
		}
	}
	if msg != nil {
		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:wxdublin,項目名稱:heka-plugins,代碼行數:58,代碼來源:email_output.go

示例13: send_digest

// copy & past https://gist.github.com/chrisgillis/10888032
func send_digest(smtp_conn smtp_conn_type, msg []byte) error {
	servername := strings.Join([]string{smtp_conn.Host, smtp_conn.Port}, ":")

	auth := smtp.PlainAuth("", smtp_conn.Login, smtp_conn.Password, smtp_conn.Host)

	// TLS config
	tlsconfig := &tls.Config{
		InsecureSkipVerify: true,
		ServerName:         smtp_conn.Host,
	}

	conn, err := tls.Dial("tcp", ""+servername, tlsconfig)
	if err != nil {
		return err
	}

	c, err := smtp.NewClient(conn, smtp_conn.Host)
	if err != nil {
		return err
	}

	if err = c.Auth(auth); err != nil {
		return err
	}

	if err = c.Mail(smtp_conn.Login); err != nil {
		return err
	}

	if err = c.Rcpt(email); 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
	}

	c.Quit()

	return nil
}
開發者ID:nikonor,項目名稱:rss-downloader,代碼行數:53,代碼來源:rss-downloader.go

示例14: sendEmail

func sendEmail(recipientAddress string, subject string, messageBody string) {
	if !credentialsHaveBeenLoaded {
		log.Panic("Outgoing email credentials have not been set. Cannot send message.")
	}

	from := mail.Address{credentials.NoReplyAddressName, credentials.NoReplyAddress}

	headers := make(map[string]string)
	headers["From"] = from.String()
	headers["To"] = recipientAddress
	headers["Subject"] = subject

	message := ""
	for headerName, headerValue := range headers {
		message += fmt.Sprintf("%s: %s\r\n", headerName, headerValue)
	}
	message += "\r\n" + messageBody

	mailAuth := smtp.PlainAuth("", credentials.NoReplyAddress, credentials.NoReplyPassword, credentials.Host)

	tlsConfig := &tls.Config{
		InsecureSkipVerify: true,
		ServerName:         credentials.Host,
	}

	tcpConnection, error := tls.Dial("tcp", credentials.Host+":"+credentials.Port, tlsConfig)
	panicOnError(error)

	smtpClient, error := smtp.NewClient(tcpConnection, credentials.Host)
	panicOnError(error)

	error = smtpClient.Auth(mailAuth)
	panicOnError(error)

	error = smtpClient.Mail(credentials.NoReplyAddress)
	panicOnError(error)

	error = smtpClient.Rcpt(recipientAddress)
	panicOnError(error)

	emailStream, error := smtpClient.Data()
	panicOnError(error)

	_, error = emailStream.Write([]byte(message))
	panicOnError(error)

	error = emailStream.Close()
	panicOnError(error)

	smtpClient.Quit()
}
開發者ID:spyrosoft,項目名稱:firefractal.com,代碼行數:51,代碼來源:email.go

示例15: SendMailSSL

// SendMailSSL envoie un email par SSL
func SendMailSSL(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
	conn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: true}) //TODO: Not secure
	if err != nil {
		log.Println("Error Dialing", err)
		return err
	}
	h, _, _ := net.SplitHostPort(addr)
	c, err := smtp.NewClient(conn, h)
	if err != nil {
		log.Println("Error SMTP connection", err)
		return err
	}
	defer c.Close()

	if a != nil {
		if ok, _ := c.Extension("AUTH"); ok {
			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:DamienFontaine,項目名稱:lunarc,代碼行數:50,代碼來源:mail.go


注:本文中的net/smtp.NewClient函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。