Small programs that can be sent to restart the company's computer

Source: Internet
Author: User
Tags rfc822
The company's software often does not work. During the Chinese new year's holiday, it was pulled back to the company to restart the computer, and there was no way to make a small program to start. You can restart your computer by sending an email to the specified mailbox.

Tags:<None> code snippet (2) [Code]Main class
Import Java. io. bufferedoutputstream; import Java. io. dataoutputstream; import Java. io. file; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. text. simpledateformat; import Java. util. calendar; import Java. util. date; import Java. util. properties; import javax. mail. flags; import javax. mail. folder; import javax. mail. message; import javax. mail. nosuchprovider Exception; import javax. mail. session; import javax. mail. store; import javax. mail. urlname; import javax. mail. internet. mimemessage; import mail. recivemail; public class mainserver {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubmainserver SER = new mainserver ();/* runtime run = runtime. getruntime (); try restarting run.exe C ("shutdown-R");} catch (ioexception e) {// todo auto -Generated catch blocke. printstacktrace ();} */string STR = ""; Boolean bool = true; runtime run = runtime. getruntime (); properties props = new properties (); props. setproperty ("mail. SMTP. host "," smtp.163.com "); props. setproperty ("mail. SMTP. auth "," true "); Session session = session. getdefaultinstance (props, null); urlname = new urlname ("POP3", "pop.163.com", 110, null, "email address", "email password"); Date nowtime; while (T Rue) {nowtime = new date (); simpledateformat DFT = new simpledateformat ("yyyy-mm-dd"); Calendar da = calendar. getinstance (); DA. settime (nowtime); DA. get (calendar. minute); If (DA. get (calendar. minute) % 5 = 0 & bool) {store; bool = false; try {store = session. getstore (urlname); store. connect (); folder = store. getfolder ("inbox"); folder. open (folder. read_write); message msgs [] = folder. getmessages (); Int COUNT = msgs. length; system. out. println ("Message count:" + count); Ser. logfile (DA, "I am still alive, found new mail" + Count +! "); Recivemail Rm = NULL; For (INT I = 0; I <count; I ++) {Rm = new recivemail (mimemessage) msgs [I]); // RM. recive (MSGs [I], I); STR = RM. getsubject (); msgs [I]. setflag (flags. flag. deleted, true); msgs [I]. isset (flags. flag. deleted);} folder. close (true); If (! Str. equals ("") {Ser. logfile (DA, STR); run.exe C (STR); run. GC (); STR = "" ;}} catch (exception e) {// todo auto-generated catch blocke. printstacktrace (); Ser. logfile (DA, E. getmessage (); bool = true ;}} else if (DA. get (calendar. minute) % 5! = 0) {bool = true;} Try {thread. sleep (5000);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace (); Ser. logfile (DA, E. getmessage () ;}} public mainserver () {filedir = system. getproperty ("user. dir ") + file. separator + "strvermail.txt"; system. out. println ("filedir ----->" + filedir); try {OUTFILE = new fileoutputstream (filedir, true); bout = new bufferedoutputstream (OUTFILE); dout = new dataoutputstream (bout );} catch (filenotfoundexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} public void logfile (calendar da, string Str) {string writestr = ""; try {simpledateformat DFT = new simpledateformat ("yyyy-mm-dd hh: MM: SS "); writestr = DFT. format (DA. gettime () + ":" + STR + "\ r \ n"; dout. writeutf (writestr); // dout. writechars (writestr); dout. flush ();} catch (exception e) {// todo auto-generated catch blocke. printstacktrace () ;}} fileoutputstream OUTFILE; bufferedoutputstream bout; dataoutputstream dout; string filedir ;}
[Code]A piece of code copied from the Internet to receive emails
Import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. file; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. unsupportedencodingexception; import Java. text. simpledateformat; import Java. util. date; import Java. util. properties; import javax. mail. bodypart; import javax. mail. flags; import J Avax. mail. folder; import javax. mail. message; import javax. mail. messagingexception; import javax. mail. multipart; import javax. mail. part; import javax. mail. session; import javax. mail. store; import javax. mail. urlname; import javax. mail. internet. internetaddress; import javax. mail. internet. mimemessage; import javax. mail. internet. mimeutility; public class recivemail {private mimemessage MSG = NULL; private string Saveattchpath = ""; private stringbuffer bodytext = new stringbuffer (); Private string dateformate = "YY-mm-dd hh: mm"; Public recivemail (mimemessage MSG) {This. MSG = MSG;} public void setmsg (mimemessage MSG) {This. MSG = MSG;}/*** get the sender information * @ return * @ throws messagingexception */Public String getfrom () throws messagingexception {internetaddress [] address = (internetaddress []) MSG. getfrom (); String from = Address [0]. getaddress (); If (from = NULL) {from = "";} string personal = Address [0]. getpersonal (); If (Personal = NULL) {personal = "";} string fromaddr = personal + "<" + from + ">"; return fromaddr ;} /*** get the email recipient, CC, and BCC addresses and information. Different parameters are passed: "to" --> recipient, "cc" --> CC address, "BCC" --> BCC address * @ Param type * @ return * @ throws messagingexception * @ throws unsupportedencodingexception */Public String getmailaddress (string type) throws messagingexception, unsupportedencodingexception {string mailaddr = ""; string addrtype = type. touppercase (); internetaddress [] address = NULL; If (addrtype. equals ("to") | addrtype. equals ("cc") | addrtype. equals ("BCC") {If (addrtype. equals ("to") {address = (internetaddress []) MSG. getrecipients (message. recipienttype. to);} If (addrtype. equals ("cc") {address = (internetaddress []) MSG. getrecipients (message. recipienttype. CC);} If (addrtype. equals ("BCC") {address = (internetaddress []) MSG. getrecipients (message. recipienttype. BCC);} If (address! = NULL) {for (INT I = 0; I <address. length; I ++) {string mail = Address [I]. getaddress (); If (Mail = NULL) {mail = "" ;}else {mail = mimeutility. decodetext (Mail);} string personal = Address [I]. getpersonal (); If (Personal = NULL) {personal = "";} else {personal = mimeutility. decodetext (personal);} string compositeto = personal + "<" + mail + ">"; mailaddr + = "," + compositeto;} mailaddr = mailaddr. substring (1) ;}} Else {Throw new runtimeexception ("error email type! ");} Return mailaddr;}/*** get email subject * @ return * @ throws unsupportedencodingexception * @ throws messagingexception */Public String getsubject () throws unsupportedencodingexception, messagingexception {string subject = ""; subject = mimeutility. decodetext (MSG. getsubject (); If (subject = NULL) {subject = "";} return subject ;} /*** get the mail sending date * @ return * @ throws messagingexception */Public String Getsenddate () throws messagingexception {date senddate = MSG. getsentdate (); simpledateformat SMD = new simpledateformat (dateformate); Return SMD. format (senddate);}/*** get mail body content * @ return */Public String getbodytext () {return bodytext. tostring ();}/*** resolve the email and save the obtained email content to a stringbuffer object. The parsed email mainly performs different operations based on the mimetype, step-by-Step parsing * @ Param part * @ throws messagingexception * @ throws ioexception */pub LIC void getmailcontent (Part) throws messagingexception, ioexception {string contenttype = part. getcontenttype (); int nameindex = contenttype. indexof ("name"); Boolean conname = false; If (nameindex! =-1) {conname = true;} system. Out. println ("contenttype:" + contenttype); If (part. ismimetype ("text/plain ")&&! Conname) {bodytext. append (string) part. getcontent ();} else if (part. ismimetype ("text/html ")&&! Conname) {bodytext. append (string) part. getcontent ();} else if (part. ismimetype ("multipart/*") {multipart = (multipart) part. getcontent (); int COUNT = multipart. getcount (); For (INT I = 0; I <count; I ++) {getmailcontent (multipart. getbodypart (I);} else if (part. ismimetype ("message/rfc822") {getmailcontent (Part) part. getcontent () ;}}/*** determines whether a receipt is required for an email. If yes, return true; otherwise, return false * @ return * @ throws Messagingexception */Public Boolean getreplysign () throws messagingexception {Boolean replysign = false; string needreply [] = msg. getheader ("disposition-Notification-to"); If (needreply! = NULL) {replysign = true;} return replysign;}/*** get the message-id of this email * @ return * @ throws messagingexception */Public String getmessageid () throws messagingexception {return MSG. getmessageid ();}/*** determines whether the email has been read. If not, false is returned. If read, true * @ return * @ throws messagingexception */Public Boolean isnew () is returned () throws messagingexception {Boolean isnew = false; flags = (Message) MSG ). getflags (); flags. FL Ag [] flag = flags. getsystemflags (); system. out. println ("Flags's length:" + flag. length); For (INT I = 0; I <flag. length; I ++) {If (flag [I] = flags. flag. seen) {isnew = true; system. out. println ("seen message ....... "); break;} return isnew ;} /*** determine whether the attachment is contained * @ Param part * @ return * @ throws messagingexception * @ throws ioexception */Public Boolean iscontainattch (Part) throws messagingexception, ioex Ception {Boolean flag = false; string contenttype = part. getcontenttype (); If (part. ismimetype ("multipart/*") {multipart = (multipart) part. getcontent (); int COUNT = multipart. getcount (); For (INT I = 0; I <count; I ++) {bodypart = multipart. getbodypart (I); string dispostion = bodypart. getdisposition (); If (dispostion! = NULL) & (dispostion. equals (part. attachment) | dispostion. equals (part. inline) {flag = true;} else if (bodypart. ismimetype ("multipart/*") {flag = iscontainattch (bodypart);} else {string contype = bodypart. getcontenttype (); If (contype. tolowercase (). indexof ("Appliaction ")! =-1) {flag = true;} If (contype. tolowercase (). indexof ("name ")! =-1) {flag = true ;}}} else if (part. ismimetype ("message/rfc822") {flag = iscontainattch (Part) part. getcontent ();} return flag;}/*** Save the attachment * @ Param part * @ throws messagingexception * @ throws ioexception */Public void saveattchment (Part) throws messagingexception, ioexception {string filename = ""; if (part. ismimetype ("multipart/*") {multipart MP = (multipart) part. getcontent (); for (I NT I = 0; I <MP. getcount (); I ++) {bodypart mpart = MP. getbodypart (I); string dispostion = mpart. getdisposition (); If (dispostion! = NULL) & (dispostion. equals (part. attachment) | dispostion. equals (part. inline) {filename = mpart. getfilename (); If (filename. tolowercase (). indexof ("gb2312 ")! =-1) {filename = mimeutility. decodetext (filename);} SaveFile (filename, mpart. getinputstream ();} else if (mpart. ismimetype ("multipart/*") {saveattchment (mpart);} else {filename = mpart. getfilename (); If (filename! = NULL & (filename. tolowercase (). indexof ("gb2312 ")! =-1) {filename = mimeutility. decodetext (filename);} SaveFile (filename, mpart. getinputstream () ;}}} else if (part. ismimetype ("message/rfc822") {saveattchment (Part) part. getcontent () ;}}/*** get the address for saving the attachment * @ return */Public String getsaveattchpath () {return saveattchpath ;} /*** set the Save attachment address * @ Param saveattchpath */Public void setsaveattchpath (string saveattchpath) {This. saveattchpath = saveatt Chpath;}/*** set the date format * @ Param dateformate */Public void setdateformate (string dateformate) {This. dateformate = dateformate;}/*** save file content * @ Param filename * @ Param inputstream * @ throws ioexception */private void SaveFile (string filename, inputstream) throws ioexception {string osname = system. getproperty ("OS. name "); string storedir = getsaveattchpath (); string sepatror =" "; I F (osname = NULL) {osname = "";} If (osname. tolowercase (). indexof ("win ")! =-1) {sepatror = "//"; if (storedir = NULL | "". equals (storedir) {storedir = "D: // Temp" ;}} else {sepatror = "/"; storedir = "/Temp ";} file storefile = new file (storedir + sepatror + filename); system. out. println ("storefile's path:" + storefile. tostring (); bufferedoutputstream Bos = NULL; bufferedinputstream Bis = NULL; try {Bos = new bufferedoutputstream (New fileoutputstream (storefile); Bis = new buff Eredinputstream (inputstream); int C; while (C = bis. Read ())! =-1) {Bos. write (c); Bos. flush () ;}} catch (filenotfoundexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();} finally {Bos. close (); bis. close () ;}} public void recive (Part part, int I) throws messagingexception, ioexception {system. out. println ("------------------ start -----------------------"); system. out. println ("message" + I + "Subject:" + getsubject (); system. out. println ("message" + I + "from:" + getfrom (); system. out. println ("message" + I + "isnew:" + isnew (); Boolean flag = iscontainattch (part); system. out. println ("message" + I + "iscontainattch:" + flag); system. out. println ("message" + I + "replysign:" + getreplysign (); getmailcontent (part); system. out. println ("message" + I + "content:" + getbodytext (); setsaveattchpath ("C: // temp //" + I); If (FLAG) {saveattchment (part);} system. out. println ("------------------ end -----------------------");}}
Related Article

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.