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


Golang Client.Quit方法代码示例

本文整理汇总了Golang中net/smtp.Client.Quit方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.Quit方法的具体用法?Golang Client.Quit怎么用?Golang Client.Quit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net/smtp.Client的用法示例。


在下文中一共展示了Client.Quit方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: smtpSendMailPart2

func smtpSendMailPart2(c *smtp.Client, smtpHost string, a smtp.Auth, from string, to []string, msg []byte) (err error) {
	defer c.Close()
	if err = c.Hello("localhost"); err != nil {
		return err
	}
	if ok, _ := c.Extension("STARTTLS"); ok {
		config := &tls.Config{ServerName: smtpHost}
		if err = c.StartTLS(config); 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
		}
	}
	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:keysonZZZ,项目名称:kmg,代码行数:40,代码来源:SmtpSendEmail.go

示例2: send

func send(c *smtp.Client, m *mail.Message) error {
	if err := c.Mail(config.Message_From); err != nil {
		return fmt.Errorf("while setting From `%s`: %s", config.Message_From, err)
	}

	if config.ScanMessage {
		for _, i := range []string{"To", "Cc", "Bcc"} {
			if 0 == len(m.Header[i]) {
				continue
			}

			if l, err := m.Header.AddressList(i); err != nil {
				fmt.Fprintf(os.Stderr, "ScanMessage: Could not parse recipients in %s `%s`; %s", i, l, err)
			} else {
				for _, v := range l {
					config.Message_To = append(config.Message_To, v.Address)
				}
			}
		}

		if 0 == len(config.Message_To) {
			fmt.Fprintln(os.Stderr, "ScanMessage: No recipients found in message-body")
		}
	}

	for _, to := range config.Message_To {
		if err := c.Rcpt(to); err != nil {
			return fmt.Errorf("while setting Recipient `%s`: %s", to, err)
		}
	}

	w, err := c.Data()
	if err != nil {
		return fmt.Errorf("while setting Data: %s", err)
	}

	var s = ""
	for k, h := range m.Header {
		for _, v := range h {
			s += k + ": " + v + "\r\n"
		}
	}

	b := bytes.Buffer{}
	b.ReadFrom(m.Body)

	if _, err := w.Write([]byte(s + "\r\n" + b.String())); err != nil {
		return fmt.Errorf("while sending message: %s", err)
	}

	if err = w.Close(); err != nil {
		return fmt.Errorf("while closing message: %s", err)
	}

	if err = c.Quit(); err != nil {
		return fmt.Errorf("while closing connection: %s", err)
	}

	return nil
}
开发者ID:parse-nl,项目名称:go-ssmtp,代码行数:60,代码来源:go-ssmtp.go

示例3: sendMail

func sendMail(c *conf.ConfigFile, e Envelope) error {
	var addr string
	var err error
	var conn *smtp.Client

	addr, _ = c.GetString("main", "smtp")

	conn, err = smtp.Dial(addr)
	if err != nil {
		return err
	}

	if err = conn.Mail(e.Sender); err != nil {
		return err
	}

	for _, addr := range e.Recipients {
		if err = conn.Rcpt(addr); err != nil {
			return err
		}
	}

	w, err := conn.Data()
	if err != nil {
		return err
	}

	io.Copy(w, serializeMail(e))
	conn.Quit()
	return nil
}
开发者ID:sprungknoedl,项目名称:postcrypt,代码行数:31,代码来源:encrypt.go

示例4: SendEmail

