Newer Emacsen have the ability to use authentication when SendingMail built-in.


Here’s how you would configure it:

    (setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
    (setq message-send-mail-function 'smtpmail-send-it) ; if you use message/Gnus
    (setq smtpmail-default-smtp-server "YOUR SMTP HOST") ; set before loading library
    (setq smtpmail-local-domain "YOUR DOMAIN NAME")
    (setq smtpmail-sendto-domain "YOUR DOMAIN NAME")
    (setq smtpmail-debug-info t) ; only to debug problems
    (setq smtpmail-auth-credentials  ; or use ~/.authinfo
          '(("YOUR SMTP HOST" 25 "username" "password")))
    (setq smtpmail-starttls-credentials
          '(("YOUR SMTP HOST" 25 "~/.my_smtp_tls.key" "~/.my_smtp_tls.cert")))

Where 25 is the value of ‘smtpmail-smtp-service’; it can be an integer or a string, just as long as they match (eq).

The key/certificate strings in smtpmail-starttls-credentials are optional, only use them if you intend to do certificate-based authentication (i.e., if you won’t supply a username/password).

Here’s a typical example:

    (require 'smtpmail)
    (setq smtpmail-smtp-server ""
          send-mail-function 'smtpmail-send-it
          smtpmail-auth-credentials "~/.netrc")

Extra configuration may be necessary

Note that if you don’t have a TLS handler (starttls or gnutls) installed, smtpmail will silently use a plain channel, which often means you won’t be able to authenticate (depending on your SMTP server’s configuration). I’ll see if I can put a fix in Emacs CVS but this will affect older versions for a while. – tzz 2008-10-29

For some reason I found I needed to set the GNU TLS parameters explicitly, because the connection didn’t work without them. It seems that gnustls-cli should be used as a default if nothing is set, but that didn’t work for me. Adding these lines in my .gnus.el file before the above parameters as a workaround made it work. – AlexLan 2006-12-02

    (setq starttls-use-gnutls t)
    (setq starttls-gnutls-program "gnutls-cli")
    (setq starttls-extra-arguments nil)

Using a File

The variable ‘smtpmail-auth-credentials’ is set to a filename by default: “~/.authinfo”. See GnusAuthinfo.

Other Mail Transfer Agent

If you use sendmail or exim (a mail transfer agent outside of Emacs), then you have to change your sendmail or exim config. There is nothing you can do on the Lisp layer. How do you know this applies? If message-send-mail-function’s value is sendmail-send-it, then you are using a sendmail-compatible mail transfer agent outside of Emacs. Check man sendmail(8).

On a Debian system, read /usr/share/doc/exim4-base/README.SMTP-AUTH, for example.