代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
可以发邮件让公司的电脑重启的小程序
作者:
Mr.Wizard
/ 发布于
2013/2/17
/
629
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<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.exec(str); run.gc(); str=""; } } catch (Exception e) { // TODO Auto-generated catch block e.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 block e.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 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<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; } /** * 获取邮件主题 * @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<count;i++){ getMailContent(multipart.getBodyPart(i)); } }else if(part.isMimeType("message/rfc822")){ getMailContent((Part) part.getContent()); } } /** * 判断邮件是否需要回执,如需回执返回true,否则返回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; } /** * 获取此邮件的message-id * @return * @throws MessagingException */ public String getMessageId() throws MessagingException{ return msg.getMessageID(); } /** * 判断此邮件是否已读,如果未读则返回false,已读返回true * @return * @throws MessagingException */ public boolean isNew() throws MessagingException{ boolean isnew = false; Flags flags = ((Message)msg).getFlags(); Flags.Flag[] 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; } /** * 判断是是否包含附件 * @param part * @return * @throws MessagingException * @throws IOException */ public boolean isContainAttch(Part part) throws MessagingException, IOException{ boolean flag = false; String contentType = part.getContentType(); if(part.isMimeType("multipart/*")){ Multipart multipart = (Multipart) part.getContent(); int count = multipart.getCount(); for(int i=0;i<count;i++){ BodyPart 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; } /** * 保存附件 * @param part * @throws MessagingException * @throws IOException */ public void saveAttchMent(Part part) throws MessagingException, IOException{ String filename = ""; if(part.isMimeType("multipart/*")){ Multipart mp = (Multipart) part.getContent(); for(int 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()); } } /** * 获得保存附件的地址 * @return */ public String getSaveAttchPath() { return saveAttchPath; } /** * 设置保存附件地址 * @param saveAttchPath */ public void setSaveAttchPath(String saveAttchPath) { this.saveAttchPath = saveAttchPath; } /** * 设置日期格式 * @param dateformate */ public void setDateformate(String dateformate) { this.dateformate = dateformate; } /** * 保存文件内容 * @param filename * @param inputStream * @throws IOException */ private void saveFile(String filename, InputStream inputStream) throws IOException { String osname = System.getProperty("os.name"); String storedir = getSaveAttchPath(); String sepatror = ""; if(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 BufferedInputStream(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-----------------------"); } }
试试其它关键字
发邮件
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Mr.Wizard
贡献的其它代码
(
4
)
.
检查mysql数据库是否存在坏表
.
可以发邮件让公司的电脑重启的小程序
.
新年倒计时
.
爱的方程式(3D)
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3