func SendEmail(address string, from string, toArray []string, subject string, message []byte) (string, *gkerr.GkErrDef) {

	var err error
	var conn *smtp.Client
	var dataWriter io.WriteCloser
	var gkErr *gkerr.GkErrDef
	var sendId string

	conn, err = smtp.Dial(address)
	if err != nil {
		gkErr = gkerr.GenGkErr("smtp.Dial", err, ERROR_ID_SMTP_DIAL)
		return "", gkErr
	}

	conn.Mail(from)
	for _, adr := range toArray {
		conn.Rcpt(adr)
	}

	dataWriter, err = conn.Data()
	if err != nil {
		gkErr = gkerr.GenGkErr("conn.Data", err, ERROR_ID_SMTP_DATA)
		return "", gkErr
	}

	defer dataWriter.Close()

	buf := bytes.NewBufferString("From: " + from + "\nTo: " + toArray[0] + "\nSubject: " + subject + "\n")
	_, err = buf.WriteTo(dataWriter)
	if err != nil {
		gkErr = gkerr.GenGkErr("buf.WriteTo", err, ERROR_ID_SMTP_WRITE)
		return "", gkErr
	}

	buf = bytes.NewBufferString(string(message))
	_, err = buf.WriteTo(dataWriter)
	if err != nil {
		gkErr = gkerr.GenGkErr("buf.WriteTo", err, ERROR_ID_SMTP_WRITE)
		return "", gkErr
	}

	err = conn.Quit()
	if err != nil {
		localError := fmt.Sprintf("%v", err)
		if strings.Index(localError, "Ok") != -1 &&
			strings.Index(localError, "queued as") != -1 {
			sendId = localError
		} else {
			gkErr = gkerr.GenGkErr("conn.Quit", err, ERROR_ID_SMTP_QUIT)
			//gklog.LogTrace(fmt.Sprintf("smtp quit %T [%v] %v",err, err, gkErr))
			return "", gkErr
		}
	}

	return sendId, nil
}
开发者ID:gk-turnip,项目名称:server,代码行数:56,代码来源:gkNetEmail.go

示例5: SendMail

func SendMail(to []string, subject, body string) (err error) {
	if addr == "" {
		// skip if not configured
		return nil
	}
	// create client
	var conn net.Conn
	var client *smtp.Client
	if conn, err = net.Dial("tcp", addr+":25"); err != nil {
		return logError(err, to, subject, body)
	}
	if client, err = smtp.NewClient(conn, ""); err != nil {
		return logError(err, to, subject, body)
	}
	defer client.Quit()
	msg := fmt.Sprintf("From: %s\n", from)
	if err = client.Mail(from); err != nil {
		return logError(err, to, subject, body)
	}
	for _, addr := range to {
		msg += fmt.Sprintf("To: %s\n", addr)
		if err = client.Rcpt(addr); err != nil {
			return logError(err, to, subject, body)
		}
	}
	for _, addr := range cc {
		msg += fmt.Sprintf("Cc: %s\n", addr)
		if err = client.Rcpt(addr); err != nil {
			return logError(err, to, subject, body)
		}
	}
	msg += fmt.Sprintf("Date: %s\n", time.Now().String())
	msg += fmt.Sprintf("Subject: %s\n\n", subject)
	msg += body

	w, err := client.Data()
	if err != nil {
		return logError(err, to, subject, body)
	}

	fmt.Fprintf(w, msg)
	w.Close()
	log.Printf("[SMTP] SENT: %v '%s'", to, subject)
	return nil
}
开发者ID:irregular,项目名称:atlantis-manager,代码行数:45,代码来源:smtp.go

示例6: send

// send does the low level sending of the email
func send(host string, port string, from string, to []string, msg string, auth smtp.Auth, encryption encryption, config *tls.Config, connectTimeout int) error {
	var smtpConnectChannel chan smtpConnectErrorChannel
	var c *smtp.Client = nil
	var err error

	// set the timeout value
	timeout := time.Duration(connectTimeout) * time.Second

	// if there is a timeout, setup the channel and do the connect under a goroutine
	if timeout != 0 {
		smtpConnectChannel = make(chan smtpConnectErrorChannel, 2)
		go func() {
			c, err = smtpConnect(host, port, from, to, msg, auth, encryption, config)
			// send the result
			smtpConnectChannel <- smtpConnectErrorChannel{
				client: c,
				err:    err,
			}
		}()
	}

	if timeout == 0 {
		// no timeout, just fire the connect
		c, err = smtpConnect(host, port, from, to, msg, auth, encryption, config)
	} else {
		// get the connect result or timeout result, which ever happens first
		select {
		case result := <-smtpConnectChannel:
			c = result.client
			err = result.err
		case <-time.After(timeout):
			return errors.New("Mail Error: SMTP Connection timed out")
		}
	}

	// check for connect error
	if err != nil {
		return err
	}

	defer c.Close()

	// Set the sender
	if err := c.Mail(from); err != nil {
		return err
	}

	// Set the recipients
	for _, address := range to {
		if err := c.Rcpt(address); err != nil {
			return err
		}
	}

	// Send the data command
	w, err := c.Data()
	if err != nil {
		return err
	}

	// write the message
	_, err = fmt.Fprint(w, msg)
	if err != nil {
		return err
	}

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

	return c.Quit()
}
开发者ID:dulumao,项目名称:mail-1,代码行数:74,代码来源:mail.go

