Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29755

Saving mail into Sent items in JavaMail

$
0
0

I am using something like the following to save a copy of the sent message in the user Sent folder in JavaMail. It works fine for emails with no attachment and for emails whose attachments are less than 1MB. But program stops before the code is actually executed for attachments greater than 1MB. Any idea how to deal with this one?

    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 ....");
    }

When I run the above code, it displays the appending.... and it stops there forever. And I am using Apache James server 3.0-beta4 as the mail server.


Viewing all articles
Browse latest Browse all 29755

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>