加载javamail时延迟

时间:2017-06-09 17:33:32

标签: java jsp javamail

使用javamail读取邮件,加载时间对于这么少的邮件就足够了。

<%@ page import="javax.activation.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.Part" %>
<%@ page import="javax.mail.search.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="java.text.SimpleDateFormat" %>

<%!
// Smtp authentication to connect to mail

class SMTPAuthentication extends javax.mail.Authenticator {
    public PasswordAuthentication getPasswordAuthentication() {
        String username = "correo";
        String password = "password"; 
        return new PasswordAuthentication(username, password);
 }
}
%>

<%

Connection  conn = null;
Statement   stmt = null;
ResultSet   rset = null;

Context ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:jdbc/desarrollo");
conn = ds.getConnection();

stmt=conn.createStatement();
String COMANDO  = "";

String direccion = request.getRealPath("/");
String s_id_entidad = request.getParameter("f_id_entidad");
String s_correo = "";
String s_nombre = "";



        s_correo = ""; 
        s_nombre = "name" ;


//connection from smtp.office365.com 

try{


    String host = "smtp.office365.com";
    String from = " ";

    Properties prop = new Properties();

    prop.put("mail.host", host);
    prop.put("mail.port", "587"); ///465 : 


    prop.put("mail.transport.protocol", "imaps");  
    prop.put("mail.imap.starttls.enable", "true");  

    SMTPAuthentication auth = new SMTPAuthentication();

    Session sessionmail = Session.getInstance(prop, auth);
    sessionmail.setDebug(false);
    //conection imap
    Store store = sessionmail.getStore("imaps");
    store.connect(host,"correo","password");
    Folder folder = store.getFolder("INBOX");   
    folder.open(Folder.READ_ONLY);



    Message[] mensajes = folder.getMessages();

    SearchTerm correo = new FromTerm(new InternetAddress(s_correo));


    SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd/MM/yyyy");

    //filter days 
    Date fec = new Date();
    Calendar calendario = new GregorianCalendar();
    calendario.add(Calendar.DATE, -1);
    fec = calendario.getTime();
    ReceivedDateTerm dateTerm = new ReceivedDateTerm(ComparisonTerm.GT,fec);

    Message [] mensajes2=mensajes;

//code html 

%>
<html>
<head>

<script type="text/javascript">
    var x;
    x = (document);
    x.ready(inicializarEvento);

    function inicializarEvento(){
        ('a[rel*=facebox]').facebox({modal: true,overlay: false});
    }

    function cerrar(){
        ('#reporte').load('listado2.jsp', {f_id_entidad:'<%=s_id_entidad%>'});
        .facebox.close();
    }
</script>
</head>
<body>
<table align="center" border="1" width="600">
    <tr>
        <td colspan="6" class="titulo">Lista de Correos <%=s_nombre%></td>
    </tr>
    <tr>
        <th>#</th>
        <th width="15%">From</th>
        <th width="40%">Subject</th>
        <th width="20%">Fecha</th>
        <th width="15%">Adjuntos</th>
        <th></th>
    </tr>
    <%
    int i=0, j=1;
    out.print(mensajes2.length);
    for (i=mensajes2.length-1;i>0;i--){

        fec = mensajes2[i].getSentDate();
        //System.out.println(formatoDelTexto.format(fec));
        %>
    <tr <%if(j%2==1)out.print("bgcolor='#E1DFFF'");%> >
        <td align="center"><%=j%>&nbsp;</td>
        <td><font color="#666666"><%=mensajes2[i].getFrom()[0].toString()%></font>&nbsp;</td>
        <td><%=mensajes2[i].getSubject()%>&nbsp;</td>
        <td><%=formatoDelTexto.format(mensajes2[i].getSentDate())%>&nbsp;</td>
        <td>        
        <%
            Object o = mensajes2[i].getContent();       
            //out.print(o);
            //out.print(mensajes2[i].getLineCount());

            if (o instanceof Multipart) {
            Multipart mp = (Multipart)o;
                int numPart = mp.getCount();
                //out.print(numPart);
                for (int ii=0; ii < numPart; ii++) {
                  Part part = mp.getBodyPart(ii);
                  //out.print(part);
                  String disposition = part.getDisposition();
                  //out.print(part.getDisposition());

                    if (disposition == null) {
                        //out.print("--"+part.getContentType());
                        //out.print("--"+part.isMimeType("multipart/alternative"));
                        //out.print("++"+part.isMimeType("text/plain"));
                        if (part.isMimeType("multipart/alternative")||part.isMimeType("text/plain")){
                            String cuerpoMensaje; 
                            if (part.isMimeType("multipart/alternative")) {
                            Multipart mp2 = (Multipart) part.getContent();
                               //Part part2    = mp2.getBodyPart(0);
                               //cuerpoMensaje = (String)part2.getContent();

                            }else {
                                 cuerpoMensaje = (String)part.getContent();

                            }
                        }else{
                            if(part.getContentType().indexOf("text/html") != -1) {
                                //out.print("holaa");
                                MimeBodyPart mbp = (MimeBodyPart)part;
                                mbp.saveFile(direccion+"cgh_garantia/util2/temp/temp"+ii+".html");
                            }else 
                                if(part.getContentType().indexOf("text/plain") == -1) {
                                    MimeBodyPart mbp = (MimeBodyPart)part;
                                    if(mbp.getFileName()!=null){
                                        Format formatter = new SimpleDateFormat("dd.MM.yyyy.HH.mm.ss");
                                        String s = formatter.format(mensajes2[i].getSentDate());
                                        %>
                                    <strong><a href="vercuotas.jsp?f_archivo=<%=mbp.getFileName()%>&f_id_entidad=<%=s_id_entidad%>&f_fecha_send=<%=s%>"><%=mbp.getFileName()%></a></strong>
                                    <%
                                    mbp.saveFile(direccion+"cgh_garantia/util2/temp/"+mbp.getFileName());
                                }
                            }                           
                        }
                    }else{
                        if(part.getContentType().indexOf("text/plain") == -1) {
                            MimeBodyPart mbp = (MimeBodyPart)part;
                            if(mbp.getFileName()!=null){
                                Format formatter = new SimpleDateFormat("dd.MM.yyyy.HH.mm.ss");
                                String s = formatter.format(mensajes2[i].getSentDate());
                                %>
                            <strong><a href="vercuotas.jsp?f_archivo=<%=mbp.getFileName()%>&f_id_entidad=<%=s_id_entidad%>&f_fecha_send=<%=s%>"><%=mbp.getFileName()%></a></strong>
                            <%
                            mbp.saveFile(direccion+"cgh_garantia/util2/temp/"+mbp.getFileName());
                            }
                        }
                    }
                }
            }
        %>
        </td>
        <td></td>
    </tr>

    <%

    j++;
} // cierra bucle

    %>
    <tr align="center">
        <td colspan="6"><input type="button" name="" value="Cerrar" onClick="cerrar();"/> </td>
    </tr>
</table>

</body>
</html>
<%
    folder.close(false);
    store.close();
}catch(Exception e){
    System.out.println(e); 
}
%>
//cierra db include rset close,stmt close and conn close
<%@ include file= "../../cierradb.jsp" %> 

0 个答案:

没有答案