简体   繁体   English

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

[英]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). 我在使用Java邮件(1.4.6)通过Java通过电子邮件发送附件时遇到问题。 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. 考虑这一点,我记得FileDataSource()是一个重载的语句,将字符串或文件类型作为参数,尝试两者都得到相同的结果,但是现在我将对文件类型进行更多的实验。

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. 另外,请考虑升级到JavaMail 1.5.4,并使用MimeBodyPart.attachFile方法附加文件。

For each part you have to set disposition to Part.INLINE for the body and Part.ATTACHMENT for the attachment. 对于每个零件,您必须将主体的处置设置Part.INLINE ,并将附件的处置设置Part.ATTACHMENT The attachFile methods will do that for you. attachFile方法将为您完成此操作。 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. 避免使用JavaMail 1.4.6来支持最新版本,或者至少使用JavaMail 1.4.7,其中包含针对JavaMail 1.4.6的已知问题的修复程序。

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

相关问题 通过JavaMail发送的纯文本电子邮件作为附件到达 - Plain text email sent via JavaMail arrives as an attachment 我尝试在MimeMessage的Java方法中发送带有主体内容附件的电子邮件时,主体内容未发送 - Body content is not being sent while I am trying to send an email with attachment with main body content in Java methods of MimeMessage Android:HTML附件未与电子邮件一起发送 - Android: HTML attachment not being sent with an email 密码以纯文本格式发送到服务器java spring - Password sent in plain text to server java spring 带有附件的Javamail电子邮件:未发送文本 - Javamail email with attatchment: Text not being sent Java Email API电子邮件未正确发送 - Java Email API emails not being sent properly 作为电子邮件附件发送的PDF文件已损坏(java) - PDF file sent as email attachment becomes corrupted (java) CSV文本电子邮件附件中的Java换行符/换行符破坏了完整的附件 - Java linebreaks/newlines in CSV text email attachment breaks the complete attachment Java邮件:以.htm文件附件以及附件和正文的形式接收电子邮件正文 - Java mail : receiving email body as .htm file attachment along with attachment and body 在Java中以文本/纯文本形式发送多部分消息,而不是Java中的多部分/替代 - Multipart message sent as text/plain instead multipart/alternative in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM