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

xiaoxiao2021-03-06  119

Web.xml file mailsenderrseRVlet com.email.mailsenderServlet server SMTP.163. COM port 25 address username@163.com UserName username password ****** < Init-param> 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 delivery Program initialization failed! "); Return;} sender.setServer (Server); sender.setPort (port); sender.setAddress (address); sender.setparsword (parame); String Time = GetItParameter "Time"); if (time! = null) {sender.Parseint (Time));} String Validate = GetInitParameter ("Validate"); if (VA Lidate! = null) {serolean.valueof (validate) .booleanValue ());} T = new thread (sender); t.start (); // Start mail sending thread} public void destroy () { Sender.Stop (); // Stop Mail Send Thread Try {T.Join (1000); if (T.IsAlive ()) {System.out.Println ("The mail sending thread is not stopped.

");}}}}}}}}} Mail sender package com.email; import java.io. *; Import java.sql. *; Import java.util. *; Import java.util.date; Import javax.Activation. *; import javax.mail. *; import javax.mail.Mel.MPort javax.mail.internet. *; / ** *

Copyright: CopyRight (c) 2004 * @author flyxxxxx@163.com * @version 1.0 * / public class MailSender implements Runnable {private int time = 5 * 60 * 1000; // database scan interval private boolean flag = true; // stop the 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; password of the sender private // boolean validate = true; // message authentication server requires private File root = null; // WEB root MailConfigManager manager = MailConfigManager.getInstance (); public MailSender () {} public Void setadDress (string 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 = username;} 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) {/ / Prevent system modification time LastTime =

New Date (); gettime (); payue;} if (k> time) {// exceeds the set time interval Start sending mail sendData (); LastTime = new Date (). gettime ();} try {thread.sleep (100);}}}}} public void stop () {FLAG = false;} / ** * Retrieve database and send mail * / private void senddata () {results = null; // Read Database Data Try {session session = session.getInstance (getProperties (), new authentic ()); when (flag) {// server stops exiting the thread string toaddress = null; // Send 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 (E) {E.PrintStackTrace ();} finally {if (rs! = Null) {Try {r} ();}}}}} / ** * Send Mail * @Param session session * @Param toaddress destination address * @Param Subject mail topic * @PAR am content message content (HTML) * @param files email attachments * @return sent successfully * / private boolean sendMail (Session session, String toAddress, String subject, String content, String [] files) {toAddress = "lijin @ regaltec. com.cn "; try {Message rs = new MimeMessage (session); address from = new InternetAddress (address); rs.setFrom (from); // send address rs.setRecipient (RecipientType.TO, new InternetAddress (toAddress)) ; // Receive address rs.setSubject (subject); // mail topic Multipart MP = new mimemultipart (); bodypart html = new mimebodypart ();

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

New Post(0)