繁体   English   中英

Java电子邮件附件在正文中作为纯文本发送

[英]java email attachment being sent as plain text in body

我在使用Java邮件(1.4.6)通过Java通过电子邮件发送附件时遇到问题。 看来,当我附加任何类型的文档并将其发送时,收件人将在正文中获取纯文本格式的文件。 并非您猜到了,而是像您期望的那样发送了整个文件,并且身体没有受到干扰。

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

    } 

考虑这一点,我记得FileDataSource()是一个重载的语句,将字符串或文件类型作为参数,尝试两者都得到相同的结果,但是现在我将对文件类型进行更多的实验。

编辑:经过更多测试后,我注意到有时文件不会与发送时正文中的任何内容一起出现。

您将附件设置为主体的第一部分。 它必须是第二部分。

另外,请考虑升级到JavaMail 1.5.4,并使用MimeBodyPart.attachFile方法附加文件。

对于每个零件,您必须将主体的处置设置Part.INLINE ,并将附件的处置设置Part.ATTACHMENT attachFile方法将为您完成此操作。 避免使用JavaMail 1.4.6来支持最新版本,或者至少使用JavaMail 1.4.7,其中包含针对JavaMail 1.4.6的已知问题的修复程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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