Normally this would be unrelated to struts, the problem is that this method
only fails when called from the action class.

I have a form, which uploads a file the action class then saves the file on
the server and attempts to email the file as an attachment. The email
arrives with subject but no message text or file attachment. The file saves
ok on the server.

I thought it may have been a threading/timing issue however if I hard code
files that already exist it still arrives without the message and
attachment.

The method works (and has worked for some time) called from any java class
(non struts type).

Has anyone else succeeded in doing what I am attempting?
Can anyone see an obvious bug I am missing?

Code snippet:

public static void sendMessage(String from, String  to, String subject,
String message, String fileName) {

        String smtpHost = "smtp-server";
        Properties props = System.getProperties();
        props.put("mail.smtp.host", smtpHost);

        // Get a session
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(false);

        try {
            // Define message
            MimeMessage mailMsg = new MimeMessage(session);
            mailMsg.setFrom(new InternetAddress(from));
            mailMsg.addRecipient(Message.RecipientType.TO, new
InternetAddress(to));
            mailMsg.setSubject(subject);
            // mailMsg.setSentDate(new java.util.Date());

            if (fileName == null) mailMsg.setText(message);
            else {
                // Create the message part
                BodyPart messageBodyPart = new MimeBodyPart();
                messageBodyPart.setText(message);

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

                // create the second message part � the attachment
                messageBodyPart = new MimeBodyPart();

                DataSource source = new FileDataSource(file);
                messageBodyPart.setDataHandler(new DataHandler(source));
                // attach the file to the message
                messageBodyPart.setFileName(source.getName());
                multipart.addBodyPart(messageBodyPart);

                // Put parts in message
                mailMsg.setContent(multipart);
            }

            // Send message
            Transport.send(mailMsg);

            catch�

TIA

Joe

Reply via email to