Java Mail API

The JavaMail API provides a platform-independent and protocol-independent framework for sending and receiving mails. The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform. By using JavaMail API we can send, receive, delete, manage the emails.

Following are some of the protocols supported in JavaMail API:

SMTP« Acronym for Simple Mail Transfer Protocol.:-- It provides a mechanism to deliver/send electronic mail(Email). An authentication is required in case of sending and receiving the emails through SMTP server which was provided by host server like host name, port number, and security settings. We can use Apache James server, Postcast server, cmail server etc. as an SMTP server. if we purchase the host space, an SMTP server is bydefault provided by the host provider.

POP « Acronym for Post Office Protocol:-- also known as POP3 - Post Office Protocol version 3. It provides a mechanism to receive/get their emails. It defines support for a single mailbox for each user. RFC 1939 defines this protocol.

IMAP « Acronym for Internet Message Access Protocol:-- It is an advanced protocol for receiving messages. It provides support for multiple mailbox for each user and also provides the facility to share a single mailbox among multiple users. It is defined in RFC 2060.

MIME « Acronym for Multipurpose Internet Mail Extensions.:-- It is not a mail transfer protocol. Instead, it defines the content of what is transferred: the format of the messages, attachments, and so on. There are many different documents that take effect here: RFC 822, RFC 2045, RFC 2046, and RFC 2047.

NNTP « There are many protocols that are provided by third-party providers. Some of them are Network News Transfer Protocol (NNTP), Secure Multipurpose Internet Mail Extensions (S/MIME) etc.

JavaMail Layered Architecture components:

• The Abstract Layer declares classes, interfaces and abstract methods intended to support mail handling functions that all mail systems support. API elements comprising the Abstract Layer are intended to be sub-classed and extended as necessary in order to support standard data types, and to interface with message access and message transport protocols as necessary.
• The internet implementation layer implements part of the abstract layer using internet standards - RFC822 and MIME.
• Java-mail uses the JavaBeans Activation Framework (JAF) in order to encapsulate message data, and to handle commands intended to interact with that data. Interaction with message data should take place via JAF-aware JavaBeans, which are not provided by the JavaMail API.

The JavaMail Framework

The JavaMail API is intended to perform the following functions, which comprise the standard mail handling process for a typical client application:

• Create a mail message consisting of a collection of header attributes and a block of data of some known data type as specified in the Content-Type header field. JavaMail uses the Part interface and the Message class to define a mail message. It uses the JAF-defined DataHandler object to contain data placed in the message.
• Create a Session object, which authenticates the user, and controls access to the message store and transport.
• Send the message to its recipient list.
• Retrieve a message from a message store.
• Execute a high-level command on a retrieved message. High-level commands like view and print are intended to be implemented via JAF-Aware JavaBeans.

The Structure of a Message

The Message class models an electronic mail message. It is an abstract class that implements the Part interface.
The Message class defines a set of attributes and content for an electronic mail message. The attributes, which are name-value pairs, specify addressing information and define the structure of the message's content . Messages can contain a single content object or, indirectly, multiple content objects. In either case, the content is held by a DataHandler object.
Simple Messages « A simple message has a single content object, which is wrapped by a DataHandler object.
Multipart Messages « In this case the DataHandler object contains a Multipart object, instead of merely a a single block of content data. A Multipart object is a container of BodyPart objects. The structure of a BodyPart object is similar to the structure of a Message object, because they both implement the Part interface.
Each BodyPart object contains attributes and content, but the attributes of a Bodypart object are limited to those defined by the Part interface. An important attribute is the content-type of this part of the message content. The content of a BodyPart object is a DataHandler that contains either data or another Multipart object.

Prerequisites to Send EMail:

To send an e-mail using your Java Application is simple enough but to start with you should have JavaMail API(mail-1.4.7.jar) and Java Activation Framework (JAF - activation-1.1.1.jar) installed on your machine.
SMPT server « Eighter you Install and use any SMTP server such as Postfix server (for Ubuntu), Apache James server etc. (or) Use SMPT server provided by Gmail, which offers to use their public SMTP server for free of charge.


public enum MailDomain {
	USER_NAME("*****@gmail.com"), PASSWORD("*****");
	
	private String value;
	MailDomain(final String value) {	this.value = value;	}
	public String getValue() {	return value;	}
}

public class MailSenderSMTPGmail_Client {
	
	private static String TO_ADDRESS = "*****@gmail.com";
	
	public static void main(String[] args) {
		
		String subject = "Testing Subject";
		String electronic_Message = "MIME message Body - Test Mail using JavaMailAPI";
		String contentType = "Inline File"; // plain | HTML | Attached File | Inline File
		
		if( sendMail(subject, contentType, electronic_Message, TO_ADDRESS) ) {
			System.out.println("Sent message successfully.");
		} else {
			System.out.println("message failed.");
		}
	}
	
