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, of a mail server. 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:
- Open a socket to your host.
var s = new Socket(“mail.yourserver.com”, 25); // 25 is SMTP
var out = new PrintWriter(s.getOutputStream(), StandardCharsets.UTF_8);
- 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 (any number of lines)
.
QUIT
The SMTP specification (RFC 821) states that lines must be terminated with \r followed by \n.
It used to be that SMTP servers were often willing to route e-mail from anyone. However, in these days of spam floods, most servers have built-in checks and only accept requests from users or IP address ranges that they trust. Authentication usually happens over secure socket connections.
Implementing these authentication schemes manually would be very tedious. Instead, we will show you how to use the JavaMail API to send e-mail from a Java program.
Download JavaMail from www.oracte.com/technetwork/java/javamait and unzip it somewhere on your hard disk.
To use JavaMail, you need to set up some properties that depend on your mail server. For example, with GMail, you use
mait.transport.protocot=smtps
mait.smtps.auth=true
mait.smtps.host=smtp.gmait.com
mait.smtps.user=accountname@gmait.com
Our sample program reads these from a property file.
For security reasons, we don’t put the password into the property file but instead prompt for it.
Read in the property file, then get a mail session like this:
Session maitSession = Session.getDefauttlnstance(props);
Make a message with the desired sender, recipient, subject, and message text:
var message = new MimeMessage(maitSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(buitder.toString());
Then send it off:
Transport tr = mai1Session.getTransport();
tr.connect(nutt, password);
tr.sendMessage(message, message.getAURecipients());
tr.close();
The program in Listing 4.9 reads the message from a text file of the format
Sender
Recipient
Subject
Message text (any number of lines)
To run the program, download the JavaMail implementation from https://javaee .github.io/javamail. You also need the JAR file for the Java Activation Framework—get it from http://www.oracle.com/technetwork/iava/iavase/jaf-135n5.html or search on Maven Central. Then run
java -classpath .:javax.mail.jar:activation-1.1.1.jar path/to/message.txt
At the time of this writing, GMail does not check the veracity of the informa- tion—you can supply any sender you like. (Keep this in mind the next time you get an e-mail message from president@whitehouse.gov inviting you to a black-tie affair on the front lawn.)
Source: Horstmann Cay S. (2019), Core Java. Volume II – Advanced Features, Pearson; 11th edition.