Skip to content

Instantly share code, notes, and snippets.

@andelf
Created March 8, 2013 18:40
Show Gist options
  • Select an option

  • Save andelf/5118732 to your computer and use it in GitHub Desktop.

Select an option

Save andelf/5118732 to your computer and use it in GitHub Desktop.
golang net/smtp SMTP AUTH LOGIN Auth Handler
// MIT license (c) andelf 2013
import (
"net/smtp"
"errors"
)
type loginAuth struct {
username, password string
}
func LoginAuth(username, password string) smtp.Auth {
return &loginAuth{username, password}
}
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", []byte{}, nil
}
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if more {
switch string(fromServer) {
case "Username:":
return []byte(a.username), nil
case "Password:":
return []byte(a.password), nil
default:
return nil, errors.New("Unkown fromServer")
}
}
return nil, nil
}
// usage:
// auth := LoginAuth("loginname", "password")
// err := smtp.SendMail(smtpServer + ":25", auth, fromAddress, toAddresses, []byte(message))
// or
// client, err := smtp.Dial(smtpServer)
// client.Auth(LoginAuth("loginname", "password"))
@jrichardsz

Copy link
Copy Markdown

Thanks a lot!!!

@nicmwe

nicmwe commented May 15, 2024

Copy link
Copy Markdown

Thank you for this!

@AmrMady

AmrMady commented Dec 3, 2024

Copy link
Copy Markdown

Thank you

@ablogo

ablogo commented Aug 3, 2026

Copy link
Copy Markdown

Microsoft has permanently disabled Basic Authentication in Exchange Online (Microsoft 365), you will receive the next message "Authentication unsuccessful, basic authentication is disabled".

Unfortunately this code no longer work, it's mandatory implement OAuth 2.0 or another method accepted by Microsoft

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment