topskip
topskip

Reputation: 17345

Debugging SMTP connection

I use go's net/smtp to send emails, and it works fine for some emails but not for others. I get an 554 5.5.1 Error: no valid recipients but I am pretty sure I give the correct mail address.

(The ultimate goal is to make the net/smtp work for all mail recipients. So answers on that are welcome as well)

How can I debug what's going on in the background? What commands are sent to and from the SMTP server? I'd like to replay the commands from the command line (telnet) to get find out more about the error.

This is the code I use (from the go-wiki):

package main

import (
        "bytes"
        "log"
        "net/smtp"
)

func main() {
        // Connect to the remote SMTP server.
        c, err := smtp.Dial("mail.example.com:25")
        if err != nil {
                log.Fatal(err)
        }
        // Set the sender and recipient.
        c.Mail("[email protected]")
        c.Rcpt("[email protected]")
        // Send the email body.
        wc, err := c.Data()
        if err != nil {
                log.Fatal(err)
        }
        defer wc.Close()
        buf := bytes.NewBufferString("This is the email body.")
        if _, err = buf.WriteTo(wc); err != nil {
                log.Fatal(err)
        }
}

I know this is a very vague question, but any help is much appreciated.

Upvotes: 4

Views: 1921

Answers (1)

topskip
topskip

Reputation: 17345

It looks (after some debugging), that the servers who rejected the valid email addresses require a EHLO command with a proper hostname, not just localhost which is the default as documented

So when I insert a line in the beginning like this

c.Hello("example.com") // use real server name here

everything works fine. BUT: (note to myself): never run code without proper error checking. It helped me to check the error on all the commands such as

err = c.Hello("example.com")
...
err = c.Mail("[email protected]")
...
err = c.Rcpt("[email protected]")
...

I am not sure anymore which error message gave me the hint to use a proper hostname, but it was something like:

550 5.7.1 <localhost>: Helo command rejected: Don't use hostname localhost

Upvotes: 4

Related Questions