Automatically send mail from the background (Java Mail, Web Edition)

xiaoxiao2021-03-06  40

Web.xml file

mailsenderservlet COM. Email.mailsenderServlet server SMTP.163.com port 25 < / init-param> 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 threadt;

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 flyxxxx@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; // The sender address private string username; // sender's user name private string password; // sender's password prizter boolean validate = true; // mail server requires authentication private file root = null; // Web Root Catalog 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 () {flag = false;} / ** * Retrieve database and send mail * / private void sendData () {resultset = null; // read database data try {session session = session.GetInstance (getproperties (), new authentic ()); when (flag) {// server stops exiting the thread string toaddress = null; // Send an address (obtained from RS) string subject = null; // Mail Topic string content = null; // Mail content string file [] = null; // All attachments (absolute path) Sendmail (session, toaddress, subject, content, file); // Send mail}} catch (Exception E) {E.PrintStackTrace (); } Finally {if (rs! = 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 {

Toaddress = "lijin@regaltec.com.cn";

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 Topic 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);

}

}

转载请注明原文地址:https://www.9cbs.com/read-64835.html

New Post(0)