3.5. Sending E-Mail

In the past, it was simple to write a program that sends e-mail by making a socket connection to port 25, the SMTP port. The Simple Mail Transport Protocol (SMTP) describes the format for e-mail messages. Once you are connected to the server, send a mail header (in the SMTP format, which is easy to generate), followed by the mail message.

Here are the details:

1. Open a socket to your host.

Socket s = new Socket("mail.yourserver.com", 25); // 25 is SMTP PrintWriter out = new PrintWriter(s.getOutputStream());

2. Send the following information to the print stream:

HELO sending host MAIL FROM: sender e-mail address RCPT TO: recipient e-mail address DATA Subject: subject (blank line) mail message ...

Get Core Java® Volume II—Advanced Features, Ninth Edition now with O’Reilly online learning.

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