How to send email through java


This article is to let you people know that how to send an email through java.
It is the class where all the attributes are defined and the function is called from another class named emailDemo.


import java.util;
import javax.mail;
import javax.mail.internet;
import java.util.Properties;
import javax.mail.Message RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

 * @author umAr
public class emailSender {
    public static boolean sendMail(string from, string password, 
    string message, string to[]
            string host="";
            Properties props = System.getProperties();
            props.put("", host);
            props.put("mail.smtp.user", from);
            props.put("mail.smtp.password", password);
            props.put("mail.smtp.port", 587);
            props.put("mail.smtp.auth", "true");
            Session session = Session.getDefaultInstance(props,null);
            MimeMessage mimeMessage = new MimeMessage(session);
            MimeMessage.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++)
            MimeMessage.setSubject("mail from java mail client");
            Transprt transport = session.getTransprt("smtp");
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            return true;
            catch(MessagingException me)
            return false;


The main class where these functions are called is :-

public class EmailDemo {

     * @param args the command line arguments
    public static void main(String[] args) 
        String[] to = {""};
            *****, This message is sent from java mailAPI, 
            to())System.out.println("Email sent successfully");
         else System.out.println("There is an error occur");

All the work is done by using NetBeans software and in refrence
 there is also a youtube link

Let's Discuss