使用JavaMail发送附件需要很长时间

时间:2016-07-14 18:53:43

标签: java

我想将attachemnd(pdf)发送到使用JavaMail的电子邮件地址。但是sendig需要大约5分钟...当然太长了。该文件只有700kB大。那么我能做些什么来解决这个问题呢?以下是我的代码。 THX

主要部分......

 ByteArrayDataSource ds = null;
    Message message = new MimeMessage(session);
    try {
        byte[] bytearray;
        InputStream in = new BufferedInputStream(new FileInputStream("D:/Serviceantrag_Kopie_befuellt1.pdf"));
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        byte[] b = new byte[4096];
        int length = 0;
        try {
            while ((length = in.read(b)) != -1) {
                bao.write(b, 0, length);
            }
            bao.close();
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        bytearray = bao.toByteArray();

        try {
            ds = new ByteArrayDataSource(bytearray, "application/pdf");
        } catch (Exception e) {

            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Set From: header field of the header.
    message.setFrom(new InternetAddress(USERNAME));

    // Set To: header field of the header.
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(USERNAME));
    message.setHeader("Content-Type", "application/pdf;charset=UTF-8");
    // Set Subject: header field
    message.setSubject("Testing Subject");

    // Create the message part
    BodyPart messageBodyPart = new MimeBodyPart();

    // Now set the actual message
    messageBodyPart.setText("This is message body");

    // Create a multipar message
    Multipart multipart = new MimeMultipart();

    // Set text message part
    multipart.addBodyPart(messageBodyPart);

    // Part two is attachment
    messageBodyPart = new MimeBodyPart();
    String filename = "D:/Serviceantrag_Kopie.pdf";

    messageBodyPart.setDataHandler(new DataHandler(ds));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);

    // Send the complete message parts
    message.setContent(multipart);

    // Send message
    Transport.send(message);

    System.out.println("Sent message successfully....");

修改

我已经在控制台中拍摄了发送信息。看看时间......

enter image description here

0 个答案:

没有答案