简体   繁体   中英

java email attachment being sent as plain text in body

I'm having an issue with sending attachments via email in java using the java mail (1.4.6). It seems that when I attach a document of any sort and send it, the recipient gets the file in plain text format in the body. Rather than, you guessed it, sending the whole file like you would expect and the body not interfered with.

Code

    try 
    {

        // Create a message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(Compose.to));
        message.setSubject(Compose.subject);
        //message.setText(Compose.body);
        //If there are no CC's then skip it. This if seemed to decrease send time.
        if(Compose.cc != null)
        {
            message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(Compose.cc));
            message.saveChanges();
        }
        else
            message.saveChanges();

        /*
         * For adding the attached file to the email. This time the if
         * statement is used to stop the email attachment process if there
         * is none. Other wise due to the way I've set it up it'll try to
         * send file path and file name as null, and we fail an otherwise valid email.
         */

        if(Compose.filename != null)
        {
            String file = Compose.filepath;
            String fileName = Compose.filename;

            Multipart multipart = new MimeMultipart();
            BodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileName);
            multipart.addBodyPart(messageBodyPart);

            BodyPart messageBodyPart2 = new MimeBodyPart();
            messageBodyPart2.setText(Compose.body);
            multipart.addBodyPart(messageBodyPart2);

            message.setContent(multipart);
        }
        else
        {
            message.setText(Compose.body);
            message.saveChanges();
        }

        //Send the message by javax.mail.Transport .            
        Transport tr = session.getTransport("smtp");            // Get Transport object from session        
        tr.connect(smtphost, username, password);               // We need to connect
        tr.sendMessage(message, message.getAllRecipients());    // Send message

        //Notify the user everything functioned fine.
        JOptionPane.showMessageDialog(null, "Your mail has been sent.");

    } 

Thinking over this I remembered how FileDataSource() is an overloaded statement taking a string or a file type as a parameter, trying both I got the same result but I will experiment more with the the file type now.

EDIT: After more testing I noticed that sometimes the file will not appear along with whatever was in the body at the time of sending.

You're setting the attachment as the first body part. It needs to be the second body part.

Also, consider upgrading to JavaMail 1.5.4 and using the MimeBodyPart.attachFile method to attach the file.

For each part you have to set disposition to Part.INLINE for the body and Part.ATTACHMENT for the attachment. The attachFile methods will do that for you. Avoid JavaMail 1.4.6 in favor of the latest release or at least use JavaMail 1.4.7 which contains fixes for known issues with JavaMail 1.4.6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM