Java mail

JavaMail Projects

JavaMail Project 1

JAVA Mail Using Session Beans
Previous Home Next

Finally Sending the Message through the help of Transport classes. The Transport class speaks the protocol-specific language for sending the message with using SMTP. The Transport class is an abstract class and his working like as Session. If you want to use the Transport class , so default version of the class by just calling the static send() method:


youcan get a instance from Session for your protocol. After that you pass the along for username and password sending this message and finally close the connection.

message.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());

when you need to send multiple messages it will keep the connection with the mail server active between messages. The basic send() mechanism makes a separate connection to the server for each method call. Watch the mail command then go to mail server an set the debug flag with session.setDebug(true);


Store And Folder

There are a many mails sending with a Session. However , after getting the session. then you connect to Store to username, password and Authenticator. we can say that it working for same Transport. On which protocol is use to store .

// Store store = session.getStore("imap");
Store store = session.getStore("pop3");
store.connect(host, username, password);

The mails are Stored after that get a Folder and if you want to read this message then open the Folder and read this messages.

Folder folder = store.getFolder("INBOX");;
Message message[] = folder.getMessages();

when you using POP3 the only folder available is the INBOX. If you are using IMAP, you can have other folder available. Sun's provides are smart. While Message message[]=folder.getMessage(); a slow operation reading every message from the server, only when you actually need to get a part of the message is the message content retrieved. you have Message to read then it's get content with getContent () or if you are write this content then using writeTo (). The getContent () method only gets the message content, while writeTo() output includes headers. System.out.println(((MimeMessage)message).getContent());

Finally you're done reading mail, close the connection to the folder and store.



Lastly Close the folder states for the close() method, after close folder to update folder removing deleted the message.

Previous Home Next