Java Mail API:
-->DB software manages the data in the form of DB table cols
-->WebServer manages and executes web application
-->JNDI Registry manages objects
-->Similarly mail server manages and executes Email Accounts and Email Messages
Java App---(jdbc api(java.sql.javax.sql,...))---->DW Software
Java App---(jndi api(javax.naming and its sub packages))--->JNDI registry
E.g:- rmi registry, Cos registry...
Java App--(java mail api(javax.mail,javax.activation ))-->Mail Server
E.g:- james server, Lotus Notes, Microsoft exchange server
Mail Server Architecture:-
1.)SMTP=Simple Mail Transfer Protocol
2.)POP=Post Office Protocol
3.)IMAP=Internet Mail Access Protocol
-->Incoming server receives and manages email messages in the email accounts
-->Outgoing server sends the email messages from one account to another account
-->The pop3 enabled mail server sends the email messages to mail client permanently, so there onwards maintaing email messages is the responsibility of mail client
-->The IMAP enabled mail server maintains the email message even after it is ready by mail client
Gmail MailServers Outgoing Server Details:-
Host name: smtp.gmail.com
Port no: 465
-->when startls is enabled the port no: 587
Incoming server Details:-
Host name: pop.gmail.com
Port no: 995
Possible Email Operations:-
1.)Send mail
2.)Send mail with attachment
3.)Read Mail
4.)Delete Mail
To establish the connection to outgoing server of gmail
Steps:-
- //prepare mail properties
- Properties props=new Properties();
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.port", "465");
- props.put("mail.smtp.auth","true");
- props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
- //create session object representing connection with outgoing server
- Session session=Session.getInstance(props,auth(object of class extends Authenticator );
- private class MyAuthenticator extends Authenticator
- {
- public static PasswordAuthentication getPasswordAuthentication() {
- return MyAuthenticator auth=new MyAuthenticator();
- }
-->java mail api comes in the form of javax.mail<ver>.jar(collect it from mvn repository.com)
-->To establish connection with incoming serverof gmail, we can use the above code but in the mail property names replacesmtp with pop3 and use the following host, port details
Host name: pop3.gmail.com
Port no: 995
ConnTest:
- package com.nt.mail;
- import java.util.properties;
- import javax.mail.Session;
- public class ConnTest{
- public static void main(String... args) throws Exception{
- //Prepare Mail Properties
- Properties props=new Properties();
- props.put("mail.transport.protocol","smtp");
- props.put("mail.smtp.host","localhost");
- props.put("mail.smtp.port","25");
- //Get Session Object
- Session ses=Session.getInstance(props);
- if(ses==null)
- System.out.println("Connection not established");
- else
- System.out.println("Connection established");
- }
- }
Procedure to send Email Message:-
1.)Prepare mail properties
2.)Prepare Authenticator object
3.)Create session object having main properties, authenticator object (Establish the connection without going server to gmail mail server)
4.)Prepare Email message having header, body values
5.)Send Email Messages
SendMailTest:
SendMailTest:
- package com.nt.mail;
- import java.util.Date;
- import java.util.Properties;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMessage.RecipientType;
- public class SendMail {
- public static void main(String[] args) throws Exception{
- Properties props=new Properties();
- props.put("mail.transport.protocol","smtp");
- props.put("mail.smtp.host","localhost");
- props.put("mail.smtp.port","25");
- //get Session obj
- Session ses=Session.getInstance(props);
- //Create Email Message
- MimeMessage message=new MimeMessage(ses);
- message.setFrom(new InternetAddress("kalam1"));
- message.setRecipient(RecipientType.TO,new InternetAddress("modi1"));
- message.setSubject("open it to know it");
- message.setSentDate(new Date());
- message.setText("Mr.Modi Do not declare holiday on the day of my death, Instead make people to work an extra day");
- //send message
- Transport.send(message);
- System.out.println("Mail has been delivered");
- }
- }
Sending Email Message with Attachment:-
(1),(2),(3) same as above
(4)Prepare Email message just having header values
(5)Prepare body of type Multipart
(6)Prepare body parts(like text, attachment and etc...)
(7)Add Body part2 to body
(8)Set body as the Message content
(9)Send message
SendMailAttachment:
SendMailAttachment:
- package com.nt.mail;
- /*
- Java Application to send an E-mail with attachment
- */
- import java.util.Date;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.mail.Message;
- import javax.mail.Message.RecipientType;
- import javax.mail.Multipart;
- 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 SendAttachment
- {
- public static void main(String []args) throws Exception
- {
- //Creating Properties Object
- Properties properties=new Properties();
- //adding protocol,mailserver address & the port number of the mailserver
- properties.put("mail.transport.protocol","smtp");
- properties.put("mail.smtp.host","localhost");
- properties.put("mail.smtp.port","25");
- //Creating the Session Object
- Session session=Session.getInstance(properties);
- //Creating and configuring the Message object
- Message message=new MimeMessage(session);
- message.setFrom(new InternetAddress("kalam1"));
- message.setRecipient(RecipientType.TO,new InternetAddress("modi1"));
- message.setSentDate(new Date());
- message.setSubject("MAIL WITH ATTACHMENT");
- // mail body obj
- Multipart mailbody=new MimeMultipart();
- // mail body part1 obj (text content)
- MimeBodyPart part1=new MimeBodyPart();
- part1.setText("hello see my resume");
- mailbody.addBodyPart(part1);
- //mail body part2(attachment)
- FileDataSource fds=new FileDataSource("src/resume.txt");// represents attachment file
- MimeBodyPart part2=new MimeBodyPart();
- part2.setDataHandler(new DataHandler(fds));
- part2.setFileName(fds.getName());
- mailbody.addBodyPart(part2);
- message.setContent(mailbody);
- //Sending the mail.
- Transport.send(message);
- System.out.println("Mail has been delivered");
- }//main
- }//class
- //>javac SendAttachment.java
- //>java SendAttachment one.txt
To read Email Messages:-
(1),(2),(3) same as above but pointing to incoming server
(4)Connect to pop3s store based Email account
(5)Open Inbox folder in ReadOnly mode
(6)Read all the messages
(7)Close folder and store
ReadMail:
ReadMail:
- package com.nt.mail;
- import java.util.Properties;
- import javax.mail.Folder;
- import javax.mail.Message;
- import javax.mail.Session;
- import javax.mail.Store;
- public class RecieveMail {
- public static void main(String[] args)throws Exception {
- Properties properties=new Properties();
- properties.put("mail.transport.protocol","pop");
- properties.put("mail.pop.port","110");
- properties.put("mail.host","localhost");
- Session session=Session.getInstance(properties);
- Store store=session.getStore("pop3");
- store.connect("localhost","modi1","modi1");
- // Access InBox
- Folder inbox=store.getFolder("INBOX");
- inbox.open(Folder.READ_ONLY);
- //read email messages
- System.out.println("Messages count"+inbox.getMessageCount());
- Message messages[]=inbox.getMessages();
- //Display messages
- for(Message msg:messages){
- msg.writeTo(System.out);
- System.out.println("------------------------------");
- }
- store.close();
- inbox.close(false);
- }
- }
For Deleting Email Messages:-
(1),(2),(3) &(4) are same as read mail
(5)Open Inbox folder in READWrite mode
(6)Read all messages or specific messages
(7)Mark certain messages for deletion(Flags.Flag.DELETED,true)
(8)Close Inbox with flag true
DeleteMail:
DeleteMail:
- package com.nt.mail;
- import java.util.Properties;
- import javax.mail.Flags;
- import javax.mail.Folder;
- import javax.mail.Message;
- import javax.mail.Session;
- import javax.mail.Store;
- public class DeleteMail {
- public static void main(String[] args) throws Exception{
- Properties properties=new Properties();
- properties.put("mail.transport.protocol","pop");
- properties.put("mail.pop.port","110");
- properties.put("mail.host","localhost");
- Session session=Session.getInstance(properties);
- Store store=session.getStore("pop3");
- store.connect("localhost","modi1","modi1");
- // Access InBox
- Folder inbox=store.getFolder("INBOX");
- inbox.open(Folder.READ_WRITE);
- System.out.println("Messages count"+inbox.getMessageCount());
- //Read messages
- Message message=inbox.getMessage(1);
- //Mark the message for deleteion
- message.setFlag(Flags.Flag.DELETED,true);
- //close inbox with flag true
- inbox.close(true);
- System.out.println("Messages count"+inbox.getMessageCount());
- store.close();
- }
- }
Note: When Inbox is closed with the flag true, all the marked messages for deletion will be deleted
Comments
Post a Comment