![]() |
VOOZH | about |
The JavaMail API defines classes that represent the components of a mail system. JavaMail does not implement an email server, instead, it allows you to access an email server using a Java API. In order to test the code presented, you must have access to an email server. While the JavaMail API specification does not mandate support for specific protocols, JavaMail typically includes support for POP3, IMAP, and SMTP.
Prerequisite:
There are the following three steps to send email using JavaMail. They are as follows:
Get the session object - javax.mail.Session class provides object of session, Session.getDefaultInstance() method and Session.getInstance() method.
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// mail username and password
properties.setProperty("mail.user", "user");
properties.setProperty("mail.password", "password$$");
Compose the message- javax.mail.Transport class provides method to send the message.
// javax.mail.internet.MimeMessage class is
// mostly used for abstraction.
MimeMessage message = new MimeMessage(session);
// header field of the header.
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("subject");
message.setText("Hello, aas is sending email ");
Send the message
Transport.send(message);
Following is the Send Mail in Java using SMTP without authentication full implementation in java-
Output
Yo it has been sent...
The program is simple to understand and works well, but in real life, most of the SMTP servers use some sort of authentication such as TLS or SSL authentication. So, we will now see how to create a Session object for these authentication protocols. For TLS & SSL you can know the port in which the mail server running those services. We will provide you code taking Gmail into consideration. Following is the Send Mail in Java using SMTP with TLS authentication full implementation in java-
Following is the Send Mail in Java using SMTP with SSL authentication full implementation in java-
Multiple clients we can have the following changes in the above code