At First we will create a Java Project using Eclipse IDE We give the project name as Sendmail. Click and Next and Click on Finish Now we create a new folder in name of lib. This folder holds the required external libraries for our java project. Now we will see the required components needed to send mail we need something called as SMTP (Simple Mail Transfer Protocol). Either it could be installed in you local machine or if we are under organization the SMTP host name and port Now the time to get to know the three parts of JAVAMAIL API need to send an e-mail 1. Getting the SMTP session object, 2. Compose the message, 3. send the message First is session initialization for SMTP session In this part, we initialize the SMTP session using getDefaultInstance() method second part is Compose the message In this part, we use something called MimeMessage class to compose the message In the MimeMessage class, we add the recipient, from mail, subject, message body and etc. third part is Send the message the method that is used send message is send() The below example shows how simple that is to send an mail using JAVAMAILAPI The required external libraries that we need to include are mail.jar and activation.jar. please download the same now we have copy those two downloaded libraries into our project lib folder Let’s do the code in our project. now we create a new Java class with the name of Sendemail Let’s define the required variable need ed for like from, to, smtp host and port, etc.. Now create an instance for Properties. this Properties instance holds the properties needed for initiating the session for SMTP protocal now we will the require properties like smtp server host, port and SSLSocketFactory and etc.. Fisrt part comes here. Open the smtp session Now let’s open the smtp session with password authentication type using the getDefaultInstance() method Second part – Composing the message comes here Initiate the MimeMessage class and add the properties like recepient, from, subject and body of mail using respective method Adding recepient using addRecepient() method Adding subject and body of mail using setSubject() and setBody() methods Now the 3rd part comes, Transport the mail using send method Update the required variables like host,port, from and to accordingly We are at the end. Let’s run the code Yeah…. we made it the mail has been sent Let’s check the mail.. Yeah I have received the mail that has send using JAVAMAILAPI If you are using gmail, kindly enable the less secure apps since this is unknown source for smtp The source code link is provided in the description Thank you for watching the video. Kindly leave your comment about what in java your looking for to find answers.

4 thoughts on “Java Code to SEND EMAIL – JAVAMAIL API

  1. Good Attempt 👍🏻👌🏼 but suggest to show this steps from your own system or method rather than showing from a website directly…..! Hope you won’t mistake me….

Leave a Reply

Your email address will not be published. Required fields are marked *