4

我正在使用类似以下的内容将已发送消息的副本保存在 JavaMail 的用户已发送文件夹中。它适用于没有附件的电子邮件和附件小于 1MB 的电子邮件。但是对于大于 1MB 的附件,程序在实际执行代码之前就停止了。知道如何处理这个吗?

    String host = ReadConfigPropertiesFile.getPropertyValue("server.host");
        String smtpHost = ReadConfigPropertiesFile.getPropertyValue("smtp.host");
        String from = "test@myserver.net";
        String to = "test@myserver.net";
// Get system properties
        Properties properties = System.getProperties();
// Setup smtp mail server
        properties.setProperty(smtpHost, host);
        properties.setProperty("mail.mime.encodeparameters", "true");
        //  properties.setProperty("mail.mime.decodeparameters","true");
        properties.setProperty("mail.mime.encodefilename", "true");
        // properties.setProperty("mail.mime.decodefilename","true");
// Get the default Session object.
        Session session = Session.getDefaultInstance(properties);
// Define message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO,
                new InternetAddress(to));

        message.setSubject("Peace ", "UTF-8");
// Create the message part
        BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message body
        messageBodyPart.setContent("Hello attachment", "text/html; charset=UTF-8");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        String[] filename = {"C:/Users/Dake/Desktop/music.mp3"};
        for (int i = 0; i < filename.length; i++) {
            // Part two is attachment
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(filename[i]);
            messageBodyPart.setDataHandler(new DataHandler(source));
            // messageBodyPart.setFileName(filename[i]);
            //messageBodyPart.setHeader("Content-Type", "text/plain; charset=UTF-8; name="+MimeUtility.encodeText(filename[i]));
            // messageBodyPart.setHeader("Content-ID", filename[i]);
            messageBodyPart.setFileName(MimeUtility.encodeText(filename[i], "UTF-8", null)); //encode filename
            //bodyPart.setFileName(MimeUtility.encodeText(attachment.getName(), "UTF-8", null));
            multipart.addBodyPart(messageBodyPart);
        }
        // Put parts in message
        message.setContent(multipart);
        //set the time
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:MM:SS");
        Date date = new Date();
        String sentDate = df.format(date);
        Date dd = (Date) df.parse(sentDate);
        message.setSentDate(date);

        // Send the message
        Transport.send(message);
        System.out.println("Message sent...");
        // Copy message to "Sent Items" folder as read
        Store store = session.getStore("imap");
        store.connect(host, "user", "userpwd");

        Folder folder = (Folder) store.getFolder("Sent");
        if (!folder.exists()) {
            folder.create(Folder.HOLDS_MESSAGES);
        }
        folder.open(Folder.READ_WRITE);
        System.out.println("appending...");
        // folder.appendMessages(new Message[]{message});
        try {

            folder.appendMessages(new Message[]{message});
           // Message[] msgs = folder.getMessages();
            message.setFlag(FLAGS.Flag.RECENT, true);

        } catch (Exception ignore) {
            System.out.println("error processing message " + ignore.getMessage());
        } finally {
            store.close();
           // folder.close(false);
        }

        System.out.println("Msg send and saved ....");
    }

当我运行上面的代码时,它会显示附加......并且它永远停在那里。我使用 Apache James server 3.0-beta4 作为邮件服务器。

4

2 回答 2

0

您的服务器是否因为发送要附加的消息花费的时间太长而中断连接?(如果有,需要多长时间?)

还是服务器中断连接是因为它不允许您附加那么大的消息?

您是否从调试输出中的服务器获得任何有用的信息?

于 2013-08-21T03:20:23.620 回答
0

我知道这是一个老问题,但也许这对你或其他人有帮助。我猜你的代码的问题是你试图操纵Recent-Flag: message.setFlag(FLAGS.Flag.RECENT, true);。根据IMAP 标准,客户端不能更改最近标记。

于 2016-03-14T08:45:47.983 回答