邮件系统部分内容(不完整)

来源:互联网 发布:网络打印机怎么连接 编辑:程序博客网 时间:2024/06/05 18:22
package com.ais.bean;import java.text.SimpleDateFormat;import javax.mail.Address;import javax.mail.BodyPart;import javax.mail.Flags.Flag;import javax.mail.Message;import javax.mail.Multipart;import javax.mail.Part;import javax.mail.internet.ContentType;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMultipart;import javax.mail.internet.MimeUtility;public class MailBean {    private String subject;    private String from;    private String to;    private String cc;    private String bcc;    private String date;    private int size;    private String text;    private int seen;    private int hasAttach = 0;    private String attach;    private long uid;    public MailBean() {    }    public MailBean(Message msg,int flag){                if(msg!=null){            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm ");            try {                date = dateFormat.format(msg.getSentDate()!=null?msg.getSentDate():msg.getReceivedDate());                subject = msg.getSubject();                size = msg.getSize();                if (msg.isSet(Flag.SEEN)) {                    seen = 1;                } else {                    seen = 0;                }                Object content = msg.getContent();                ContentType cType = new ContentType(msg.getContentType());//                String contentType = msg.getContentType();                if(cType.match("text/*")){                    text=content.toString();                }else if(cType.match("multipart/*")){                    Multipart multipart = (Multipart)content;                    StringBuffer sb = new StringBuffer();                    StringBuffer sb1 = new StringBuffer();                    for(int i=0;i<multipart.getCount();i++){                        BodyPart bodyPart = multipart.getBodyPart(i);                        String disposition = bodyPart.getDisposition();                                                if ((disposition != null)&& ((disposition.equals(Part.ATTACHMENT)) || (disposition.equals(Part.INLINE)))) {                            hasAttach = 1;                            String fileName = bodyPart.getFileName();                                if (fileName.toLowerCase().indexOf("gb2312") != -1) {                                fileName = MimeUtility.decodeText(fileName);                            }else{                                fileName=new String(fileName.getBytes("ISO-8859-1"),"gb2312");                                }                            String attach1 = "<a href='../download?pid=" + i + "&filename=" + fileName + "'>" + fileName + "</a>&nbsp;";                            sb1.append(attach1);                        } else {                            /*Scanner in = new Scanner(bodyPart.getInputStream());                            while(in.hasNextLine()){                                sb.append(in.nextLine());                            }*/                            MimeMultipart mmp = (MimeMultipart)bodyPart.getContent();                            ContentType ccType = new ContentType(mmp.getContentType());                            System.out.println(ccType);                            if(ccType.match("text/*")){                                System.out.println(content.toString());                            }                        }                    }                        text = sb.toString();                    attach = sb1.toString();                }                from = convertAddress(msg.getFrom());                to = convertAddress(msg.getRecipients(Message.RecipientType.TO));                cc = convertAddress(msg.getRecipients(Message.RecipientType.CC));                bcc = convertAddress(msg.getRecipients(Message.RecipientType.BCC));            } catch (Exception e) {                e.printStackTrace();            }        }    }    private String convertAddress(Address[] addr){        if(addr==null)            return "";        String addressStr="";        boolean tf = true;        for(int i=0;i<addr.length;i++){            addressStr = addressStr+((tf)? " " : ",")+((InternetAddress)addr[i]).getAddress();            tf = false;        }        return addressStr;    }    public long getUid() {        return uid;    }    public void setUid(long uid) {        this.uid = uid;    }    public String getSubject() {        return subject;    }    public void setSubject(String subject) {        this.subject = subject;    }    public String getFrom() {        return from;    }    public void setFrom(String from) {        this.from = from;    }    public String getTo() {        return to;    }    public void setTo(String to) {        this.to = to;    }    public String getCc() {        return cc;    }    public void setCc(String cc) {        this.cc = cc;    }    public String getBcc() {        return bcc;    }    public void setBcc(String bcc) {        this.bcc = bcc;    }    public String getDate() {        return date;    }    public void setDate(String date) {        this.date = date;    }    public int getSize() {        return size;    }    public void setSize(int size) {        this.size = size;    }    public String getText() {        return text;    }    public void setText(String text) {        this.text = text;    }    public int getSeen() {        return seen;    }    public void setSeen(int seen) {        this.seen = seen;    }    public int getHasAttach() {        return hasAttach;    }    public void setHasAttach(int hasAttach) {        this.hasAttach = hasAttach;    }    public String getAttach() {        return attach;    }    public void setAttach(String attach) {        this.attach = attach;    }}

下载邮件附件的代码

package com.ais.servlet;import java.io.IOException;import java.io.InputStream;import javax.mail.Message;import javax.mail.Multipart;import javax.mail.internet.MimeBodyPart;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;@SuppressWarnings("serial")public class DownloadServlet extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        resp.setContentType("application/octet-stream;charset=GBK");        req.setCharacterEncoding("GBK");        HttpSession session=req.getSession();        ServletOutputStream out=resp.getOutputStream();        int bodyId=Integer.parseInt(req.getParameter("pid"));        String fileName=req.getParameter("filename");        Message message=(Message)session.getAttribute("message");        try{            resp.setHeader("Content-Disposition","attachment;filename="+fileName);            Multipart multi=(Multipart)message.getContent();            MimeBodyPart bodyPart=(MimeBodyPart)multi.getBodyPart(bodyId);            InputStream is=bodyPart.getInputStream();                int c=0;            while((c=is.read())!=-1){                out.write(c);            }        }catch(Exception e){            e.printStackTrace();        }        out.flush();        out.close();    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        // TODO Auto-generated method stub        doGet(req, resp);    }}

连接邮箱的代码

public static boolean connect(String password) {    String host = "imap.qq.com";    String username = "121399522@qq.com";    Properties prop = new Properties();            prop.setProperty("mail.store.protocol", "imap");        prop.setProperty("mail.imap.host", host);                prop.setProperty("mail.imap.class", "com.sun.mail.imap.IMAPStore");    prop.setProperty("mail.transport.protocol", "smtp");    Session session = Session.getDefaultInstance(prop, null);        session.setDebug(false);        Store store = null;    try {        store = session.getStore("imap");        store.connect(host, username, password);    } catch (NoSuchProviderException e) {//            e.printStackTrace();    } catch (MessagingException e) {//            e.printStackTrace();    }    return store.isConnected();}