diff --git a/pop3.go b/pop3.go index 4297313..15ef399 100644 --- a/pop3.go +++ b/pop3.go @@ -17,7 +17,8 @@ import ( // Client implements a Client e-mail client. type Client struct { - opt Opt + opt Opt + dialer Dialer } // Conn is a stateful connection with the POP3 server/ @@ -34,11 +35,16 @@ type Opt struct { // Default is 3 seconds. DialTimeout time.Duration `json:"dial_timeout"` + Dialer Dialer `json:"-"` TLSEnabled bool `json:"tls_enabled"` TLSSkipVerify bool `json:"tls_skip_verify"` } +type Dialer interface { + Dial(network, address string) (net.Conn, error) +} + // MessageID contains the ID and size of an individual message. type MessageID struct { // ID is the numerical index (non-unique) of the message. @@ -64,9 +70,16 @@ func New(opt Opt) *Client { opt.DialTimeout = time.Second * 3 } - return &Client{ - opt: opt, + c := &Client{ + opt: opt, + dialer: opt.Dialer, } + + if c.dialer == nil { + c.dialer = &net.Dialer{Timeout: opt.DialTimeout} + } + + return c } // NewConn creates and returns live POP3 server connection. @@ -75,7 +88,7 @@ func (c *Client) NewConn() (*Conn, error) { addr = fmt.Sprintf("%s:%d", c.opt.Host, c.opt.Port) ) - conn, err := net.DialTimeout("tcp", addr, c.opt.DialTimeout) + conn, err := c.dialer.Dial("tcp", addr) if err != nil { return nil, err }