Server | Mailing List Appendix: Listserver.java
/**
* 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 java.io.*;
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 ();
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.open (Folder.read_write);
Get new Message Count
int totalmessages = Folder.getmessagecount ();
if (totalmessages = 0)
{
Debugmsg (Folder + "no new Mail");
Folder.close (FALSE);
Store.close ();
Return
}
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 ();
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.