⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mail.java

📁 JSP常用模块源代码之用户管理模块的全部源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public String[] getAttachMentNames(Part part)throws Exception{ 
        String fileName = ""; 
        StringBuffer fileNames = new StringBuffer();
        if(part.isMimeType("multipart/*")){
            Multipart mp = (Multipart)part.getContent(); 
            for(int i=0;i<mp.getCount();i++){ 
                BodyPart mpart = mp.getBodyPart(i); 
                String disposition = mpart.getDisposition(); 
                if((disposition != null) &&((disposition.equals(Part.ATTACHMENT)) ||(disposition.equals(Part.INLINE)))){ 
                    fileName = mpart.getFileName(); 
                    if(fileName.toLowerCase().indexOf("gb2312") != -1) 
                        fileName = MimeUtility.decodeText(fileName);
                    if (fileName!=null)
                    	fileNames.append(fileName).append(",");
                }else if(mpart.isMimeType("multipart/*")){ 
                    saveAttachMent(mpart); 
                }else{ 
                    fileName = mpart.getFileName(); 
                    if((fileName != null) && (fileName.toLowerCase().indexOf("GB2312") != -1)) 
                        fileName=MimeUtility.decodeText(fileName);
                    if (fileName!=null)
                    	fileNames.append(fileName).append(",");
                } 
            } 
        }else if(part.isMimeType("message/rfc822")){ 
            saveAttachMent((Part)part.getContent()); 
        }
        return fileNames.toString().split(",");
    }     
    /** 
     * 【设置附件存放路径】 
     */ 
    public void setAttachPath(String attachpath){ 
        this.saveAttachPath = attachpath; 
    }      
    /** 
     * 【设置日期显示格式】 
     */ 
    public void setDateFormat(String format)throws Exception{ 
        this.dateformat = format; 
    }      
    /** 
     * 【获得附件存放路径】 
     */ 
    public String getAttachPath(){ 
        return saveAttachPath; 
    }      
    /** 
     * 【真正的保存附件到指定目录里】 
     */ 
    private void saveFile(String fileName,InputStream in)throws Exception{ 
        String osName = System.getProperty("os.name"); 
        String storedir = getAttachPath(); 
        String separator = ""; 
        if(osName == null) osName=""; 
        if(osName.toLowerCase().indexOf("win") != -1){ 
            separator = "\\"; 
            if(storedir == null || storedir.equals("")) storedir="c:\\tmp"; 
        }else{ 
            separator = "/"; 
            storedir = "/tmp"; 
        } 
        File storefile = new File(storedir+separator+fileName); 
        System.out.println("附件保存到: "+storefile.toString()); 
        BufferedOutputStream bos = null; 
        BufferedInputStream  bis = null; 
        try{ 
            bos = new BufferedOutputStream(new FileOutputStream(storefile)); 
            bis = new BufferedInputStream(in); 
            int c; 
            while((c=bis.read()) != -1){ 
                bos.write(c); 
                bos.flush(); 
            } 
        }catch(Exception exception){ 
            exception.printStackTrace(); 
            throw new Exception("附件保存失败!"); 
        }finally{ 
            bos.close(); 
            bis.close(); 
        }
    }    
    /** 
     * 删除邮件 
     */ 
    public void delMail()throws Exception{ 
    	mimeMessage.setFlag(Flags.Flag.DELETED,true);
    }     
    /** 
     * 转发邮件 
     */ 
    public void fwdMail(Properties props)throws Exception{
    	Session session2=Session.getInstance(props);
    	Message forward = new MimeMessage(session2);
    	forward.setSubject("Fwd: " + mimeMessage.getSubject());
    	forward.setFrom(new InternetAddress(props.getProperty("mail.smtp.from")));
    	forward.addRecipient(Message.RecipientType.TO,new InternetAddress(props.getProperty("mail.smtp.to")));
    	forward.addRecipient(Message.RecipientType.CC,new InternetAddress(props.getProperty("mail.smtp.cc")));
    	forward.addRecipient(Message.RecipientType.BCC,new InternetAddress(props.getProperty("mail.smtp.bcc")));
    	BodyPart messageBodyPart = new MimeBodyPart();
    	messageBodyPart.setText("转发邮件");
    	Multipart multipart = new MimeMultipart();
    	multipart.addBodyPart(messageBodyPart);
    	messageBodyPart.setDataHandler(mimeMessage.getDataHandler());
    	multipart.addBodyPart(messageBodyPart);
    	forward.setContent(multipart);
    	Transport transport=session2.getTransport("smtp");
    	transport.connect(props.getProperty("mail.smtp.host"),props.getProperty("mail.smtp.user"),props.getProperty("mail.smtp.password"));
    	transport.sendMessage(forward,forward.getAllRecipients());
    	transport.close();
    }
		//  开始发送信件的方法
		public boolean startSend(Properties props) {
		Session session = Session.getDefaultInstance(props);
		try {
			//    创建一个消息,并初始化该消息的各项元素
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(props.getProperty("mail.smtp.from")));
			InternetAddress[] address = { new InternetAddress(props.getProperty("mail.smtp.to"))};
			msg.setRecipients(Message.RecipientType.TO, address);
			msg.setSubject(props.getProperty("mail.smtp.subject"));
			//    后面的BodyPart将加入到此处创建的Multipart中
			Multipart mp = new MimeMultipart();
			boolean isMulti=false;
			if (null != props.getProperty("mail.smtp.files") && props.getProperty("mail.smtp.files").length()>0){
				String[] files = props.getProperty("mail.smtp.files").split(",");
				for (int i=files.length-1;i>=0;i--) {
					MimeBodyPart mbp = new MimeBodyPart();
					//    选择出每一个附件名
					String filename = files[i].toString();
					filename.substring(0,filename.length()-1);
					//    得到数据源
					FileDataSource fds = new FileDataSource(filename);
					//    得到附件本身并至入BodyPart
					mbp.setDataHandler(new DataHandler(fds));
					//    得到文件名同样至入BodyPart
					mbp.setFileName(fds.getName());
					mp.addBodyPart(mbp);
				}
				isMulti=true;
			}
			String strContent=props.getProperty("mail.smtp.text");
			if ("1".equals(props.getProperty("mail.smtp.ishtml"))){
				BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
				mdp.setContent(strContent,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式的编码方式
				mp.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
				isMulti=true;
			}
			else
				msg.setText(strContent);
			if (isMulti)
				msg.setContent(mp);
			
			//    设置信件头的发送日期
			msg.setSentDate(new Date());
			//    发送信件
	    	Transport transport=session.getTransport("smtp");
	    	transport.connect(props.getProperty("mail.smtp.host"),props.getProperty("mail.smtp.user"),props.getProperty("mail.smtp.password"));
	    	transport.sendMessage(msg,msg.getAllRecipients());
		} catch (MessagingException mex) {
			mex.printStackTrace();
			Exception ex = null;
			if ((ex = mex.getNextException()) != null) {
				ex.printStackTrace();
			}
			return false;
		}
		return true;
	}
    public static void main(String args[])throws Exception{ 
    	Properties props = new Properties();
    	props.put("mail.smtp.from","tougao-email@126.com");
    	props.put("mail.smtp.to","myte@163.com");
    	props.put("mail.smtp.subject","aaaaaaaaaaaaaaaa");
    	props.put("mail.smtp.files","");
//    	props.put("mail.smtp.files","c:\\fs.sql,c:\\log4j.log");
    	props.put("mail.smtp.ishtml","1");
    	props.put("mail.smtp.text","dafadsfas<font color=red>asfa</font>dfasd");
    	props.put("mail.smtp.host","smtp.126.com");
    	props.put("mail.smtp.auth","true");
    	props.put("mail.smtp.user","tougao-email");
    	props.put("mail.smtp.password","8807698");
    	Mail pmm = new Mail();
    	pmm.startSend(props);
    }
