xhit / go-simple-mail

Golang package for send email. Support keep alive connection, TLS and SSL. Easy for bulk SMTP.
MIT License
650 stars 102 forks source link

StarTTLS problems #68

Closed icyrogue closed 2 years ago

icyrogue commented 2 years ago

Hello there. Geting

Must issue a STARTTLS command first.

Error when trying to use smtp.gmail.com as host. Is it possible to use use gmail host? I was trying other go-mail package with Google App password and everything worked Here is the code:

    server := mail.NewSMTPClient()
    server.Port = 587
    server.Username = "<my gmail>"
    server.Password = "<App password for this gmail>"
    server.Host = "smtp.gmail.com"

    client, err := server.Connect()
    if err != nil {
        return err
    }

    server.Encryption = mail.EncryptionSTARTTLS
    server.TLSConfig = &tls.Config{InsecureSkipVerify: true}
    //Tried without those options and got the same error 

    email := mail.NewMSG()
    email.SetFrom("<my gmail>")
    email.AddTo("<my other gmail>")
    email.SetBody(mail.TextPlain, "this is a test")
    if email.Error != nil{
        log.Fatal(email.Error)
    }

    err = email.Send(client)
    if err != nil {
        log.Println(err)
    } else {
        log.Println("Email Sent")
    }
    return nil
}

Am I missing something? Thanks in advance

xhit commented 2 years ago

Yes, you are connecting before set encryption

icyrogue commented 2 years ago

Yes, you are connecting before set encryption

Oh, yes, that's it.... Thanks a lot, such a stupid mistake to make, guess I have to take a break

Love your work, thank you again

xhit commented 2 years ago

Resting is part of the job my friend, have a good day :+1: