| CARVIEW |
|
mailsystem
|
| Summary | MailingSystem is a high-speed newsletter mailing software |
|---|---|
| Categories | None |
| License | Sun Public License (SPL) |
| Owner(s) | madhu_sud |
Message from the owner(s)
This software is developed in Java using the concept of Network Programming, Client/Server Computing, Object Oriented Design & Pattern and TCP/IP Protocol and Architecture.
Description
How Mail Systems Work
Mail systems consist of two major components: a mail client or user agent (UA), and a mail server or message transfer agent (MTA). User agents let users compose and send email and retrieve email from message transfer agents. Message transfer agents store and forward email for user agents and support the exchange of mail across a network or group of networks.
Examples of user agents are email programs, such as Eudora and Outlook. These programs also provide limited message transfer capabilities. Examples of message transfer agents are email server programs, such as Sendmail and Exchange. On the Internet, the Post Office Protocol 3 (POP3) is the most popular protocol for user agents to receive mail from message transfer agents. The Simple Message Transfer Protocol (SMTP) is the most popular protocol for user agents to send mail to message transfer agents, and for message transfer agents to exchange mail with each other.
Multipurpose Internet Mail Extensions (MIME)
Many Internet programs, including email clients, Web browsers, and Web servers, use MIME to associate an object type with a file. These object types include text, multimedia files, and application-specific files. MIME types consist of a type and a subtype. Examples are text/html, text/plain, image/gif, and image/jpeg, where text and image are the types and html, text, gif, and jpeg are the subtypes.
We can use java for electronic mail. That is, we can use Java to send e-mails newsletters for bulk marketing and advertising.
This article includes:
· Creating messages
· Sending a message
· Storing and retrieving a messages
· Communicate with a client
Sun Microsystems provides JavaMail API. JavaMail API can be used to create email client programs in Java, to mail-enable other programs (such as editors and browsers), or to implement special email features in embedded products, such as Web phones.
Although the Java Mail API contains many more classes than those discussed here, concentrating on some of the core classes to start with makes it easy to understand the essence of the API. The following is a detailed description of these core classes, which include javax.mail.Session, javax.mail.Store, javax.mail.Transport, javax.mail.Folder, and javax.mail.Message.
javax.mail.Session
The javax.mail.Session class is the top-level entry class for the Java Mail API, and its most commonly used methods provide the ability to control and load the classes that represent the service provider implementations (SPI) for various mail protocols.
The constructors for the class are private. You can get a single default session that can be shared with the getDefaultInstance() method:
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
Or, you can create a unique session with getInstance():
Properties props = new Properties();
Session session = Session.getInstance(props, null);
javax.mail.Store
The javax.mail.Store class is implemented by a service provider, such as a POP Mail implementation developer, and allows for read, write, monitor, and search access for a particular mail protocol. The javax.mail.Folder class is accessed through this class.
// Store store = session.getStore("imap");
Store store = session.getStore("pop3");
store.connect(host, username, password);
javax.mail.Transport
The javax.mail.Transport class is another provider-implemented class and is used for sending a message over a specific protocol.
Transport.send(message);
javax.mail.Folder
The javax.mail.Folder class is implemented by a provider; it gives hierarchical organization to mail messages and provides access to e-mail messages in the form of javax.mail.Message class objects.
After connecting to the Store, you can then get a Folder, which must be opened before you can read messages from it:
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();
javax.mail.Message
The javax.mail.Message class is implemented by a provider and models all the details of an actual e-mail message, such as the subject line, sender/recipient e-mail address, sent date, and so on. The guidelines for providers who implement the javax.mail.Message indictate that the actual fetching of e-mail message components should be delayed as long as possible in order to make this class as lightweight as possible.
To create a Message, pass along the Session object to the MimeMessage constructor:
MimeMessage message = new MimeMessage(session);
Once you have your message, you can set its parts, as Message implements the Part interface (with MimeMessage implementing MimePart). The basic mechanism to set the content is the setContent() method, with arguments for the content and the mime type: message.setContent("Hello", "text/plain");
If, however, you know you are working with a MimeMessage and your message is plain text, you can use its setText() method which only requires the actual content, defaulting to the MIME type of text/plain:
message.setText("Hello");
For setting the subject, use the setSubject() method:
message.setSubject("First");
The Java Mail API and JAF
One design fact worth mentioning is that the Java Mail API is tied to, or rather leverages, another Java extension: the Java Activation Framework (JAF). The JAF is intended to unify the manner of working with the multitude of data formats that are available, whether they be simple text or extremely complex documents composed of images, audio, video, and even "live" objects. In this sense, it might be useful to think of the JAF as providing for Java what plug-ins provide to a Web browser
Sending Messages
Sending an email message involves getting a session, creating and filling a message, and sending it. You can specify your SMTP server by setting the mail.smtp.host property for the Properties object passed when getting the Session:
String host = ...;
String from = ...;
String to = ...;
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");
// Send message
Transport.send(message);
// Get attributes & flags for all messages
//
Message[] messages = folder.getMessages();
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
fp.add("X-Mailer");
folder.fetch(messages, fp);
Conclusion:
The Java Mail API presents an excellent opportunity for developers to integrate advanced mail capabilities into their applications with all the benefits that this API can provide: ease of development, multiple protocol support, and flexible integration of future advances. With the introduction to the key Java Mail classes provided in this article, you should now have a good understanding of some of the capabilities of this API.
| Powered by CollabNet | Feedback |
FAQ |
Press |
Developer tools
© 1995 - 2007 CollabNet. CollabNet is a registered trademark of CollabNet, Inc. |