示例7: sendEmail


//.........这里部分代码省略.........
	from := mail.Address{
		Name:    "",
		Address: viper.GetString("smtp_from"),
	}
	to := mail.Address{
		Name:    "",
		Address: toUser,
	}

	// Setup headers
	headers := make(map[string]string)
	headers["From"] = viper.GetString("smtp_from")
	headers["To"] = to.String()
	headers["Subject"] = subject

	// Setup message
	message := ""
	for k, v := range headers {
		message += fmt.Sprintf("%s: %s\r\n", k, v)
	}
	message += "\r\n" + b.String()

	// Connect to the SMTP Server
	servername := fmt.Sprintf("%s:%s", viper.GetString("smtp_host"), viper.GetString("smtp_port"))

	// TLS config
	tlsconfig := &tls.Config{
		InsecureSkipVerify: true,
		ServerName:         viper.GetString("smtp_host"),
	}

	var c *smtp.Client
	if viper.GetBool("smtp_tls") {

		// Here is the key, you need to call tls.Dial instead of smtp.Dial
		// for smtp servers running on 465 that require an ssl connection
		// from the very beginning (no starttls)

		conn, errc := tls.Dial("tcp", servername, tlsconfig)
		if errc != nil {
			log.Errorf("Error with c.Dial:%s", errc.Error())
			return err
		}

		c, err = smtp.NewClient(conn, viper.GetString("smtp_host"))
		if err != nil {
			log.Errorf("Error with c.NewClient:%s", err.Error())
			return err
		}
	} else {
		c, err = smtp.Dial(servername)
		if err != nil {
			log.Errorf("Error while smtp.Dial:%s", err)
		}
		defer c.Close()
	}

	// Auth
	if viper.GetString("smtp_user") != "" && viper.GetString("smtp_password") != "" {
		auth := smtp.PlainAuth("", viper.GetString("smtp_user"), viper.GetString("smtp_password"), viper.GetString("smtp_host"))
		if err = c.Auth(auth); err != nil {
			log.Errorf("Error with c.Auth:%s", err.Error())
			return err
		}
	}

	// To && From
	if err = c.Mail(from.Address); err != nil {
		log.Errorf("Error with c.Mail:%s", err.Error())
		return err
	}

	if err = c.Rcpt(to.Address); err != nil {
		log.Errorf("Error with c.Rcpt:%s", err.Error())
		return err
	}

	// Data
	w, err := c.Data()
	if err != nil {
		log.Errorf("Error with c.Data:%s", err.Error())
		return err
	}

	_, err = w.Write([]byte(message))
	if err != nil {
		log.Errorf("Error with c.Write:%s", err.Error())
		return err
	}

	err = w.Close()
	if err != nil {
		log.Errorf("Error with c.Close:%s", err.Error())
		return err
	}

	c.Quit()

	return nil
}
开发者ID:ovh,项目名称:tat,代码行数:101,代码来源:email.go

示例8: SendMailMessage

// SendMailMessage handles outgoing message to SMTP server.
//
// - The connections to the service can be either plain (port 25)
//   or SSL/TLS (port 465)
//
// - If the server supports STARTTLS and the channel is not already
//   encrypted (via SSL), the application will use the "STLS" command
//   to initiate a channel encryption.
//
// - Connections can be tunneled through any SOCKS5 proxy (like Tor)
func SendMailMessage(host, proxy, fromAddr, toAddr string, body []byte) error {
	var (
		c0  net.Conn
		c1  *tls.Conn
		cli *smtp.Client
	)
	defer func() {
		if cli != nil {
			cli.Close()
		}
		if c1 != nil {
			c1.Close()
		}
		if c0 != nil {
			c0.Close()
		}
	}()

	uSrv, err := url.Parse(host)
	if err != nil {
		return err
	}
	if proxy == "" {
		c0, err = net.Dial("tcp", uSrv.Host)
	} else {
		host, port, err := SplitHost(uSrv.Host)
		if err != nil {
			return err
		}
		c0, err = Socks5Connect("tcp", host, port, proxy)
	}
	if err != nil {
		return err
	}
	if c0 == nil {
		return errors.New("Can't estabish connection to " + uSrv.Host)
	}

	sslConfig := &tls.Config{InsecureSkipVerify: true}
	if uSrv.Scheme == "smtps" {
		c1 = tls.Client(c0, sslConfig)
		if err = c1.Handshake(); err != nil {
			return err
		}
		cli, err = smtp.NewClient(c1, uSrv.Host)
	} else {
		cli, err = smtp.NewClient(c0, uSrv.Host)
		if err == nil {
			if ok, _ := cli.Extension("STLS"); ok {
				err = cli.StartTLS(sslConfig)
			}
		}
	}
	if err != nil {
		return err
	}
	pw, _ := uSrv.User.Password()
	auth := smtp.PlainAuth("", uSrv.User.Username(), pw, uSrv.Host)
	if err = cli.Auth(auth); err != nil {
		return err
	}
	if err = cli.Mail(fromAddr); err != nil {
		return err
	}
	if err = cli.Rcpt(toAddr); err != nil {
		return err
	}
	wrt, err := cli.Data()
	if err != nil {
		return err
	}
	wrt.Write(body)
	wrt.Close()
	if err = cli.Quit(); err != nil {
		return err
	}
	return nil
}
开发者ID:bfix,项目名称:gospel,代码行数:88,代码来源:smtp.go

