1

嗨,我是 JavaMail 的新手。

我想上传一个文件作为附件。它只存储文件名。

文件成功地附加了文件名,而不是该文件的内容。

如何解决它。提前致谢...

我尝试了以下代码:

Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromAddress));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(toAddr));

        if (subject == null) {
            message.setSubject("");
        } else {
            message.setSubject(subject);
        }

        if (attachedFile == null || attachedFile.equalsIgnoreCase("")) {
            if (content == null) {
                message.setText("");
            } else {
                message.setText(content);
            }

        } else {
            // Create the message part
            BodyPart messageBodyPart = new MimeBodyPart();

            if (content == null) {
                messageBodyPart.setText("");
            } else {
                messageBodyPart.setText(content);
            }

            // Create a multipar message
            Multipart multipart = new MimeMultipart();

            // Set text message part
            multipart.addBodyPart(messageBodyPart);

            // Part two is attachment
            // messageBodyPart = new MimeBodyPart();
            DataSource source = new ByteArrayDataSource(
                    attachedFile.getBytes("UTF-8"),
                    "application/octet-stream");
            messageBodyPart = new MimeBodyPart();

            // DataSource source = new FileDataSource(attchedFile);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(attachedFile);
            // messageBodyPart.setHeader("Content-ID", "image/*");
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
        }

        Transport.send(message);
4

2 回答 2

8
    MimeBodyPart messageBodyPart = new MimeBodyPart();

    messageBodyPart.setContent(body, "text/plain");

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

    // adds attachments


            MimeBodyPart attachPart = new MimeBodyPart();


            File file=new File("Z:/filename");

            try {

                attachPart.attachFile(file);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            multipart.addBodyPart(attachPart);

    // sets the multi-part as e-mail's content
    message.setContent(multipart);

might be this will work

于 2014-12-04T12:11:42.040 回答
2
 MimeMessage message = new MimeMessage(session);

 // create the message 
 MimeBodyPart messageBodyPart = new MimeBodyPart();

 //fill message to be sent
 messageBodyPart.setText("Hi hello try and send this message");

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

 //attach your file here
 messageBodyPart = new MimeBodyPart();
 DataSource source =  new FileDataSource(fileAttachment); // your file
 messageBodyPart.setDataHandler( new DataHandler(source));
 messageBodyPart.setFileName(fileAttachment);
 multipart.addBodyPart(messageBodyPart);

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


 Transport.send( message );
于 2012-09-26T06:32:48.087 回答