	public static boolean sendMail(String subject, String contentType, String body, String recipients) {
		
		String mailhost = "smtp.gmail.com", mailport = "465";
		// mail.domain.com [587]
		Session mailSession = getSessionObject(mailhost, mailport, true);
		try {
			// Create a default MimeMessage object.
			Message message = new MimeMessage( mailSession );
			message.setFrom( new InternetAddress( MailDomain.USER_NAME.getValue() ) );
			
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(TO_ADDRESS));
			
			message.setSubject( subject );
			
			// Multipart Message - [Attached File, In-line Images]
			if( contentType.equalsIgnoreCase("Attached File") ) {
				Multipart multipart = new MimeMultipart();
				
				// Create a mime body and Set Text
				BodyPart messageBodyPart = new MimeBodyPart();
				messageBodyPart.setText( body );
				multipart.addBodyPart( messageBodyPart );

				String filename = "D:\\Test.js";
				// Part two is attachment
				BodyPart  fileBodyPart = new MimeBodyPart();
				DataSource source = new javax.activation.FileDataSource( filename );
				fileBodyPart.setDataHandler( new DataHandler( source ) );
				fileBodyPart.setFileName( filename );
				multipart.addBodyPart( fileBodyPart );

				//http://www.oracle.com/technetwork/java/javamail/faq/index.html#overridefiletype
				// Send the complete message parts
				message.setContent( multipart, Part.ATTACHMENT ); // "multipart/*"
			} else if( contentType.equalsIgnoreCase("Inline File") ) {
				Multipart multipart = new MimeMultipart();
				
				// Create a mime body and Set Text
				BodyPart messageBodyPart = new MimeBodyPart();
				String htmlText = "

Hello

";// Get from Header messageBodyPart.setContent(htmlText, "text/html"); multipart.addBodyPart( messageBodyPart ); String filename = "H:\\MailMesageBody.png"; BodyPart fileBodyPart = new MimeBodyPart(); DataSource source = new javax.activation.FileDataSource( filename ); fileBodyPart.setDataHandler( new DataHandler( source ) ); String header_name = "Content-ID", header_value = ""; fileBodyPart.setHeader(header_name, header_value); multipart.addBodyPart( fileBodyPart ); // Send the complete message parts message.setContent( multipart, Part.INLINE ); } else if ( contentType.equalsIgnoreCase("HTML") ) { String htmlData = "

This is actual message embedded in HTML tags

"; message.setContent( htmlData, "text/html"); } else { //Simple Message - [Plain Text | HTML Content] String header_name = "Content-ID", header_value = ""; message.addHeader( header_name, header_value ); message.setContent( body, "text/plain" ); //(OR) message.setText( body ); } // Send message Transport transport = mailSession.getTransport("smtps"); transport.connect(mailhost, MailDomain.USER_NAME.getValue(), MailDomain.PASSWORD.getValue()); message.saveChanges(); transport.sendMessage(message, message.getAllRecipients()); transport.close(); System.out.println("Sent message successfully."); return true; } catch (AddressException ex) { System.out.println(ex); } catch (MessagingException ex) { System.out.println(ex); } catch(Exception ex) { System.out.println(ex); } return false; } public static Session getSessionObject(String mailhost, String mailport, boolean authProps) { System.out.println("==- Outgoing Mail (SMTP) Server details like SMTP properties and Authenticate -=="); Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", mailhost); props.put("mail.smtp.port", mailport); props.put("mail.smtp.auth", "true"); if( mailport.equals("587") ) { props.put("mail.smtp.starttls.enable", "true"); } else { props.put("mail.smtps.ssl.enable", "true"); } props.put("mail.smtp.socketFactory", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.fallback", "false"); props.put("mail.smtp.debug", "true"); props.put("mail.smtp.quitwait", "false"); //create Authenticator object to pass in Session.getInstance argument Authenticator authenticator = new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { PasswordAuthentication passwordAuthentication = new PasswordAuthentication( MailDomain.USER_NAME.getValue(), MailDomain.USER_NAME.getValue()); return passwordAuthentication; } }; // com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); // Get the Session object by Authenticating Password. if ( authProps ){ props.put("mail.smtp.user", MailDomain.USER_NAME.getValue()); props.put("mail.smtp.password", MailDomain.PASSWORD.getValue()); System.out.println("Session DefaultInstance"); return Session.getDefaultInstance(props); //return Session.getInstance(props); } System.out.println("Session Instance with Authentication"); //return Session.getDefaultInstance(props, authenticator); return Session.getInstance(props, authenticator); } }

References By: oracle share on :        :  

Love to hear your Views / Guidance / Recommendations on this Post…

Explore the Technology World

The mind behind Linux & GIT : Linus TorvaldsLinus Torvalds transformed technology twice — first with the Linux kernel, which helps power the Internet, and again with Git, the source code management syst ...Read MoreTensorflow and deep learningWith Tensorflow ,deep machine learning transitions from an area of research into mainstream software engineering. ...Read MoreJava program to return all possible permutations from collection of distinct numbersexample of Java program to return all possible permutations from collection of distinct numbers ...Read MoreReal Time Analytics with Spring Application Development FrameworkSpring is the most popular application development framework for enterprise Java.Millions of developers use Spring to build simple, truly portable, fast and fle ...Read MorePath Setting in Java When an application is launched from command prompt Windows OS searches for the executable program in the current working directory. The prompt throws an error ...Read MorePicture-in-Picture App for Android devices with Source CodePicture-in-Picture is a special type of multi-window mode mostly used for video playback for Android devices.Released under Apache License 2.0 , which means you ...Read MoreFacebook |Google+ | Twitter | LinkedIn | Youtube About | Cookies-Policy | Disclaimer | Site Map | Contact

Call / Visit for New Batch

Services

Why Join Us

  • ➯ Free Demo Classes
  • ➯ No Registration Fee
  • ➯ Interview Questions
  • ➯ Study Materials
  • ➯ Softwares
  • ➯ Aptitude & Reasoning
  • ➯ Placement Assitance

Address

  • MyJava Training Institute ,
  • Plot No.243 , shop No 14 ,
  • Secnd Floor , Zone-II ,
  • M.P.Nagar , Bhopal,
  • Madhya Pradesh ( 462011 )
  • ☎  08871209400 , 7987028543
  • 🌐 www.myjava.in
FacebookGoogle +TwitterYouTube

MyJava - java Training Institute