Last active
          May 5, 2017 10:26 
        
      - 
      
 - 
        
Save shamimevatix/600144730b4afa11350ade01acd27d56 to your computer and use it in GitHub Desktop.  
    Golang gmail smtp authentication
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package main | |
| import ( | |
| "log" | |
| "net/smtp" | |
| "strconv" | |
| "crypto/tls" | |
| ) | |
| func ValidateSMTPServer(port int, serverHost, securityType, username, password string) error { | |
| // Connect to server | |
| serverAddress := serverHost + ":" + strconv.Itoa(port) | |
| log.Println(serverAddress) | |
| smtpClient, err := smtp.Dial(serverAddress) | |
| if err != nil { | |
| return err | |
| } | |
| defer smtpClient.Close() | |
| log.Println("Dialed") | |
| if ok, _ := smtpClient.Extension("STARTTLS"); ok { | |
| config := &tls.Config{ServerName: serverHost} | |
| if err = smtpClient.StartTLS(config); err != nil { | |
| return err | |
| } | |
| } | |
| log.Println("STARTTL Done") | |
| auth := smtp.PlainAuth("", username, password, serverHost) | |
| if err = smtpClient.Auth(auth); err != nil { | |
| return err | |
| } | |
| return nil | |
| } | |
| func main() { | |
| log.Println("starting smtp authentication") | |
| err := ValidateSMTPServer(587, "smtp.gmail.com", "SSL", "[email protected]", "password"); | |
| log.Println(err) | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment