Sending Email with Java Mail. Email stands for Electronic Mail that…
By: Haider Imtiaz
Tags: Software Development, Coding, Java, Programming, Email

Email stands for Electronic Mail that makes our life easier and reliable we are sending our text in a minute to anyone in the world in just a minute. Email providers like Gmail, Outlook, Yahoo providing email services free for personal users that a really great thing for us because we are using email for making our daily life work easier, example universities using email to send information to their students like class scheduling, Job offers info, sending data from one department to another, Students are using email, Companies are using email for sending their data and etc we are using the email in our daily life but what about if we need a robot that sends an email at a specific time when we are not available, let take an example why we need Email bot, Supposes you made a scraping bot and you want the feature that when the scraping is done, it should email you the output data, in that case, we need the Email Bot.

In this article we will walk through how we develop an Email bot with Java language using the Java Mail module, Well without wasting any time let straight jump into it.

Installation Java:

We need Java JDK which stands for a java development kit and will be needed for writing the java programs. You can download the latest version of JDK from the Oracle website here Site_URL . Follow the following steps to install them Java JDK properly in your operating system.

Step 1: Download JDK from the site

Step 2: Install the JDK

Step 3: Install the Java JDK bin the Environmental Variables

Step 4: Verify the JDK by typing Java in the Command prompt

Step 5: Write your first Hello world test program

Step 6: Compile and Run your java program

For Mac os and other Operating system Guide please visit that Site

JDK or JRE?

Many new java programmers also confused about what to choose between for the java compiler JDK or JRE. Well JRE Stands for Java Runtime and needed for running the Java program while JDK is a development Kit and includes the JRE in it such as compiler and debugger so you don't need to download the JRE. With JDK you will get a JRE install of the same version.

Download Module:

We need the Java Mail module for coding our Java Email bot, download the java mail latest version from the following site.

URL: https://www.oracle.com/java/technologies/javamail-releases.html

Coding Part:

First thing first we will load the modules of java mail and java built-in modules. From line 1 to 2 we loaded the built-in modules of java and from line 3 to 5 we loaded the Activision packages and at last, we loaded the Java mail modules. We will work through each functionality of function that we loaded from the Java modules in the next code part.

We create a public class with a name the same as the name of the java file in which we are currently working. Well in the first part of the code we loaded the required modules and in the next part we created a class naming EmailUtill this will be our main class and within the class, I made another static class name Email in which I pass the 4 parameters one session type and other 3 was String type that was a body, subject and Reciever Email. From line 21 to 25 we set up the new session using MimeMessage msg = new MimeMessage(session); and then using msg. headers function we are populating the headers with the information of what type of email we are sending I set it to test/html format and encoding will be utf-8 that including almost languages charset.

In line 27 I had used the msg. setFrom() function in which we create a new InternetAddress which holding our login details Email and password and on the next line msg.setReplyTo() function holding the email of the receiver. From line 31 to 35 we are setting up the email body, subject, and date. In the next line, I use the msg.setRecipients() function in which add the information we store on the internetAddress, and at last, on next Transport.send(msg) we delivered our email to the receiver email. if you had noticed we used the try and except method that was useful if we got any bug in the time execution we will get a msg of error occurred using the printStackTrace() function.

Send Email in Java with Attachment:

We had seen how we can send a simple email with java, now we will learn how we can send an email with an attachment it can be a file or an image.

If you saw the Code We are setting up the body, subject and login details, etc like we one in the previous code and from line 14 to 17 new message body part using the mimeBodyPart() function and passing the body part as a parameter and next step we MimeMultipart() in which we are passing the mimeBodyPart() as a parameter this process is done so we send our message body part with attachment, from line 20 to 27 we are setting up the filename that we need to send, storing the filename in string datatype variable, I store a text file but you can attach any file format you want to send to receiver emails.

The next step is passing the filename in the function setFileName() and then the email body that we made with the MimeMultipart() function and pass this to addBodyPart() function and on the next line msg.setContent() function we are passing the multipart datatype variable which includes all the parts of the message including body, subject and content/file and in last we send the email with Transport.send().

Send Email with Image Attachment:

In this part we will walk through that how we can make a java mail program that can send mail with an image attachment, In simple words, you can view the image in the Email body, Take a look at the Code below.

If you saw the Code we already cover half of the code in the previous section, From line 23 to 38 we will set up the image has an attachment in an email, I again created a new MimeBodyPart() function and pass the body in it and next, I declared a string datatype variable and store the name of the image with its extension and on next lines, we set the headers to the image by using setHeader() so that email header when read by backend so they know that the email has an image viewer attachment.

On the next line, I created new MimeBodyPart() and using its method I pass setContent() I pass the name of the image that will be shown as a title if you notice I used <h1></h1> tags that because email always opened has an HTML view so <h1> the tag will make a heading size text, next final lines we again using Transport.send() to deliver our email.

So in the end we learn how we can send Emal using Java mail. I hope you learn something from this article and feel free to share your opinion.