Java Mail API and its application-implementation of a Mail list server (IV) (posting)
Last Update:2017-01-13
Source: Internet
Author: User
Appendix: ListServer. java
/**
* ListServer provides the basic mail list service: reads all the new mails of the specified email account and forwards the emails
* The format of the email account in all email accounts (subscribers) specified in the emailListFile file. emailListFile
* One email account for each row.
*/
Import java. util .*;
Import java. io .*;
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 (): creates and starts a ListServer instance.
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 cycle (minutes) sender name ");
System. exit (1 );
}
// Assign the command line parameter to the corresponding variable
String smtpHost = args [0], // smtp server
Pop3Host = args [1], // pop3 server
User = args [2], // pop3 email account
Password = args [3], // pop3 mail account password
EmailListFile = args [4], // subscriber list 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 process new emails cyclically
ListServer ls = new ListServer ();
Ls. setDebug (false );
While (true)
{
Ls. debugMsg ("ListServer startup ...");
Ls. process (smtpHost, pop3Host, user, password, emailListFile, fromName );
Ls. debugMsg ("ListServer pause (rest" + checkPeriod + "minutes )");
Thread. sleep (checkPeriod * 1000*60 );
}
}
// Process (): check the new email and call processMsg () to process a single email.
Public void process (String smtpHost, String pop3Host, String user, String password, String emailListFile, String fromName)