KDJ Guru

Java Email Sending Code

Read Time3 Minutes, 15 Seconds

Hello and welcome to KDJ GURU. Here you can learn how to send an email using java. This is actually quiet simple. Hope you’d get something from this post. Enjoy!

First you need to download javax.mail.jar and activation.jar files.

activation.jar file -> download from : http://www.java2s.com/Code/Jar/a/Downloadactivationjar.htm

javax.mail.jar file -> download from :

http://www.java2s.com/Code/Jar/j/Downloadjavaxmailapi147jar.htm

Then you need to create a java class file and name it sendMail.

Note: sendMail is a java class which we create.

Here is the code that should be written in the java class we created.

package GUI;

import java.util.Properties;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

/**

 *

 * @author asus

 */

public class sendMail {

    public void sendMail(String address, String subject, String message) throws Exception{

        String from = “Your Email Address”;

        String pass = “Password For Your Email Address”;

        String[] to = {address};

        String host = “smtp.gmail.com”;

        Properties prop = System.getProperties();

        prop.put(“mail.smtp.starttls.enable”, “true”);

        prop.put(“mail.smtp.host”, host);

        prop.put(“mail.smtp.user”, from);

        prop.put(“mail.smtp.password”, pass);

        prop.put(“mail.smtp.port”, “587”);

        prop.put(“mail.smtp.auth”, “true”);

        Session session = Session.getDefaultInstance(prop);

        MimeMessage msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(from));

        InternetAddress[] toaddress = new InternetAddress[to.length];

        for(int i = 0; i < to.length; i++){

            toaddress[i] = new InternetAddress(to[i]);

        }

        for(int i = 0; i < toaddress.length; i++){

            msg.setRecipients(Message.RecipientType.TO, toaddress);

        }

        msg.setSubject(subject);

        msg.setContent(message, “text/html”);

        Transport transport = session.getTransport(“smtp”);

        transport.connect(host, from, pass);

        transport.sendMessage(msg, msg.getAllRecipients());

        transport.close();

    }

}

———————————————————————————————————————————————————————————————————————————————————————————————

public class sendMail {

    public void sendMail(String address, String subject, String message) throws Exception{

        String from = “Your Email Address”;

        String pass = “Password For Your Email Address”;

In the highlighted area, type your email address.

public class sendMail {

    public void sendMail(String address, String subject, String message) throws Exception{

        String from = “Your Email Address”;

        String pass = “Password For Your Email Address”;

In the highlighted area, type the password for your email address. Then create an interface like the following interface.

The Reciever(s) field should be a jTextArea and change the variable name as eaddres.

Subject field should be a jTextField and change the variable name as subject.

Message field should be a jTextArea and change the variable name as ebody.

Then double click on the Send button and type the following code.

try {

                if (eaddress.getText() != null && !eaddress.getText().equals(“”)) {

                    sendMail sm = new sendMail();

                    sm.sendMail(a, esubject.getText(), ebody.getText());

                } else {

                    JOptionPane.showMessageDialog(null, “Error”);

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

But now you can send only one email each time. And we need to send an email to many recipients.

For that, try this code.

String str = eaddress.getText();

        String parts[] = str.split(“\n”);

        for (int i = 0; i < parts.length; i++) {

            String a = parts[i].toString();

            try {

                if (eaddress.getText() != null && !eaddress.getText().equals(“”)) {

                    sendMail sm = new sendMail();

                    sm.sendMail(a, esubject.getText(), ebody.getText());

                } else {

                    JOptionPane.showMessageDialog(null, “Error”);

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

When you run the program, type one email address in the Address field and press enter and enter another and do this as many as you wish to.

But there’s a catch. You can only use an email address which has low security access. To change your email account’s security access to low, follow these simple steps.

First go to your email account.

Click on your profile picture à Google Account à Security à Less Secure App Access

Then you’re good to go.

Following is the zip file for the code. Enjoy!

https://drive.google.com/file/d/1K_-M1ZFCnyCEYF87GdcswUsjr6ys2K8K/view?usp=sharing

1 0

About Post Author

Kavidu Jayakody

Kavidu Dilshan Jayakody is the founder and the author of KDJ Blog. He is a skilled web developer and graphic designer. In addition, he has earned his degree BSc (Hons) Computing, UK. He is working as a tutor of an institute and as a part-time blogger, he will help you all to make your problems solved.
100 %
Happy
0 %
Sad
0 %
Excited
0 %
Angry
0 %
Surprise

Leave a Comment

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

Scroll to Top