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="mithileshgniit@gmail.com";//change as needs be 
  final String user="mithilesh@zeteticsoft.com";//change as needs be   
  final String password="********";//change as needs be   
   
  //1. get the session object     
  Properties properties = System.getProperties();  
  properties.setProperty("mail.smtp.host", "mail.javatportal.com");  
  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     
  try{  
    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 = "SendAttachment.java";//change as needs be   
    DataSource source = new FileDataSource(filename);  
    messageBodyPart2.setDataHandler(new DataHandler(source));  
    messageBodyPart2.setFileName(filename);  
     
     
    //5. create Multipart object and add MimeBodyPart objects to this object      
    Multipart multipart = new MimeMultipart();  
    multipart.addBodyPart(messageBodyPart1);  
    multipart.addBodyPart(messageBodyPart2);  
  
    //6. set the multiplart object to the message object  
    message.setContent(multipart );  
     
    //7. send message  
    Transport.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 SendAttachment.java
run by c:\> java SendAttachment