Java Mail API and its application--implementation of a mailing list server (ii) (paste)

Source: Internet
Author: User
Tags mail account
Mailing list services are often used to provide an e-mail-based discussion environment for a workgroup, through which subscribers discuss issues of common interest.
The sample program provided in this article listserver is a simple mailing list forwarding server that reads new messages from a specified account and sends them to all subscribers. The Java Mail API not only makes it fairly simple to implement its basic functionality (using the default POP3 and SMTP), but it also ensures that programs are easy to support any system environment that might be encountered.
Running the program requires the following supporting files: Java Mail (Mail.jar), JAF (Activation.jar), and the default POP3 support (Pop3.jar), as shown in the following DOS batch command file (these jar files are available from Sun Java Mail home download, see Related resources):
@echo off
PATH.; d:jdk1.1in
Set classpath=.; D:jdk1.1libclasses.zip;activation.jar;mail.jar;pop3.jar
Java listserver%1%2%3%5%6%7%8%9
After the program starts, the main () process reads the command-line parameters, such as mail server, mail account number, update cycle, and so on. Next, create the Listserver instance, and finally go to the infinite loop "read the new message and forward it to the subscriber/wait until the next check time arrives". The core functions of listserver (that is, reading and forwarding all new messages) are implemented in the process () procedure, and the main procedures are:
Gets the Javax.mail.Session instance and then obtains the Javax.mail.Store instance.
Use the Javax.mail.Store instance to get the default Inbox (INBOX) Javax.mail.Folder instance.
Extracts new messages using the Javax.mail.Folder object, which is stored as an array of Javax.mail.Message objects.
Create Javax.mail.FetchProfile to (potentially) optimize the extraction of components of a particular message.
Please note the application of the Javax.mail.FetchProfile class here. The Fetchprofile class provides mail protocol provider-specific optional parameters for the purpose of achieving a more efficient prefetching of message components. The Java Mail API encourages late extraction of message components--that is, only when they are really needed. This not only helps improve the system's response time, but some actions, such as displaying a list of mailing headers, can also benefit from it. The following code illustrates the basic usage of fetchprofile:
Extract properties and tags 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);
The sendmsg () procedure is invoked by Processmsg () to parse the specified collection and forward it to all subscribers. It first obtains the default session instance, and then creates the Javax.mail.Transport object to send the message:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.