示例9: SendMail

func SendMail(timeout time.Duration, addr string, from string, to string, msg []byte) error {
	response := make(chan error, 1)
	var conn *smtp.Client
	var err error

	go func() {
		conn, err = smtp.Dial(addr)
		if err != nil {
			response <- err
			return
		}
		response <- nil
	}()

	select {
	case res := <-response:
		if res == nil {
			go func() {
				defer conn.Close()
				if err = conn.Hello("localhost"); err != nil {
					response <- err
					return
				}
				if ok, _ := conn.Extension("STARTTLS"); ok {
					config := &tls.Config{ServerName: addr}
					if err = conn.StartTLS(config); err != nil {
						response <- err
						return
					}
				}
				if err = conn.Mail(from); err != nil {
					response <- err
					return
				}
				if err = conn.Rcpt(to); err != nil {
					response <- err
					return
				}
				w, err := conn.Data()
				if err != nil {
					response <- err
					return
				}
				_, err = w.Write(msg)
				if err != nil {
					response <- err
					return
				}
				err = w.Close()
				if err != nil {
					response <- err
					return
				}
				response <- conn.Quit()
			}()
			return <-response
		} else {
			return res
		}
	case <-time.After(time.Second * timeout): //don't do the smtp transaction, abandon the socket, it'll timeout after ~3 mins in syn_sent
		return fmt.Errorf("Sending timeout")
	}
}
开发者ID:andrewstucki,项目名称:gomail,代码行数:63,代码来源:smtp.go

示例10: SendWithHELO


//.........这里部分代码省略.........
	}
	stdin, err := cmd.StdinPipe()
	if err != nil {
		return err
	}
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return err
	}

	err = cmd.Start()
	if err != nil {
		return err
	}

	stdin.Write(emailBytes)
	stdin.Close()

	signedData, err := ioutil.ReadAll(stdout)
	if err != nil {
		return err
	}
	err = cmd.Wait()
	if err != nil {
		return err
	}

	var signedEmail bytes.Buffer
	headerToBytes(&signedEmail, e.msgHeaders())
	signedEmail.Write(signedData)
	raw := signedEmail.Bytes()

	// Manually send email using net/smtp

	var c *smtp.Client
	if port == 465 {
		// TLS config
		tlsconfig := &tls.Config{
			InsecureSkipVerify: true,
			ServerName:         hostname,
		}

		// Here is the key, you need to call tls.Dial instead of smtp.Dial
		// for smtp servers running on 465 that require an ssl connection
		// from the very beginning (no starttls)
		conn, err := tls.Dial("tcp", addr, tlsconfig)
		if err != nil {
			return err
		}

		c, err = smtp.NewClient(conn, hostname)
		if err != nil {
			return err
		}
	} else {
		c, err = smtp.Dial(addr)
		if err != nil {
			return err
		}
	}

	defer c.Close()
	if err = c.Hello(heloHostname); err != nil {
		return err
	}
	if ok, _ := c.Extension("STARTTLS"); ok {
		config := &tls.Config{ServerName: hostname}
		if err = c.StartTLS(config); 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.Address); 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(raw)
	if err != nil {
		return err
	}
	err = w.Close()
	if err != nil {
		return err
	}
	return c.Quit()
}
开发者ID:revapost,项目名称:email,代码行数:101,代码来源:email.go


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