Disk File monitoring (Java) and sending e-mail notifications, system regular execution methods

Source: Internet
Author: User
Tags mailmessage

The following is the configuration of the monitoring path, file, and message information through the XML file, and the read XML file uses the three-party jar package: dom4j

Send and receive mail using the most common javamail, need two jar package, Mail.jar and Activition.jar

The key code is as follows:

public boolean sendhtmlmail (Mailsenderinfo mailinfo) {
Determine if identity authentication is required
Myauthenticator authenticator = null;
Properties Pro = Mailinfo.getproperties ();
If authentication is required, create a password validator
if (Mailinfo.isvalidate ()) {
Authenticator = new Myauthenticator (Mailinfo.getusername (), Mailinfo.getpassword ());
}
Constructs a session that sends messages based on mail session properties and password validators
Session sendmailsession = session.getdefaultinstance (pro,authenticator);
Sendmailsession.setdebug (TRUE);
Sendmailsession.setdebugout (System.out);
try {
Create a mail message based on session
Message MailMessage = new MimeMessage (sendmailsession);
Create a Mail Sender address
Address from = new InternetAddress (mailinfo.getfromaddress ());
Set the sender of a mail message
Mailmessage.setfrom (from);
Create the recipient address of the message and set it to the mail message
Address to = new InternetAddress (mailinfo.gettoaddress ());
The Message.RecipientType.TO property indicates that the recipient's type is to
Mailmessage.setrecipient (message.recipienttype.to,to);
Set the subject of a mail message
Mailmessage.setsubject (Mailinfo.getsubject ());
Set when mail messages are sent
Mailmessage.setsentdate (New Date ());
The Minimultipart class is a container class that contains objects of type MimeBodyPart
Multipart Mainpart = new Mimemultipart ();
Create a mimebodypart that contains HTML content
BodyPart html = new MimeBodyPart ();
Set HTML content
Html.setcontent (Mailinfo.getcontent (), "text/html; Charset=utf-8 ");
Mainpart.addbodypart (HTML);
Set the Minimultipart object to the message content
Mailmessage.setcontent (Mainpart);
Send mail
Transport.send (MailMessage);
return true;
} catch (Messagingexception ex) {
Ex.printstacktrace ();
}
return false;
}

Here's another thing to say is that the conversion between mb,kb and bytes, get the file length (file.length) After getting a long byte number, 1mb=1024kb,1kb=1024b, it is best to use this notation:

BigDecimal megabyte = new BigDecimal (1024 * 1024);
Float returnvalue = Filesize.divide (megabyte, 2, bigdecimal.round_up). Floatvalue ();

More accurate accuracy !

about how to make a jar package and run it in a timed way:

Due to the use of three additional jar packages, all packaging methods are as follows:

The first step:

Step Two:

The third step: Choose your own project, storage path, choose to join the jar package, complete

Attention:

The well-made jar package does not have the entrance procedure, must set itself, this side does not have much to repeat

Scheduled operation requires the use of the system's own planned tasks:

There's one here. Create a basic task and create a task, select Create Task. Here are the two differences, the creation task is a custom task, can be executed in a loop, or it can be finished. Basic tasks are tasks that are performed together when the system starts, and are destroyed only when the system shuts down.

The creation of the task here is not more than that, it is relatively simple, it should be noted that: Jar package is not directly run, you need to use the bat file through the Jar-jar package name parameter "parameter. "Is executed, so the action inside the task executes the bat file instead of the jar package

Disk File monitoring (Java) and sending e-mail notifications, system regular execution methods

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.