可以发邮件让公司的电脑重启的小程序

公司的软件很二经常不工作,过年放假几天还被拉回公司重启电脑,没办法作了个小程序放在启动里。需要重启的时候发个邮件给指定邮箱就可以重启电脑了。
标签: <无>

代码片段(2)

[代码] main类

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.NoSuchProviderException;
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 stub
		mainServer ser=new mainServer();
/*		Runtime run=Runtime.getRuntime();
		try {
			run.exec("shutdown -r");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.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 urlname = new URLName("pop3","pop.163.com",110,null,"邮箱地址","邮箱密码");
        Date nowtime;
        while(true)
	    {
	        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 store;
		        bool=false;
				try {
					store = session.getStore(urlname);
			        store.connect();
			        Folder 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,"我还活着呢,发现新邮件"+count+"封!");
			        ReciveMail rm = null;
			        for(int i=0;i"+filedir);
				
		try {
			outfile = new FileOutputStream(filedir,true);		
			bout=new BufferedOutputStream(outfile);
			dout=new DataOutputStream(bout);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.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 block
			e.printStackTrace();
		}
	}
	FileOutputStream outfile;
	BufferedOutputStream bout;
	DataOutputStream dout;
	String filedir;
}

[代码] 网上抄的一段对接受邮件的代码

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 javax.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;
    }
    
    /**
     * 获取发送邮件者信息
     * @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;
    }
    
    /**
     * 获取邮件收件人,抄送,密送的地址和信息。根据所传递的参数不同 "to"-->收件人,"cc"-->抄送人地址,"bcc"-->密送地址
     * @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";
                    mailaddr += ","+compositeto; 
                }
                mailaddr = mailaddr.substring(1);
            }
        }else{
            throw new RuntimeException("Error email Type!");
        }
        return mailaddr;
    }
    
    /**
     * 获取邮件主题
     * @return
     * @throws UnsupportedEncodingException
     * @throws MessagingException
     */
    public String getSubject() throws UnsupportedEncodingException, MessagingException{
        String subject = "";
        subject = MimeUtility.decodeText(msg.getSubject());
        if(subject == null){
            subject = "";
        }
        return subject;
    }
    
    /**
     * 获取邮件发送日期
     * @return
     * @throws MessagingException
     */
    public String getSendDate() throws MessagingException{
        Date sendDate = msg.getSentDate();
        SimpleDateFormat smd = new SimpleDateFormat(dateformate);
        return smd.format(sendDate);
    }
    
    /**
     * 获取邮件正文内容
     * @return
     */
    public String getBodyText(){
        
        return bodytext.toString();
    }
    
    /**
     * 解析邮件,将得到的邮件内容保存到一个stringBuffer对象中,解析邮件 主要根据MimeType的不同执行不同的操作,一步一步的解析
     * @param part
     * @throws MessagingException
     * @throws IOException
     */
    public void getMailContent(Part 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 = (Multipart) part.getContent();
            int count = multipart.getCount();
            for(int i=0;i

你可能感兴趣的:(Java)