Thursday, 18 January 2018

How to send email using JavaMail API | JAVA

First of all you have to setup environment to send a mail using JavaMail API. You have to download some jar files and them to the CLASSPATH. steps are given below.

1. Download JavaMail API  and install it.
2. Download JavaBeans Activation Framework (JAF) and install it.
3. Add the mail.jar and activation.jar in the CLASSPATH.
4. You have to install an SMTP server such as jangoSMTP or you can use your gmail account. To use your gmail account. You have to connect to smtp.gmail.com at port number 587 if you are using TLS certificate. If you are using SSL then you have to connect to port number 465.

In my example, I am using my gmail account to send the mail to a friend. This is an example of sending simple text email.

Steps to send emails using JavaMail API:

1. Setup 'From' and 'To' address.
2. Get user login credentials.
3. Set SMTP host (for gmail, it is smtp.gmail.com)
4. Setup Properties value.
5. Create Message Object.
6. Form the message details.
7. Send by using Transport.send() method.

Program:

import java.util.Properties;
import java.util.Scanner;
import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.Session;




public class EmailSender {

   public static void main(String args[]) {
       Scanner in=new Scanner(System.in);
       System.out.println("\nEnter the username to login: ");
       String user_name=in.next();
       System.out.println("Enter the userpassword to login: ");
       String user_password=in.next();
       
       System.out.println("Enter the host address: ");
       String host_name=in.next();
       System.out.println("Enter the receiver email address: ");
       String receiver_mail=in.next();
       System.out.println("Enter the subject of the email: ");
       String subject=in.nextLine();
       in.nextLine();
       System.out.println("Enter the message to be send: ");
       String message=in.nextLine();
       
       String sender_mail=user_name;
       
       Properties propvls=new Properties();
       propvls.put("mail.smtp.auth","true");
       propvls.put("mail.smtp.starttls.enable","true");
       propvls.put("mail.smtp.host", host_name);
       propvls.put("mail.smtp.port","587");
       
       Session sessionobj=Session.getInstance(propvls, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication(){
         
         return new PasswordAuthentication(user_name,user_password);
            
        }
       });
       
       try {
           
        Message messageobj=new MimeMessage(sessionobj);
        messageobj.setFrom(new InternetAddress(sender_mail));
        messageobj.addRecipients(Message.RecipientType.TO,InternetAddress.parse(receiver_mail));
        messageobj.setSubject(subject);
        messageobj.setText(message);
        Transport.send(messageobj);
        System.out.println("Your message is sent successfully");
       }
       catch(MessagingException exp) {
          
        System.out.println(exp.getMessage());
       }
   }
}

OUTPUT:




EmoticonEmoticon