Monday, 15 September 2014

HOW TO SEND MAIL USING JAVAMAIL API

JAVAMAIL API

In order to send e-mail using javamail api,
you need an SMTP server, which is responsible for send your email.

In this example, I am using a gmail SMTP server configuration. In fact, we can also use any other companies SMTP server such as yahoo or a JangoSMTP server.

Through out the example, I am gonna use different javax.mail.* and javax.mail.internet.* package classes.

To use these classes in your program, download the required jars such as mail.jar, activation.jar


Let's see some of the classes used javax.mail.* package & javax.mail.internet.* package classes used in this example.


javax.mail.Session 

javax.mail.Message

javax.mail.Address

javax.mail.Authenticator

javax.mail.Transport

javax.mail.Store

javax.mail.Folder

javax.mail.internet.MimeMessage

javax.mail.internet.InternetAddress


Note:

change the code in these string with your
own details
image-file-location
sender-gmail-address-password
sender-gmail-address
receiver-gmail-address

Example:

package com.javamail;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;



public class SimpleMail {


public static void main(String[] args) {

String mail_to ="receiver-gmail-address";
String mail_from ="sender-gmail-address";


//These are details of SMTP server - gmail smtp server 
//and sender gmail authentication details

final String username="sender-gmail-address";
final String password="sender-gmail-address-password";  
String host="smtp.gmail.com"; 

Properties props=new Properties();

props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");

/*here are details of Jango SMTP server - need to register 
 in JangoSMTP. In this example I am using gmail smtp server 
 so - just commented Jango details But you can try this 
final String username="jangosmtp-username";
final String password="jangosmtp-password";  
String host="relay.jangosmtp.net";
props.put("mail.smtp.port", "25");*/

/*In order to use javamail api need to get the 
 Session object proper sender credentials -
 username & password needs to be given
otherwise, transfer of mail - fails*/
Session session=Session.getInstance(props, 
 new javax.mail.Authenticator() { 
  protected PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication(username,password);
  }
 });
 
 try {
  //create a default MimeMessage object.
 
 //Message models the message that you want to send
 //Message is an abstract class, so MimeMessage 
 //class provide the implementation
 Message message=new MimeMessage(session);
 
 message.setFrom(new InternetAddress(mail_from));
 
 //set the receviers and type of sending
 
 //you can send as cc: bcc:
 //use Message.RecipientType.CC or Message.RecipientType.BCC
 message.setRecipients(Message.RecipientType.TO, 
   InternetAddress.parse(mail_to));
 
 //set subject   
 message.setSubject("Testing mail through Java");
 
 /*set the message body- a message body can be multipart message
    this means we can send mail text and attachment also*/
 BodyPart messageBodyPart = new MimeBodyPart();
 messageBodyPart.setText("This is the message body ");
 
 
 Multipart multipart =new MimeMultipart();
 
 //adding message body part - part1
 multipart.addBodyPart(messageBodyPart);
 
 //adding attachment -part2
 messageBodyPart=new MimeBodyPart();
 
 String filename="image-file-location";
 
 DataSource source=new FileDataSource(filename);
 messageBodyPart.setDataHandler(new DataHandler(source));
 messageBodyPart.setFileName(filename);
 
 multipart.addBodyPart(messageBodyPart);
 
 //send complete message parts
 message.setContent(multipart);   

 Transport.send(message);

 //a confirmation to your mail program
 System.out.println("Sent message Successfuly");
 
} catch (MessagingException e) {
 throw new RuntimeException(e);
}
}

}



No comments:

Post a Comment