Sending Mail Without Using JavaMail


You want to send mail, but don’t want to require javax.mail.


This is a Really Bad Idea. You can implement the SMTP protocol yourself, but you shouldn’t.


Implementing an Internet protocol from the ground up is not for the faint of heart. To get it right, you need to read and study the requisite Internet RFC[43] pseudo-standards. I make no pretense that this mail sender fully conforms to the relevant RFCs; in fact, it almost certainly does not. The toy implementation here uses a simpler send-expect sequencing to keep in sync with the SMTP server at the other end. Indeed, this program has little to recommend it for serious use; I can only say that I had it around, and it’s a good illustration of how simple a mail sender can be. Reading it may help you to appreciate the JavaMail API, which handles not just SMTP but also POP, IMAP, and many other protocols. Do not use this code in production; use the JavaMail API instead!

The basic idea of SMTP is that you send requests like MAIL, FROM, RCPT, and DATA in ASCII over an Internet socket (see Section 15.2). Even if your mail contains 8- or 16-bit characters, the control information must contain only “pure ASCII” characters. This suggests either using the byte-based stream classes from (see Section 9.2) or using Readers/Writers with ASCII encoding. Further, if the data contains 8- or 16-bit characters, it should be encoded using MIME (see Section 19.5). This trivial example ...

Get Java Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.