Sending email with connection using JavaMail API

For sending email with connection, JavaMail API gives some valuable classes like BodyPart, MimeBodyPart and so on.

For better comprehension of this illustration, take in the progressions of sending email utilizing JavaMail API first.

For sending the email utilizing JavaMail API, you have to stack the two jug documents:

  • mail.jar
  • activation.jar

download these jar files go to the Oracle site to download the latest version.

Sending email with connection utilizing JavaMail API

There are absolute 7 stages for sending connection with email. They are:

  1. Get the session object
  2. create message
  3. make MimeBodyPart object and set your message content
  4. make new MimeBodyPart protest and set DataHandler article to this item
  5. make Multipart protest and add MimeBodyPart articles to this item
  6. set the multiplart item to the message object
  7. send message

Sample of sending email with connection utilizing JavaMail API

import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
import javax.activation.*;  
class SendAttachment{  
 public static void main(String [] args){  
  String to="";//change as needs be 
  final String user="";//change as needs be   
  final String password="********";//change as needs be   
  //1. get the session object     
  Properties properties = System.getProperties();  
  properties.setProperty("", "");  
  properties.put("mail.smtp.auth", "true");  
  Session session = Session.getDefaultInstance(properties,  
   new javax.mail.Authenticator() {  
   protected PasswordAuthentication getPasswordAuthentication() {  
   return new PasswordAuthentication(user,password);  
  //2. compose message     
    MimeMessage message = new MimeMessage(session);  
    message.setFrom(new InternetAddress(user));  
    message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
    message.setSubject("Message Aleart");  
    //3. make MimeBodyPart object and set your message text     
    BodyPart messageBodyPart1 = new MimeBodyPart();  
    messageBodyPart1.setText("This is message body");  
    //4. create new MimeBodyPart object and set DataHandler object to this object      
    MimeBodyPart messageBodyPart2 = new MimeBodyPart();  
    String filename = "";//change as needs be   
    DataSource source = new FileDataSource(filename);  
    messageBodyPart2.setDataHandler(new DataHandler(source));  
    //5. create Multipart object and add MimeBodyPart objects to this object      
    Multipart multipart = new MimeMultipart();  
    //6. set the multiplart object to the message object  
    message.setContent(multipart );  
    //7. send message  
   System.out.println("message sent....");  
   }catch (MessagingException ex) {ex.printStackTrace();}  

As should be obvious in the above sample, absolute 7 stages are taken after to send email with connection. Presently run this project by :

Load the jar file c:\> set classpath=mail.jar;activation.jar;.;
compile the source file c:\> javac
run by c:\> java SendAttachment