在Go中使用smtp发送Gmail

I was able to send email with gmail via smtp before, but not anymore. I am still able to send from localhost on my computer on Windows where I develop, but not from the production server. I have enabled "Allow less secure apps" with my gmail account. But still this is the error I get:

2016/06/07 10:27:03 534 5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtq
5.7.14 ZHRyO8gjNXDZjjls6t7JWkP7y1UM8Jc44oPvw0dXO96SljvNT3d2QDMlJAT3X7p4teLYY1
5.7.14 zj9NBwLOytlrdRXkjm7BZNWpPLLscyxEOUcyLL3EFHdnYiq_1P_PwQVcLvZlVGUdChm_
5.7.14 6BIdD9uJLkOOG_6b6oXyJ1YfZJneoM8IiUYlWaecNECJQYF8WLYY3hSCCnNDazjJuRvjbH
5.7.14 yqJu4xBfx-929yDQwvPmuVxL4bQv8> Please log in via your web browser and
5.7.14 then try again.
5.7.14  Learn more at
5.7.14  https://support.google.com/mail/answer/78754 89sm7243327qth.2 - gsmtp

enter image description here

It says that It has detected "an unknown device" which is the production server, but there is NO WAY for me to confirm that that device is legit. How am I supposed to send emails?

This is the code I have written to send emails, note that it works when I send from localhost on my Windows development environment, just not from the production server.

type Mail struct {
    Hostname   string
    Port       string
    User       string
    Password   string
    Header     map[string]string
    IsHTML     bool
    Subject    string
    Body       string
    Recipients []string
}

func New() *Mail {
    return &Mail{
        Hostname: cfg.MailHost,
        Port:     cfg.MailPort,
        User:     cfg.MailUser,
        Password: cfg.MailPass,
        Header:   map[string]string{},
        IsHTML:   true,
    }
}

func (m *Mail) AddHeader(key, value string) {
    m.Header[key] = value
}

func (m *Mail) AddRecipient(address string) {
    m.Recipients = append(m.Recipients, address)
    m.AddHeader("To", "<"+address+">")
}

func (m *Mail) HTML(isHTML bool) {
    m.IsHTML = isHTML
}

func (m *Mail) Send() error {

    if m.Subject == "" {
        m.Subject = "Some random subject..."
    }
    m.AddHeader("Subject", m.Subject)

    if _, ok := m.Header["From"]; !ok {
        m.AddHeader("From", cfg.MailName+" <"+cfg.MailUser+">")
    }

    if m.IsHTML {
        m.AddHeader("MIME-version", "1.0;")
        m.AddHeader("Content-Type", "text/html; charset=\"UTF-8\";")
    }

    body := ""
    for k, v := range m.Header {
        body += fmt.Sprintf("%s: %s
", k, v)
    }
    body += "
" + m.Body

    auth := smtp.PlainAuth("", m.User, m.Password, m.Hostname)

    err := smtp.SendMail(
        m.Hostname+":"+m.Port,
        auth,
        m.User,
        m.Recipients,
        []byte(body),
    )

    if err != nil {
        return err
    }
    return nil
}

I have no idea what to do now.

Some of the variables from config.go:

MailHost = "smtp.gmail.com"
MailPort = "587"

If I change the port from 587 to 465 as noted here: https://support.google.com/a/answer/176600?hl=en

The I get the error: "EOF" when trying to send email from the production server.

It keeps saying I should go to "advanced settings in gmail" to whitelist IP address. But there is no advanced settings in gmail, only settings.

enter image description here