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

Source: Internet
Author: User
Tags mail mail account
Server | Mailing List Mailing list service is 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.1\bin

Set classpath=.; D:\jdk1.1\lib\;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:

Set properties and get the default process instance
Properties Props = new properties ();
Props.put ("", _smtphost);
Session session = Session.getdefaultinstance (props, null);
Send a message
Transport transport = Session.gettransport (Smtp_mail);
Transport.connect (_smtphost, _user, _password);
Transport.sendmessage (Newmessage, _tolist);

The settings for message fields, such as recipients, senders, message subjects, and dates, are as follows:

Create new mail address replytolist[] = {internetaddress (ReplyTo)};
Message Newmessage = new MimeMessage (session);
if (_fromname!= null)
Newmessage.setfrom new InternetAddress (from,
_fromname + "/" + ReplyTo);
Newmessage.setfrom (New InternetAddress (from));
Newmessage.setreplyto (replytolist);
Newmessage.setrecipients (Message.RecipientType.BCC, _tolist);
Newmessage.setsubject (subject);
Newmessage.setsentdate (sentdate);

Fill in the content of the message should distinguish between its type (plain text and composite content using different methods):

Fill in the contents of the mail Object content = message.getcontent (); Original message Content
String Debugtext = "Subject:" + Subject + ", Send date:" + sentdate;
if (content instanceof Multipart)
Debugmsg ("Forward Compound content mail (" + Debugtext +) ");
Newmessage.setcontent ((Multipart) message.getcontent ());
Debugmsg ("Forward Plain text mail (" + Debugtext +) ");
Newmessage.settext ((String) content);

As you can see, the Javax.mail.internet.MimeMessage content-reading routine getcontent () can read complex, hierarchical composite messages only one call at a time.

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: 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.