JavaMail API - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JavaMail Management

JavaMail Management

shape Description

JavaMail Management explains how to manage folders, quota, bounced message in the JavaMail API. Generally JavaMail API involves to send, receive, compose an email by making use of Java program.The JavaMail API management invoves size, limit, listing, delete the mails based on name or number to reduce the complexity. Have to know where the message is stored in inbox or any other folder. Here discussing about some management techniques involved in JavaMail API. 

shape Conceptual figure

Above figure shows managing the simple JavaMail from sender's to recipients.

Bounced Mails

shape Description

Generally bouncing of message will occur, but RFC 1211 gives more information on error reports. The particular existence username is determined only by the server, The server will returns the message indicates that the failure of sending message to sender with its original message only when server detects an error. To correlate the error message with original message and sending notification to sender is difficult and causes problem. For delivery status notifications the JavaMail API supports for resolve it. There are some techniques and heuristics for to overcome with this problem. One of the technique to set the return path in envelope. The java.mail.smtp.from represents the address where the bounced mails are sent and explained below with example in detail.

shape Examples

[c]package firstmail; import java.util.Properties; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class bouncedmails { public static void main(String[] args) throws Exception { String smtpServer = "smtp.gmail.com"; int port = 465; final String userid = "9saireddy@gmail.com";//change accordingly final String password = "splesson";//change accordingly String contentType = "text/html"; String subject = "test: bounce an email to a different address " + "from the sender"; String from = "9saireddy@gmail.com"; String to = "bouncer@fauxmail.com";//some invalid address String bounceAddr = "9saireddy@gmail.com";//change accordingly String body = "Test: get message to bounce to a separate email address"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", smtpServer); props.put("mail.smtp.port", "465"); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.from", bounceAddr); Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userid, password); } }); MimeMessage message = new MimeMessage(mailSession); message.addFrom(InternetAddress.parse(from)); message.setRecipients(Message.RecipientType.TO, to); message.setSubject(subject); message.setContent(body, contentType); Transport transport = mailSession.getTransport(); try { System.out.println("Sending ...."); transport.connect(smtpServer, port, userid, password); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); System.out.println("Sending done ..."); } catch (Exception e) { System.err.println("Error Sending: "); e.printStackTrace(); } transport.close(); }// end function main() }[/c] Output [c]Sending .. Sent .[/c]

Quota Management

shape Description

The quota management involves the size, limit of messages or amount of messages in email store and quota is limited in JavaMail API. The JavaMail API calls quota to counts request of each and every mail service. There are some quota criteria, they are Including attachments maximum size of incoming messages. Including attachments maximum size of outgoing messages. At the time recipient is a administrator, maximum size of the message.

shape Table

Class Description
public class quota It represents set of quotas from root of quota with set of resources, setsourcelimit(string name, long limit) is the method for resource class like STORAGE.
public interface QuotaAwareStore It represents an interface to implement which supports quotas, the setQuota, getQuota methods are defined by IMAP QUOTA extension and supported by quota model. IMAPStore, IMAPSSLStore, GmailStore, GmailSSLStore are the implementing class of interface.
public static class Quota.Resource It represents the each resource from root of quota.

shape Examples

Let us see the example for quota management below. [c]import java.util.Properties; import javax.mail.Quota; import javax.mail.Session; import javax.mail.Store; import com.sun.mail.imap.IMAPStore; public class QuotaExample { public static void main(String[] args) { try { Properties properties = new Properties(); properties.put("mail.store.protocol", "imaps"); properties.put("mail.imaps.port", "993"); properties.put("mail.imaps.starttls.enable", "true"); Session emailSession = Session.getDefaultInstance(properties); // emailSession.setDebug(true); // creating the IMAP3 store object and connect with the pop server Store store = emailSession.getStore("imaps"); // To change the user and password accordingly store.connect("imap.gmail.com", "abc@gmail.com", "*****"); IMAPStore imapStore = (IMAPStore) store; System.out.println("imapStore - " + imapStore); // To get quota Quota[] quotas = imapStore.getQuota("INBOX"); //Iterate through the Quotas for (Quota quota : quotas) { System.out.println(String.format("quotaRoot:'%s'", quota.quotaRoot)); //Iterate through the Quota Resource for (Quota.Resource resource : quota.resources) { System.out.println(String.format( "name:'%s', limit:'%s', usage:'%s'", resource.name, resource.limit, resource.usage)); } } } catch (Exception e) { e.printStackTrace(); } } } [/c] Output [c]imapStore -imaps://abc%40gmail.com@imap.gmail.com quotaRoot:'' name:'STORAGE', limit:'15728640', usage:'513'[/c] After getting the store object then have to fetch quota array and iterate it, print it. Here imap.gmail.com server is used for implementation of QuotaAwareStore by IMAPStore.

Folder

shape Description

Folder class is an abstract class to specify the folder for mail messages. To get the folder only after store connection is possible. And the messages actually stored in folder. The folder class is declared as shown below public abstract Class Folder extends Object The function of this class have to declare the methods for deleting, searching for folder, listing messages in folder and requests named folders from the server. By using protected constructor have to create directly a folder. From session, store also have to get folder by using getFolder() method. public abstract Folder getFolder(String name) throws MessagingException

shape Table

Method Description
boolean exists() The function of this method is to check the folder exists really or not.
abstract void open(int mode) The function of this method is to open the folder if it is closed.
abstract void close(boolean expunge) The function of this method is to close the folder when the boolean value is true.
abstract String getName() The function of this method is to return name of folder.
URLName getURLName() The function of this <method is to return the URL name to represent particular folder.
Store getStore() The function of this method is to return the store object which specifies this folder.
int getMode() The function of this method is to return the mode from Folder.READ_ONLY, Folder.WRITE_ONLY.
abstract boolean delete(boolean recurse) The function of this method is to delete folder which is closed and when boolean returns true.
void copy Message(Messages[] mess, Folder destination The function of this method is to copy the message from one folder to specified folder.
abstract Message expunge() The function of this method is to remove the messages deleted from folder.
Folder[] list() The function of this method is to represent the list of array folders.
abstract Folder[] list(String pattern) The function of this method is to represent the folders list in pattern specifying string as name of folder.
int getNewMessagesCount() The function of this method is to return the count of new messages in folder while RECENT flag is set.
Message[] getMessages() The function of this method is to return the objects of message in which represents all messages in folder.
Message[] search(SearchTerm term) The function of this method is to return to search the messages from folders which matches the criteria and empty if matches not found.
abstract Flags getPermanentFlags() The function of this method is to represent that the flags to support all the messages by the folder.
void setFlags(Message[] messages, Flags flag, boolean value) The function of this method is to set the represented flag on message in array.

Summary

shape Key Points

  • JavaMail Management - Managing the bounced Mails.
  • JavaMail Management - Managing the quota in JavaMail API.
  • JavaMail Management - Managing the folder to store and retrieve the messages, to avoid correlation of messages.
  • JavaMail Management - Generally bouncing of message will occur, but RFC 1211 gives more information on error reports.
  • JavaMail Management - The quota management involves the size, limit of messages or amount of messages in email store and quota is limited in JavaMail API.