mirror of
https://github.com/golang/go
synced 2024-11-25 20:47:58 -07:00
net/smtp: close conn in SendMail; add Client.Close method
R=rsc, dave, bradfitz CC=golang-dev https://golang.org/cl/10082044
This commit is contained in:
parent
beb6efa0fb
commit
83db738786
@ -41,12 +41,13 @@ type Client struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Dial returns a new Client connected to an SMTP server at addr.
|
// Dial returns a new Client connected to an SMTP server at addr.
|
||||||
|
// The addr must include a port number.
|
||||||
func Dial(addr string) (*Client, error) {
|
func Dial(addr string) (*Client, error) {
|
||||||
conn, err := net.Dial("tcp", addr)
|
conn, err := net.Dial("tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
host := addr[:strings.Index(addr, ":")]
|
host, _, _ := net.SplitHostPort(addr)
|
||||||
return NewClient(conn, host)
|
return NewClient(conn, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,6 +64,11 @@ func NewClient(conn net.Conn, host string) (*Client, error) {
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes the connection.
|
||||||
|
func (c *Client) Close() error {
|
||||||
|
return c.Text.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// hello runs a hello exchange if needed.
|
// hello runs a hello exchange if needed.
|
||||||
func (c *Client) hello() error {
|
func (c *Client) hello() error {
|
||||||
if !c.didHello {
|
if !c.didHello {
|
||||||
@ -264,7 +270,8 @@ func SendMail(addr string, a Auth, from string, to []string, msg []byte) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := c.hello(); err != nil {
|
defer c.Close()
|
||||||
|
if err = c.hello(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if ok, _ := c.Extension("STARTTLS"); ok {
|
if ok, _ := c.Extension("STARTTLS"); ok {
|
||||||
|
@ -238,6 +238,7 @@ func TestNewClient(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("NewClient: %v\n(after %v)", err, out())
|
t.Fatalf("NewClient: %v\n(after %v)", err, out())
|
||||||
}
|
}
|
||||||
|
defer c.Close()
|
||||||
if ok, args := c.Extension("aUtH"); !ok || args != "LOGIN PLAIN" {
|
if ok, args := c.Extension("aUtH"); !ok || args != "LOGIN PLAIN" {
|
||||||
t.Fatalf("Expected AUTH supported")
|
t.Fatalf("Expected AUTH supported")
|
||||||
}
|
}
|
||||||
@ -278,6 +279,7 @@ func TestNewClient2(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("NewClient: %v", err)
|
t.Fatalf("NewClient: %v", err)
|
||||||
}
|
}
|
||||||
|
defer c.Close()
|
||||||
if ok, _ := c.Extension("DSN"); ok {
|
if ok, _ := c.Extension("DSN"); ok {
|
||||||
t.Fatalf("Shouldn't support DSN")
|
t.Fatalf("Shouldn't support DSN")
|
||||||
}
|
}
|
||||||
@ -323,6 +325,7 @@ func TestHello(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("NewClient: %v", err)
|
t.Fatalf("NewClient: %v", err)
|
||||||
}
|
}
|
||||||
|
defer c.Close()
|
||||||
c.localName = "customhost"
|
c.localName = "customhost"
|
||||||
err = nil
|
err = nil
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user