//        String host = "61.187.87.90";     //邮件服务器IP地址
//        String username ="zb@hncccb.com";     //登陆用户名
//        String password ="right";       //登陆密码
//
//        Properties props = new Properties(); 
//        Session session = Session.getDefaultInstance(props, null); 
//        Store store = session.getStore("pop3"); 
////        Store store = session.getStore("imap"); 
//        store.connect(host, username, password);
//
//        Folder folder = store.getFolder("INBOX"); 
//        folder.open(Folder.READ_ONLY); 
//        Message message[] = folder.getMessages(); 
//        System.out.println("Messages's length: "+message.length); 
//        Mail pmm = null; 
//        for(int i=0;i<message.length;i++){ 
//            pmm = new Mail((MimeMessage)message[i]); 
//            System.out.println("Message "+i+" subject: "+pmm.getSubject()); 
//            System.out.println("Message "+i+" replysign: "+pmm.getReplySign()); 
//            System.out.println("Message "+i+" hasRead: "+pmm.isNew()); 
//            System.out.println("Message "+i+" containAttachment: "+pmm.isContainAttach((Part)message[i])); 
//            System.out.println("Message "+i+" form: "+pmm.getFrom()); 
//            System.out.println("Message "+i+" to: "+pmm.getMailAddress("to")); 
//            System.out.println("Message "+i+" cc: "+pmm.getMailAddress("cc")); 
//            System.out.println("Message "+i+" bcc: "+pmm.getMailAddress("bcc")); 
//            pmm.setDateFormat("yy年MM月dd日 HH:mm"); 
//            System.out.println("Message "+i+" sentdate: "+pmm.getSentDate()); 
//            System.out.println("Message "+i+" Message-ID: "+pmm.getMessageId()); 
////            pmm.getMailContent((Part)message[i]); 
////            System.out.println("Message "+i+" bodycontent: \r\n"+pmm.getBodyText()); 
////            pmm.setAttachPath("c:\\Downloads"); 
////            pmm.saveAttachMent((Part)message[i]); 
//        } 
//    } 

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -