将邮件保存到JavaMail中的已发送邮件中

时间:2013-08-20 06:25:19

标签: email javamail attachment

我使用类似下面的内容将已发送邮件的副本保存在JavaMail的用户Sent文件夹中。它适用于没有附件的电子邮件和附件小于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服务器3.0-beta4作为邮件服务器。

2 个答案:

答案 0 :(得分:0)

您的服务器是否破坏了连接,因为发送要附加的消息需要很长时间? (如果是这样,需要多长时间?)

或者服务器是否破坏连接,因为它不允许您附加大的消息?

您是否从debug output中的服务器获得了有用的信息?

答案 1 :(得分:0)

我知道这是一个老问题,但也许这有助于你或其他人。 我猜你的代码问题是你试图操纵Recent-Flag:message.setFlag(FLAGS.Flag.RECENT, true);。根据{{​​3}},最近的旗帜不能被客户改变。