Web.xml file
http://java.sun.com/dtd/web-app_2_3.dtd ">
MailsenderServlet
com.email.mailsendersRVlet
Server
SMTP.163.com
port
25
Address
Username@163.com
Username
Username
Password
******
Validate
True
Time
5
1
servlet:
Package com.email;
Import javax.servlet. *; import javax.servlet.http. *; import java.io. *;
Public Class MailsenderServlet Extends Httpservlet {Private Mailsender Sender; Private Thread T;
public void init () throws ServletException {sender = new MailSender (); sender.setRoot (root); String server = getInitParameter ( "server"); String port = getInitParameter ( "port"); String address = getInitParameter ( "address" String username = getinitParameter ("username"); string password = getInitParameter ("password"); if (server == null || port == null || address == null || username == null || Password = = null) {system.out.println ("System file Web.xml error: mail sender initialization failed!"); return;} sender.setServer; sender.setport (port); sender.SetAddress (Address) Sender.setUsername (username); sender.setPassword (Password);
String time = getinitParameter ("time"); if (time! = Null) {sender.parseint (time));} String Validate = GetInitParameter ("Validate"); if (Validate! = Null) {Sender .SetValidate (Boolean.Valueof (Validate) .BooleanValue ());} T = new thread (sender); t.start (); // Start mail sending thread}
Public void destroy () {sender.stop (); // Stop mail sending thread try {t.join (1000); if (t.isalive ()) {system.out.println ("Mail Sending thread is not stopped." );}} Catch (exception e) {}}}
Mail sender
Package com.email;
Import java.io. *; import java.util. *; import java.util.date; import javax.ctivation. *; import javax.mail. *; import javax.mail.Mersage. * Import Javax.mail.Internet. *;
/ ** *
Copyright: CopyRight (C) 2004
* @Author
Flyxxxxx@163.com
* @version 1.0
* /
Public class mailsender imports runnable {private int Time = 5 * 60 * 1000; // Scanning Database Interval Private Boolean Flag = True; // Stop Thread Tag Private String Server = "127.0.0.1"; // SMTP Server Address Private String Port = "25"; // SMTP server port private string address; // Used to send email sender address private string username; // sender's username private string password; // sender's password private boolean validate = TRUE; // mail server requires authentication private file root = null; // web root directory mailconfigmanager manager = mailconfigmanager.getinstance ();
Public mailsender () {}
Public void setaddress (string address) {this.address = address;}
Public void setpassword (string password) {this.password = password;}
Public void setPort (String port) {if (port! = null && port.length ()> 0) {this.port = port;}}
Public void setServer (string server) {this.server = server;
Public void setusername (string username) {this.username = usrname;}
Public void setvalidate (Boolean Validate) {this.validate = validate;}
Public void settime (int minute) {this.time = minute * 60 * 1000;
Public void run () {long lasttime = new date (). gettime (); // Save the time when sending the message before the message is exited when (Flag) {// server stops the thread long k = new date (). gettime () - lasttime; if (k <-1000) {// prevents system modification time LastTime = new Date (). gettime (); contract;} if (k> time) {// exceeds the set time interval Start sending mail sendData ( LastTime = new Date (). GetTime ();} try {thread.sleep (100);} catch (exception e) {}}} public void stop ()}} public void stop ()}}
/ ** * Retrieve the database, and send a message * / private void sendData () {resultset = null; // read database data try {session session = session.GetInstance (), new authentic ()); while (); while FLAG) {// server stops When the thread string toaddress = null; // Send an address (obtained from RS) String Subject = null; // Mail Topic string content = null; // message content string file [] = NULL; // All attachments (absolute path) Sendmail (session, toaddress, subject, content, file); // Send mail}} catch (EXCEPTION E) {E.PrintStackTrace ();} finally {ix (r = null) { Try {r.close ();} catch (sqlexception e) {}}}}
/ ** * Send a message * @Param session session * @Param toaddress destination address * @Param Subject Email theme * @Param Content Email Content (HTML) * @Param Files Mail Accessories * @Return Send Success * / Private Boolean Sendmail Session Session, String Subject, String Content, String [] Files {
Try {Message RS = New MimeMessage (SESSION);
Address from = new internetdress (address); rs.setFrom (from); // Send Address Rs.SetRecipient (RecipientType.to, New InternetDress (toadDress)); // Receive address rs.setSubject (Subject); // Mail Topics Multipart mp = new mimeMultipart (); bodybody html = new mimebodypart (); html.setcontent (Content, "text / html; charset = GBK"); // message HTML content mp.addbodypart (html); if (files! = Null && files.length> 0) {// mail attachment for (int i = 0; i Public void set (file root) {this.root = root; Class Authentic EXTENDS Authenticator {// Verification Password Public automation () {} Public PasswordAuthentication GetPasswordAuthentication () {Return New PasswordAuthentication (username, password); } }