2
votes

So I'm trying to utilize the smtp, mime, and tls packages for Tcl to be able to allow my program to send e-mails via external mail servers such as the gmail server (smtp.gmail.com) and yahoo server (smtp.mail.yahoo.com). I have an issue arising:

Sending email via the gmail server gives me the following error:

handshake failed: resource temporarily unavailable
   while executing 
"::tls::handshake $state(sd)"

I am using the smtp information found here: http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm

and my code looks like this:

tls::init -tls1 1;
set token [mime::initialize -canonical text/plain -string $body];

mime::setheader $token Subject "Test Email";
smtp::sendmessage $token -recipients "<my email address here>" -servers "smtp.gmail.com" -ports 587 -username "<my other email address here>" -password "<my password here>" -usetls true -debug 1;
mime::finalize $token;

Google automatically sent my gmail account an email saying:

We recently blocked a sign-in attempt to your Google Account <my email here>

And it gave me the option to change the security settings for "less secure apps" to allow my program to utilize the mail server. So I did, then my code worked just fine. What I don't understand is why gmail is blocking my attempts to send an e-mail; why it considers my attempt "less secure" thereby forcing me to lower the security settings on the e-mail account prior to sending emails from it. Perhaps my understanding is off, but I believe I'm using tls? And isn't tls more secure than ssl? Why does gmail have tls port if I can't connect to it anyway?

EDIT: Also I tried changing tls::init -tls1 1; to tls::init -ssl3 1; and using port 465 instead of 587, still to no avail.

When I try accessing the yahoo mail server (information retrieved from: http://www.serversmtp.com/en/smtp-yahoo), I get the following error:

premature end-of-file from server
  while executing
"smtp::sendmessage $token -recipients "<my email here>" -servers "smtp.mail.yahoo.com" -ports 465 -username "my other email here" -password "<my password>" -usetls true -debug 1;..."

UPDATE I learned that for yahoo, the account you want to use has to be a Yahoo Mail Plus account to let you do smtp mail sending.

1

1 Answers

4
votes

Have you considered the possibility that this might not be a technical limitation so much as a policy one? Mail is very heavily locked down these days due to decades of abuse by spammers. There's probably some additional mail header that you'd have to set in order to send a cryptographic token based on some API key, which would allow direct access without that option being enabled. Just a little research leads me to this page, which says:

IMAP and SMTP use the standard Simple Authentication and Security Layer (SASL), via the native IMAP AUTHENTICATE and SMTP AUTH commands, to authenticate users. The SASL XOAUTH2 mechanism enables clients to provide OAuth 2.0 credentials for authentication. The SASL XOAUTH2 protocol documentation describes the SASL XOAUTH2 mechanism in great detail, and libraries and samples which have implemented the protocol are available.

Incoming connections to the IMAP server at imap.gmail.com:993 require SSL. The outgoing SMTP server, smtp.gmail.com, requires TLS. Use port 465, or port 587 if your client begins with plain text before issuing the STARTTLS command.

Now, though there is a SASL implementation in tcllib, and the smtp package uses it by default, there isn't an implementation of the XOAUTH2 mechanism in tcllib (which you can see by inspection of the code — look for ::SASL::register), making things fall back on older mechanisms that Google are less happy with. Fixing that would be outside the scope of this answer (but in general that's where “get coding” is suggested, or at least “file a feature request”).


I've not yet been able to nail down exactly what's required in the Yahoo Mail documentation; it seems harder for me to search…