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

Source: Internet
Author: User
Tags date command line int size mail all mail mail account
Server | Mailing List Appendix:
* Class Listserver provides basic mailing list service features: Read all new messages for the specified mail account and forward to
* All mail accounts (subscribers) specified in the Emaillistfile file. Mail account format in Emaillistfile
* For each line of a mail account.
Import java.util.*;

Import javax.mail.*;
Import javax.mail.internet.*;
Import javax.activation.*;

public class Class1
private static final String INBOX = "INBOX", pop_mail= "POP3", smtp_mail= "SMTP";
Private Boolean Debugon = false;
Private String _smtphost = null,
_pop3host = NULL,
_user = NULL,
_password = NULL,
_emaillistfile = NULL,
_fromname = null;
Private internetaddress[] _tolist = null;

Main (): Create and start listserver instances
public static void Main (String args[])
Throws Exception
Check input parameters
if (Args.length < 6)
System.err.println ("Usage:java listserver SMTP Server POP3 server POP3 account POP3 account password subscriber list file check period (minutes) sender name");
System.exit (1);

Assigning command line arguments to the corresponding variable
String SMTPHost = args[0],//SMTP server
Pop3host = args[1],//POP3 server
user = args[2],//POP3 mail Account
Password = args[3],//POP3 mail account password
EMAILLISTFILE=ARGS[4],//subscriber manifest file
FromName = null; Sender Name

int checkperiod = Integer.parseint (args[5]); New mail Check cycle

if (Args.length > 6)
FromName = args[6];

Create a listserver instance to recycle new messages
Listserver ls = new listserver ();
Ls.setdebug (FALSE);
while (true)
Ls.debugmsg ("listserver start ...");
Ls.process (SMTPHost, pop3host, user, password, emaillistfile, fromname);
Ls.debugmsg ("listserver pause" + checkperiod + "Minutes");
Thread.Sleep (CHECKPERIOD*1000*60);

Process (): Check for new messages, call PROCESSMSG () to process a single message
public void process (string smtphost, String pop3host, string user, string password, string emaillistfile, String fromname)
Throws Exception
_smtphost = SMTPHost;
_pop3host = Pop3host;
_user = user;
_password = password;
_emaillistfile = Emaillistfile;
if (fromname!= null)
_fromname = FromName;

Read the Subscriber account into Java.util.Vector
Vector vlist = new vector (10);
BufferedReader listfile = new BufferedReader (new FileReader (Emaillistfile));
String line = null;
while (line = Listfile.readline ())!= null)
Vlist.addelement (new internetaddress (line));
Listfile.close ();

Debugmsg ("Total" + vlist.size () + "subscribers");

_tolist = new Internetaddress[vlist.size ()];
Vlist.copyinto (_tolist);
Vlist = null;

Get Session Object
Properties sysproperties = System.getproperties ();
Session session = Session.getdefaultinstance (sysproperties, NULL);
Session.setdebug (Debugon);

Connecting to the POP3 mail server
Store store = Session.getstore (Pop_mail);
Store.connect (Pop3host,-1, _user, _password);

Open the default folder
Folder folder = Store.getdefaultfolder ();
if (folder = = null)
throw new NullPointerException ("No default mail Folder");
folder = Folder.getfolder (INBOX);
if (folder = = null)
throw new NullPointerException ("Unable to open folder:" + folders); (Folder.read_write);

Get new Message Count
int totalmessages = Folder.getmessagecount ();
if (totalmessages = 0)
Debugmsg (Folder + "no new Mail");
Folder.close (FALSE);
Store.close ();

Get properties and tags for all new 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);

Process each new message
for (int i = 0; i < messages.length; i++)
if (!messages[i].isset (Flags.Flag.SEEN))
Processmsg (SMTPHost, messages[i]);
Messages[i].setflag (Flags.Flag.DELETED, true);

Folder.close (TRUE);
Store.close ();

Processmsg (): Analyze a single new message and call Sendmsg () to forward it to all subscribers
private void Processmsg (String smtphost, message message)
Throws Exception
String Replyto=_user, subject, Xmailer, MessageText;
Date sentdate;
int size;
Address[] A=null;

Get message headers (sender, recipient, message subject, Date sent, etc.)
if ((A = Message.getfrom ())!= null)
ReplyTo = A[0].tostring ();

Subject = Message.getsubject ();
Sentdate = Message.getsentdate ();
Size = Message.getsize (); string[] HDRs = Message.getheader ("X-mailer");
if (HDRs!= null)

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.