Updated April 15, 2023
Introduction to javax mail maven
Maven provides a different kind of repository to the user, in which that javax mail is one of the repositories that is provided by the maven. Basically, javax mail is used to compose the mail, we can read the mail or we can write the emails. Email means electronic messages. Normally javax mail is an API to use to perform different operations. In another word, we can say that JavaMail API provides the independent protocol and it is also a platform-independent tool used to send and receive emails from other users. The javax mail is used for the user registration time which means we can use it whenever we require it.
Overview on javax mail maven
JavaMail API gives a convention autonomous and platform-fee structure for sending and getting sends.
The javax.mail and javax.mail.activation groups contain the middle classes of JavaMail API. The JavaMail office can be applied to various events. It tends to be utilized at the hour of enlisting the client (sending warning like much obliged for your advantage to my site), failed to remember the secret word (sending a secret key to the client’s email id), sending notices for significant updates and so on So there can be different utilization of java mail programming interface.
Now let’s see the different types of Protocols we need to use in JavaMail API as follows.
SMTP
SMTP is a simple protocol. It gives a component to convey the email. We can utilize Apache James server, Postcast server, email server, and so on as SMTP servers. However, in the event that we buy the hosting space, an SMTP worker is by default given by the hosting supplier. For instance, my SMTP worker is mail.javatpoint.com. In the event that we utilize the SMTP worker given by the hosting supplier, verification is needed for sending and getting messages.
POP
POP is an abbreviation for Post Office Protocol, otherwise called POP3. It gives a component to get the email. It offers help for a single letterbox for every client. We can utilize the Apache James server, email server and so on as a POP worker. However, in the event that we buy the hosting space, a POP worker is by default given by the hosting supplier. For instance, the pop worker given by the hosting supplier to my site is mail.javatpoint.com.
IMAP
The guide is an abbreviation for Internet Message Access Protocol. IMAP is a high-level convention for getting messages. It offers help for various letterboxes for every client, notwithstanding, post boxes can be shared by different clients. It is characterized in RFC 2060.
MIME
Various Internet Mail Extension (MIME) mentions to the program what is being sent for example connection, arrangement of the messages and so on. It isn’t known as a mail move convention yet it is utilized by your mail program.
Others
On another type of protocol, we can use NNTP and S/MIME protocol as per requirement.
Now let’s see what are the different types of classes we require to implement JavaMail as listed below.
- Session class
- Message class
- MimeMessage class
- Address class
Authentication class and many more other classes we required.
How to Use javax mail maven?
For the implementation of Javax mail, we need to download from the official website of java otherwise we can add the maven dependency. Download the latest version of the JavaMail reference execution and recollect it for your undertaking gathering. The compartment record name will be javax.mail.jar.
If we use a maven project, then we need to add the following dependency in our specified project.
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>
After that, we need to create the session object file and also need to create other object files that we require to implement the Javax mail.
The rationale to make meeting contrasts dependent on the kind of SMTP worker, for instance, if an SMTP worker doesn’t need any confirmation we can make the Session object for certain basic properties while in the event that it requires TLS or SSL validation, the rationale to make will vary.
So we can make a utility class for certain utility strategies to send messages and afterward I will utilize this utility strategy with various SMTP workers.
javax mail maven examples
Now let’s see the different examples of javax mail for better understanding as follows.
First, we need to create the maven project by using different steps. After that, we need to add some dependencies into the pom.xml file as mentioned below.
<dependencies>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
Now update your maven project and create the package and give name com.mail and click on save button.
Now create a class file and give it the name smail.java and click on the main method. After that write the following code into the class file as follows.
package com.mail;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class smail {
public static void main(String[] args) {
String to_email = "[email protected]";
String from_email = "[email protected]";
String host = "smtp.gmail.com";
Properties properties = System.getProperties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.auth", "true");
Session session_obj = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "*********");
}
});
session_obj.setDebug(true);
try {
MimeMessage msg = new MimeMessage(session_obj);
msg.setFrom(new InternetAddress(from_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to_email));
msg.setSubject("Subject Line!");
msg.setText("Body of email");
System.out.println("sending emails...");
Transport.send(message_obj);
System.out.println("email sent successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
Explanation
First, we need to import the different required packages and classes. After that, we need to specify the sender and receiver mail id as shown in the above code. In the above code, we use different methods and classes to implement the javax mail-in maven by using SMTP host as shown in the above code. The final output of code we illustrate by using the following screenshot as follows.
Conclusion
We hope from this article you learn more about the javax mail maven. From the above article, we have taken in the essential idea of the javax mail and we also see the representation and example of javax mail. From this article, we learned how and when we use the javax mail.
Recommended Articles
This is a guide to javax mail maven. Here we discuss the essential idea of the javax mail and we also see the representation and example. You may also have a look at the following articles to learn more –