"It's me—again. And again, I have a little surprise for you."

"Today I'm going to teach you how to send email using Java."

"Let's start with the good news: Java has a native library for working with email."

"The bad news is that this library is part of Java EE, not Java SE."

"Java EE is an extended version of JavaSE, which includes classes needed for niftier applications. For example, an application for working with email."

"Got it, so what can be done then?"

"Well, I suggest you just download this library and that's it."

"Use IntelliJ IDEA wisely."

"Create a class and add the javax.mail.* and javax.mail.internet.* libraries to the import section.

"Then press Alt+Enter and let IDEA do everything for you:

"Here's what the offer to download the missing libraries looks like:"

Send email - 1

"Here's what the download window looks like:"

Send email - 2

"Or you can download it here"

"Downloaded. What's next?"

"You need to go to the project settings (Open Module Settings) to the Libraries section and add JAR files from the downloaded archive."

Send email - 3

"Done."

"Are the following lines no longer red?"

import javax.mail.internet.MimeMessage;
import javax.mail.internet.*;

"Yep."

"Great, let's carry on then."

"There are three steps to sending an email."

1) Establish a connection with the mail server that will be used to send the email

2) Create the email and, if necessary, add attachments

3) Send the email.

"Let's start from the beginning."

"To send an email in Java, you first need to establish a connection with the mail server."

"It's best if you already have an email account on the server. Concerned about spam, modern mail servers don't want to send messages from anonymous users."

"You can connect to the server with a single call to the javax.mail.Session.getDefaultInstance method:"

Create a connection with the mail server
Properties props = new Properties();

// Here we need to load data into the props object

Session session = Session.getDefaultInstance(props);

"But you need to pass the mail server settings to this method."

"For example, you can create a Mail.properties file and fill it with the desired settings, for example, something like this:"

Mail.properties
mail.transport.protocol=smtp
mail.host=smtp.gmail.com
mail.smtp.auth=true
mail.user=arnold@gmail.com
mail.password=strong

"The most important thing is to specify the protocol and host, but you may need additional settings, depending on how the mail server works."

"You can simply add this data to a Properties object right in your Java code."

"For example:"

Create a connection with the mail server"
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", “smtp.gmail.com”);
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.sendpartial", "true");

Session session = Session.getDefaultInstance(props);

"Great, we've got a session. Now let's create an email."

"This is easier to do than it seems at first glance. For example:"

Create a mail message
// Create a message
MimeMessage message = new MimeMessage(session);

// Set the message subject
message.setSubject("Test email!");

// Add the message text
message.setText("Asta la vista, baby!");

// Specify the recipient
message.addRecipient(Message.RecipientType.TO, new InternetAddress("stalone@gmail.com"));

// Specify the delivery date
message.setSentDate(new Date());

"I can specify any email address as a recipient?"

"Yep. What's more, you can also specify any email address as the sender."

"Cool! I'll take that into account."

"Now we just need to send this message."

"First, we sign in to the server, and then we send our message. Just two lines of code:"

Send a message
// Username and password for a Gmail account
String userLogin = “arnold@gmail.com”;
String userPassword = “strong”;

// Sign in on the server:
Transport transport = session.getTransport();
transport.connect("smtp.gmail.com", 465, userLogin, userPassword);

// Send a message:
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));

"How interesting! I'll have to try it."

"If you're wondering how to send a message with attachments, you can read about that here."

"If you also want to find out how to receive mail, kindly look here."

"Holy moly. What useful links!"

"Yeah, I'm going to make my own email client now. Cool!"

"Thanks, Ellie!"