Commit 83db7387 authored by Alex Jin's avatar Alex Jin Committed by Brad Fitzpatrick

net/smtp: close conn in SendMail; add Client.Close method

R=rsc, dave, bradfitz
CC=golang-dev
https://golang.org/cl/10082044
parent beb6efa0
......@@ -41,12 +41,13 @@ type Client struct {
}
// 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) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
host := addr[:strings.Index(addr, ":")]
host, _, _ := net.SplitHostPort(addr)
return NewClient(conn, host)
}
......@@ -63,6 +64,11 @@ func NewClient(conn net.Conn, host string) (*Client, error) {
return c, nil
}
// Close closes the connection.
func (c *Client) Close() error {
return c.Text.Close()
}
// hello runs a hello exchange if needed.
func (c *Client) hello() error {
if !c.didHello {
......@@ -264,7 +270,8 @@ func SendMail(addr string, a Auth, from string, to []string, msg []byte) error {
if err != nil {
return err
}
if err := c.hello(); err != nil {
defer c.Close()
if err = c.hello(); err != nil {
return err
}
if ok, _ := c.Extension("STARTTLS"); ok {
......
......@@ -238,6 +238,7 @@ func TestNewClient(t *testing.T) {
if err != nil {
t.Fatalf("NewClient: %v\n(after %v)", err, out())
}
defer c.Close()
if ok, args := c.Extension("aUtH"); !ok || args != "LOGIN PLAIN" {
t.Fatalf("Expected AUTH supported")
}
......@@ -278,6 +279,7 @@ func TestNewClient2(t *testing.T) {
if err != nil {
t.Fatalf("NewClient: %v", err)
}
defer c.Close()
if ok, _ := c.Extension("DSN"); ok {
t.Fatalf("Shouldn't support DSN")
}
......@@ -323,6 +325,7 @@ func TestHello(t *testing.T) {
if err != nil {
t.Fatalf("NewClient: %v", err)
}
defer c.Close()
c.localName = "customhost"
err = nil
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment