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

Author

7 Comments

  1. Inquiry from Washington,DC

    Are you troubled by payroll administration?

    Is staying compliant with ever-changing payroll tax laws a burden on you?

    Is hiring a full-service payroll provider too expensive for you?

    Payon was built to eliminate your pain.
    Payon has used 30 years of payroll experience to streamline your pay runs,
    guarantee taxes will be done right, and build seamless integrations with top back office software.

    You can try it first and then decide whether to continue: https://www.biglep.com/payroll

    Regards,
    Joseph M. Morris

    *******************
    Company: Biglep
    Address: 1301 Pennsylvania Ave NW, Washington, DC 20004
    Unsubscribe: https://www.biglep.com/unsubscribe

Write A Comment