I am attempting to send a file (that has been uploaded with the file tag) as
an attachment using JavaMail. I am using some very standard code that I have
used before many times, the only difference is now I am using Struts and it
is not working.

Is anyone else successfully sending files as email attachments?

Is there some sort of conflict with the Multipart Object (I know I�m
clutching at straws, but this is incredibly frustrating).

Code excerpt:
�
try {
    MimeMessage message = new MimeMessage(session);
    // From
    message.setFrom( new InternetAddress(from));
    InternetAddress[] tos = InternetAddress.parse(to);
    // To
    message.setRecipients(Message.RecipientType.TO,tos);
    // Subject
    message.setSubject(subject);
    // Date
    message.setSentDate(new Date());
    if (fileName == null) message.setText(bodyText);
    else {
            // The body
            MimeBodyPart body = new MimeBodyPart();
            body.setContent(bodyText, "text/html");
            // The attachment
            MimeBodyPart attachment = new MimeBodyPart();
            FileDataSource source = new FileDataSource(fileName);
            attachment.setDataHandler(new DataHandler(source));
            attachment.setFileName(source.getName());

            // The Multipart to which body and attachment are added
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(body);
            multipart.addBodyPart(attachment);

            // DEBUG print out of the multipart
            // This will print out the message body and file contents...
            multipart.writeTo(System.out);

            // The MimeMessage has the multipart added
            message.setContent(multipart);
    }

    Transport.send(message);
�

TIA
Joe

Reply via email to