renzheng 3 years ago
parent
commit
9b216d2e60
2 changed files with 42 additions and 13 deletions
  1. 3 0
      go.mod
  2. 39 13
      pool.go

+ 3 - 0
go.mod

@@ -0,0 +1,3 @@
+module email
+
+go 1.16

+ 39 - 13
pool.go

@@ -204,32 +204,58 @@ func addAuth(c *client, auth smtp.Auth) (bool, error) {
 	return true, nil
 	return true, nil
 }
 }
 
 
-func (p *Pool) build() (*client, error) {
-	cl, err := smtp.Dial(p.addr)
+func (p *Pool) build() (*client, error) { //修改逻辑为tls
+
+	conn, err := tls.Dial("tcp", p.addr, p.tlsConfig)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	// Is there a custom hostname for doing a HELLO with the SMTP server?
-	if p.helloHostname != "" {
-		cl.Hello(p.helloHostname)
+	c, err := smtp.NewClient(conn, p.tlsConfig.ServerName)
+	if err != nil {
+		return nil, err
 	}
 	}
-
-	c := &client{cl, 0}
-
-	if _, err := startTLS(c, p.tlsConfig); err != nil {
+	if err = c.Hello("localhost"); err != nil {
 		c.Close()
 		c.Close()
 		return nil, err
 		return nil, err
 	}
 	}
 
 
 	if p.auth != nil {
 	if p.auth != nil {
-		if _, err := addAuth(c, p.auth); err != nil {
-			c.Close()
-			return nil, err
+		if ok, _ := c.Extension("AUTH"); ok {
+			if err = c.Auth(p.auth); err != nil {
+				c.Close()
+				return nil, err
+			}
 		}
 		}
 	}
 	}
 
 
-	return c, nil
+	return &client{c, 0}, nil
+
+	// cl, err := smtp.Dial(p.addr)
+	// if err != nil {
+	// 	return nil, err
+	// }
+
+	// // Is there a custom hostname for doing a HELLO with the SMTP server?
+	// if p.helloHostname != "" {
+	// 	cl.Hello(p.helloHostname)
+	// }
+
+	// c := &client{cl, 0}
+
+	// if _, err := startTLS(c, p.tlsConfig); err != nil {
+	// 	c.Close()
+	// 	return nil, err
+	// }
+
+	// if p.auth != nil {
+	// 	if _, err := addAuth(c, p.auth); err != nil {
+	// 		c.Close()
+	// 		return nil, err
+	// 	}
+	// }
+
+	// return c, nil
 }
 }
 
 
 func (p *Pool) maybeReplace(err error, c *client) {
 func (p *Pool) maybeReplace(err error, c *client) {